Skip to content

Commit

Permalink
Fix: Handle duration as order-by option value on CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed May 5, 2019
1 parent 2ffa923 commit 84f0bed
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/TextUI/Command.php
Expand Up @@ -1158,7 +1158,7 @@ protected function showHelp(): void
--printer <printer> TestListener implementation to use
--resolve-dependencies Resolve dependencies between tests
--order-by=<order> Run tests in order: default|defects|depends|random|reverse
--order-by=<order> Run tests in order: default|defects|depends|duration|random|reverse
--random-order-seed=<N> Use a specific random seed <N> for random order
--cache-result Write run result to cache to enable ordering tests defects-first
Expand Down Expand Up @@ -1356,6 +1356,11 @@ private function handleOrderByOption(string $value): void

break;

case 'duration':
$this->arguments['executionOrder'] = TestSuiteSorter::ORDER_DURATION;

break;

case 'random':
$this->arguments['executionOrder'] = TestSuiteSorter::ORDER_RANDOMIZED;

Expand Down
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

final class TestWithDifferentDurations extends TestCase
{
public function testOne(): void
{
$this->assertTrue(true);
}

public function testTwo(): void
{
$this->assertTrue(true);
}

public function testThree(): void
{
$this->assertTrue(true);
}
}
@@ -0,0 +1 @@
C:30:"PHPUnit\Runner\TestResultCache":199:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:3:{s:35:"TestWithDifferentDurations::testOne";d:1.000;s:35:"TestWithDifferentDurations::testTwo";d:0.500;s:37:"TestWithDifferentDurations::testThree";d:1.500;}}}
34 changes: 34 additions & 0 deletions tests/end-to-end/execution-order/order-by-duration-via-cli.phpt
@@ -0,0 +1,34 @@
--TEST--
phpunit --order-by=duration ./tests/end-to-end/_files/execution-order/TestWithDifferentDurations.php
--FILE--
<?php

$tmpResultCache = tempnam(sys_get_temp_dir(), __FILE__);
file_put_contents($tmpResultCache, file_get_contents(__DIR__ . '/../_files/execution-order/TestWithDifferentDurations.phpunit.result.cache.txt'));

$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--debug';
$_SERVER['argv'][3] = '--order-by=duration';
$_SERVER['argv'][4] = '--cache-result';
$_SERVER['argv'][5] = '--cache-result-file=' . $tmpResultCache;
$_SERVER['argv'][6] = 'TestWithDifferentDurations';
$_SERVER['argv'][7] = __DIR__ . '/../_files/execution-order/TestWithDifferentDurations.php';

require __DIR__ . '/../../bootstrap.php';
PHPUnit\TextUI\Command::main();

unlink($tmpResultCache);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Test 'TestWithDifferentDurations::testTwo' started
Test 'TestWithDifferentDurations::testTwo' ended
Test 'TestWithDifferentDurations::testOne' started
Test 'TestWithDifferentDurations::testOne' ended
Test 'TestWithDifferentDurations::testThree' started
Test 'TestWithDifferentDurations::testThree' ended


Time: %s, Memory: %s

OK (3 tests, 3 assertions)
2 changes: 1 addition & 1 deletion tests/end-to-end/help.phpt
Expand Up @@ -88,7 +88,7 @@ Test Execution Options:
--printer <printer> TestListener implementation to use

--resolve-dependencies Resolve dependencies between tests
--order-by=<order> Run tests in order: default|defects|depends|random|reverse
--order-by=<order> Run tests in order: default|defects|depends|duration|random|reverse
--random-order-seed=<N> Use a specific random seed <N> for random order
--cache-result Write run result to cache to enable ordering tests defects-first

Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/help2.phpt
Expand Up @@ -89,7 +89,7 @@ Test Execution Options:
--printer <printer> TestListener implementation to use

--resolve-dependencies Resolve dependencies between tests
--order-by=<order> Run tests in order: default|defects|depends|random|reverse
--order-by=<order> Run tests in order: default|defects|depends|duration|random|reverse
--random-order-seed=<N> Use a specific random seed <N> for random order
--cache-result Write run result to cache to enable ordering tests defects-first

Expand Down

0 comments on commit 84f0bed

Please sign in to comment.