Skip to content

WIZ-11443 Fix inheritance with phpunit #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Wizaplace\PHPUnit\Slicer;

use PHPUnit\Framework\TestSuite;

class Command extends \PHPUnit\TextUI\Command
{
public function __construct()
Expand Down Expand Up @@ -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);
}
}
}
24 changes: 0 additions & 24 deletions src/TestRunner.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/TestSuiteSlicer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -34,6 +34,8 @@ public static function slice(TestSuite $suite, array $arguments)
$offset+1,
$lastTestId
);

return $suite;
}

private static function extractTestsInSuite(TestSuite $suite) : array
Expand Down
81 changes: 45 additions & 36 deletions tests/TestSuiteSlicerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
}
10 changes: 5 additions & 5 deletions tests/fixtures/BTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
}
14 changes: 7 additions & 7 deletions tests/fixtures/CTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -27,6 +27,6 @@ public function dataProvider()
];
}

public function test N() { }
public function test O() { }
public function testN() { }
public function testO() { }
}