diff --git a/src/Command.php b/src/Command.php index 2a27329..1ec6834 100644 --- a/src/Command.php +++ b/src/Command.php @@ -4,6 +4,8 @@ namespace Wizaplace\PHPUnit\Slicer; +use PHPUnit\Framework\TestSuite; + class Command extends \PHPUnit\TextUI\Command { public function __construct() @@ -51,8 +53,16 @@ protected function showHelp(): void EOT; } - protected function createRunner(): \PHPUnit\TextUI\TestRunner + protected function handleArguments(array $argv): void { - return new TestRunner($this->arguments['loader']); + parent::handleArguments($argv); + + if (isset($this->arguments['totalSlices'], $this->arguments['currentSlice'], $this->arguments['test']) + && $this->arguments['test'] instanceof TestSuite + ) { + $localArguments = $this->arguments; + + $this->arguments['test'] = TestSuiteSlicer::slice($this->arguments['test'], $localArguments); + } } } diff --git a/src/TestRunner.php b/src/TestRunner.php deleted file mode 100644 index 9b02064..0000000 --- a/src/TestRunner.php +++ /dev/null @@ -1,24 +0,0 @@ -handleConfiguration($localArguments); - - TestSuiteSlicer::slice($suite, $localArguments); - } - - return parent::doRun($suite, $arguments, $exit); - } -} diff --git a/src/TestSuiteSlicer.php b/src/TestSuiteSlicer.php index 0a536d3..5c2dcf7 100644 --- a/src/TestSuiteSlicer.php +++ b/src/TestSuiteSlicer.php @@ -8,7 +8,7 @@ class TestSuiteSlicer { - public static function slice(TestSuite $suite, array $arguments) + public static function slice(TestSuite $suite, array $arguments): TestSuite { $tests = self::extractTestsInSuite($suite); @@ -34,6 +34,8 @@ public static function slice(TestSuite $suite, array $arguments) $offset+1, $lastTestId ); + + return $suite; } private static function extractTestsInSuite(TestSuite $suite) : array diff --git a/tests/TestSuiteSlicerTest.php b/tests/TestSuiteSlicerTest.php index aac23a4..d0cc1bd 100644 --- a/tests/TestSuiteSlicerTest.php +++ b/tests/TestSuiteSlicerTest.php @@ -33,93 +33,102 @@ public static function tearDownAfterClass(): void self::$tested = null; } - public function test slice first half() + public function testSliceReturnsATestSuiteObject(): void + { + $suite = clone self::$tested; + + $modifiedSuite = TestSuiteSlicer::slice($suite, ['currentSlice' => 1, 'totalSlices' => 2]); + + self::assertInstanceOf(TestSuite::class, $modifiedSuite); + } + + public function testSliceFirstHalf() { $suite = clone self::$tested; self::assertCount(19, $suite); ob_start(); - TestSuiteSlicer::slice($suite, ['currentSlice' => 1, 'totalSlices' => 2]); + $modifiedSuite = TestSuiteSlicer::slice($suite, ['currentSlice' => 1, 'totalSlices' => 2]); $output = ob_get_clean(); self::assertEquals("PHPUnit suite slicer, running slice 1/2 (10 tests: from #1 to #10)\n", $output); - self::assertCount(10, $suite); + self::assertCount(10, $modifiedSuite); $testsNames = array_map(function(TestCase $test) { return $test->getName(); - }, $this->extractTestsInSuite($suite)); + }, $this->extractTestsInSuite($modifiedSuite)); self::assertEquals([ - 'test A', - 'test B', - 'test C', - 'test D', - 'test E', - 'test F', - 'test G', - 'test H', - 'test I', - 'test J', + 'testA', + 'testB', + 'testC', + 'testD', + 'testE', + 'testF', + 'testG', + 'testH', + 'testI', + 'testJ', ], $testsNames); } - public function test slice second half() + public function testSliceSecondHalf() { $suite = clone self::$tested; self::assertCount(19, $suite); ob_start(); - TestSuiteSlicer::slice($suite, ['currentSlice' => 2, 'totalSlices' => 2]); + $modifiedSuite = TestSuiteSlicer::slice($suite, ['currentSlice' => 2, 'totalSlices' => 2]); $output = ob_get_clean(); self::assertEquals("PHPUnit suite slicer, running slice 2/2 (9 tests: from #11 to #19)\n", $output); - self::assertCount(9, $suite); + self::assertCount(9, $modifiedSuite); $testsNames = array_map(function(TestCase $test) { return $test->getName(); - }, $this->extractTestsInSuite($suite)); + }, $this->extractTestsInSuite($modifiedSuite)); self::assertEquals([ - 'test K', - 'test L', - 'test M with data set #0', - 'test M with data set #1', - 'test M with data set #2', - 'test M with data set #3', - 'test M with data set #4', - 'test N', - 'test O', + 'testK', + 'testL', + 'testM with data set #0', + 'testM with data set #1', + 'testM with data set #2', + 'testM with data set #3', + 'testM with data set #4', + 'testN', + 'testO', ], $testsNames); } - public function test slice last third() + public function testSliceLastThird() { $suite = clone self::$tested; self::assertCount(19, $suite); ob_start(); - TestSuiteSlicer::slice($suite, ['currentSlice' => 3, 'totalSlices' => 3]); + $modifiedSuite = TestSuiteSlicer::slice($suite, ['currentSlice' => 3, 'totalSlices' => 3]); $output = ob_get_clean(); self::assertEquals("PHPUnit suite slicer, running slice 3/3 (5 tests: from #15 to #19)\n", $output); - self::assertCount(5, $suite); + self::assertCount(5, $modifiedSuite); $testsNames = array_map(function(TestCase $test) { return $test->getName(); - }, $this->extractTestsInSuite($suite)); + }, $this->extractTestsInSuite($modifiedSuite)); self::assertEquals([ - 'test M with data set #2', - 'test M with data set #3', - 'test M with data set #4', - 'test N', - 'test O', + 'testM with data set #2', + 'testM with data set #3', + 'testM with data set #4', + 'testN', + 'testO', ], $testsNames); } diff --git a/tests/fixtures/ATest.php b/tests/fixtures/ATest.php index fa82ad1..6d86085 100644 --- a/tests/fixtures/ATest.php +++ b/tests/fixtures/ATest.php @@ -6,7 +6,7 @@ class ATest extends TestCase { - public function test A() { } - public function test B() { } - public function test C() { } + public function testA() { } + public function testB() { } + public function testC() { } } diff --git a/tests/fixtures/BTest.php b/tests/fixtures/BTest.php index ec76cfd..a0bfccf 100644 --- a/tests/fixtures/BTest.php +++ b/tests/fixtures/BTest.php @@ -6,9 +6,9 @@ class BTest extends TestCase { - public function test D() { } - public function test E() { } - public function test F() { } - public function test G() { } - public function test H() { } + public function testD() { } + public function testE() { } + public function testF() { } + public function testG() { } + public function testH() { } } diff --git a/tests/fixtures/CTest.php b/tests/fixtures/CTest.php index d40aff7..f14b274 100644 --- a/tests/fixtures/CTest.php +++ b/tests/fixtures/CTest.php @@ -6,15 +6,15 @@ class CTest extends TestCase { - public function test I() { } - public function test J() { } - public function test K() { } - public function test L() { } + public function testI() { } + public function testJ() { } + public function testK() { } + public function testL() { } /** * @dataProvider dataProvider */ - public function test M($a) { } + public function testM($a) { } public function dataProvider() { @@ -27,6 +27,6 @@ public function dataProvider() ]; } - public function test N() { } - public function test O() { } + public function testN() { } + public function testO() { } }