From 19b292d7dbce639a5fc9c59e7a2a1a02d08fa6e8 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 13 Oct 2021 13:03:42 +0200 Subject: [PATCH] Add an option to output order of test execution --- src/Command/AsynitCommand.php | 3 +- src/Output/OutputFactory.php | 11 ++++++ src/Output/OutputOrder.php | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/Output/OutputOrder.php diff --git a/src/Command/AsynitCommand.php b/src/Command/AsynitCommand.php index 8ff6ff6..dd0205d 100644 --- a/src/Command/AsynitCommand.php +++ b/src/Command/AsynitCommand.php @@ -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') ; } @@ -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()); diff --git a/src/Output/OutputFactory.php b/src/Output/OutputFactory.php index 572786f..0fb8966 100644 --- a/src/Output/OutputFactory.php +++ b/src/Output/OutputFactory.php @@ -9,6 +9,13 @@ */ class OutputFactory { + private $order = false; + + public function __construct(bool $order = false) + { + $this->order = $order; + } + public function buildOutput(int $testCount): array { $countOutput = new Count(); @@ -16,6 +23,10 @@ public function buildOutput(int $testCount): array $chainOutput->addOutput(new PhpUnitAlike($testCount)); $chainOutput->addOutput($countOutput); + if ($this->order) { + $chainOutput->addOutput(new OutputOrder()); + } + return [$chainOutput, $countOutput]; } } diff --git a/src/Output/OutputOrder.php b/src/Output/OutputOrder.php new file mode 100644 index 0000000..52e68b4 --- /dev/null +++ b/src/Output/OutputOrder.php @@ -0,0 +1,65 @@ +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; + } +} \ No newline at end of file