Skip to content

Commit

Permalink
Merge pull request #54 from jolicode/feature/order-test
Browse files Browse the repository at this point in the history
Add an option to output order of test execution
  • Loading branch information
lyrixx committed Oct 13, 2021
2 parents 6cdba8c + 19b292d commit a7d225c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Command/AsynitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function configure()
->addOption('allow-self-signed-certificate', null, InputOption::VALUE_NONE, 'Allow self signed ssl certificate')
->addOption('concurrency', null, InputOption::VALUE_REQUIRED, 'Max number of parallels requests', 10)
->addOption('bootstrap', null, InputOption::VALUE_REQUIRED, 'A PHP file to include before anything else', $this->defaultBootstrapFilename)
->addOption('order', null, InputOption::VALUE_NONE, 'Output tests execution order')
;
}

Expand All @@ -52,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$testsFinder = new TestsFinder();
$testMethods = $testsFinder->findTests($input->getArgument('target'));

list($chainOutput, $countOutput) = (new OutputFactory())->buildOutput(\count($testMethods));
list($chainOutput, $countOutput) = (new OutputFactory($input->getOption('order')))->buildOutput(\count($testMethods));

// Build services for parsing and running tests
$builder = new TestPoolBuilder(new AnnotationReader());
Expand Down
11 changes: 11 additions & 0 deletions src/Output/OutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
*/
class OutputFactory
{
private $order = false;

public function __construct(bool $order = false)
{
$this->order = $order;
}

public function buildOutput(int $testCount): array
{
$countOutput = new Count();
$chainOutput = new Chain();
$chainOutput->addOutput(new PhpUnitAlike($testCount));
$chainOutput->addOutput($countOutput);

if ($this->order) {
$chainOutput->addOutput(new OutputOrder());
}

return [$chainOutput, $countOutput];
}
}
65 changes: 65 additions & 0 deletions src/Output/OutputOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Asynit\Output;

use Asynit\Test;

class OutputOrder implements OutputInterface
{
/** @var Test[] */
private $tests = [];

public function outputStep(Test $test, $debugOutput)
{
}

public function outputFailure(Test $test, $debugOutput, $failure)
{
$this->tests[] = $test;
}

public function outputSuccess(Test $test, $debugOutput)
{
$this->tests[] = $test;
}

public function outputSkipped(Test $test, $debugOutput)
{
}

public function __destruct()
{
fwrite(STDOUT, "\nTest orders:\n\n");

$orders = [];

foreach ($this->tests as $index => $test) {
$depends = $this->createDepends($test, $orders);
$orders[$test->getDisplayName()] = $index;
$dependsStr = "";

if (\count($depends) > 0) {
$dependsStr = " depends on " . join(", ", array_map(function ($i) {
return "#" . $i;
}, $depends));
}

fwrite(STDOUT, " - #".$index . ' ' . $test->getDisplayName() . $dependsStr . "\n");
}
}

public function createDepends(Test $test, array $orders = []): array
{
$depends = [];

foreach ($test->getParents() as $parentTest) {
$depends = array_merge($depends, $this->createDepends($parentTest, $orders));
}

if (\array_key_exists($test->getDisplayName(), $orders)) {
$depends[] = $orders[$test->getDisplayName()];
}

return $depends;
}
}

0 comments on commit a7d225c

Please sign in to comment.