From 865cbb8aaa3aa00870c9a2265f5d9061d437337f Mon Sep 17 00:00:00 2001 From: borNfreee Date: Sat, 13 Jun 2020 13:30:52 +0300 Subject: [PATCH 1/8] Initial setup for Pest Adapter --- src/TestFramework/Factory.php | 21 +++- .../PhpUnit/Adapter/PestAdapter.php | 104 ++++++++++++++++ .../PhpUnit/Adapter/PestAdapterFactory.php | 117 ++++++++++++++++++ .../Config/Builder/MutationConfigBuilder.php | 1 + src/TestFramework/TestFrameworkTypes.php | 2 + tests/e2e/PestTestFramework/README.md | 21 ++++ tests/e2e/PestTestFramework/composer.json | 21 ++++ .../e2e/PestTestFramework/expected-output.txt | 8 ++ tests/e2e/PestTestFramework/infection.json | 12 ++ tests/e2e/PestTestFramework/phpunit.xml | 18 +++ .../e2e/PestTestFramework/src/Calculator.php | 15 +++ .../e2e/PestTestFramework/src/SourceClass.php | 16 +++ .../tests/CalculatorTest.php | 10 ++ .../tests/SourceClassTest.php | 21 ++++ 14 files changed, 386 insertions(+), 1 deletion(-) create mode 100644 src/TestFramework/PhpUnit/Adapter/PestAdapter.php create mode 100644 src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php create mode 100644 tests/e2e/PestTestFramework/README.md create mode 100644 tests/e2e/PestTestFramework/composer.json create mode 100644 tests/e2e/PestTestFramework/expected-output.txt create mode 100644 tests/e2e/PestTestFramework/infection.json create mode 100644 tests/e2e/PestTestFramework/phpunit.xml create mode 100644 tests/e2e/PestTestFramework/src/Calculator.php create mode 100644 tests/e2e/PestTestFramework/src/SourceClass.php create mode 100644 tests/e2e/PestTestFramework/tests/CalculatorTest.php create mode 100644 tests/e2e/PestTestFramework/tests/SourceClassTest.php diff --git a/src/TestFramework/Factory.php b/src/TestFramework/Factory.php index a30985e22..68f5fc7ed 100644 --- a/src/TestFramework/Factory.php +++ b/src/TestFramework/Factory.php @@ -41,6 +41,7 @@ use Infection\Configuration\Configuration; use Infection\FileSystem\Finder\TestFrameworkFinder; use Infection\TestFramework\Config\TestFrameworkConfigLocatorInterface; +use Infection\TestFramework\PhpUnit\Adapter\PestAdapterFactory; use Infection\TestFramework\PhpUnit\Adapter\PhpUnitAdapterFactory; use InvalidArgumentException; use function is_a; @@ -105,7 +106,25 @@ public function create(string $adapterName, bool $skipCoverage): TestFrameworkAd ); } - $availableTestFrameworks = [TestFrameworkTypes::PHPUNIT]; + if ($adapterName === TestFrameworkTypes::PEST) { + $pestConfigPath = $this->configLocator->locate(TestFrameworkTypes::PHPUNIT); + + return PestAdapterFactory::create( + $this->testFrameworkFinder->find( + TestFrameworkTypes::PEST, + (string) $this->infectionConfig->getPhpUnit()->getCustomPath() + ), + $this->tmpDir, + $pestConfigPath, + (string) $this->infectionConfig->getPhpUnit()->getConfigDir(), + $this->jUnitFilePath, + $this->projectDir, + $this->infectionConfig->getSourceDirectories(), + $skipCoverage + ); + } + + $availableTestFrameworks = [TestFrameworkTypes::PHPUNIT, TestFrameworkTypes::PEST]; foreach ($this->installedExtensions as $installedExtension) { $factory = $installedExtension['extra']['class']; diff --git a/src/TestFramework/PhpUnit/Adapter/PestAdapter.php b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php new file mode 100644 index 000000000..68a77f5bd --- /dev/null +++ b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php @@ -0,0 +1,104 @@ +phpUnitAdapter = $phpUnitAdapter; + } + + public function getName(): string + { + return self::NAME; + } + + public function testsPass(string $output): bool + { + // Tests: 7 failed + if (preg_match('/Tests:\s+(.*?)(\d+\sfailed)/i', $output) === 1) { + return false; + } + + // Tests: 4 passed + $isOk = preg_match('/Tests:\s+(.*?)(\d+\spassed)/', $output) === 1; + + // Tests: 1 risked + $isOkRisked = preg_match('/Tests:\s+(.*?)(\d+\srisked)/', $output) === 1; + + return $isOk || $isOkRisked; + } + + public function hasJUnitReport(): bool + { + return $this->phpUnitAdapter->hasJUnitReport(); + } + + public function getInitialTestRunCommandLine(string $extraOptions, array $phpExtraArgs, bool $skipCoverage): array + { + return $this->phpUnitAdapter->getInitialTestRunCommandLine($extraOptions, $phpExtraArgs, $skipCoverage); + } + + public function getMutantCommandLine(array $coverageTests, string $mutatedFilePath, string $mutationHash, string $mutationOriginalFilePath, string $extraOptions): array + { + return $this->phpUnitAdapter->getMutantCommandLine( + $coverageTests, + $mutatedFilePath, + $mutationHash, + $mutationOriginalFilePath, + sprintf('--colors=never %s', $extraOptions) + ); + } + + public function getVersion(): string + { + return $this->phpUnitAdapter->getVersion(); + } + + public function getInitialTestsFailRecommendations(string $commandLine): string + { + return $this->phpUnitAdapter->getInitialTestsFailRecommendations($commandLine); + } +} diff --git a/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php b/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php new file mode 100644 index 000000000..c455f0d45 --- /dev/null +++ b/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php @@ -0,0 +1,117 @@ +setCustomBootstrapPath($customAutoloadFilePath, $xPath); $this->setFilteredTestsToRun($tests, $dom, $xPath); + // todo where is caching? we had it before I guess file_put_contents( $customAutoloadFilePath, $this->createCustomAutoloadWithInterceptor( diff --git a/src/TestFramework/TestFrameworkTypes.php b/src/TestFramework/TestFrameworkTypes.php index effe131d4..e21513ca3 100644 --- a/src/TestFramework/TestFrameworkTypes.php +++ b/src/TestFramework/TestFrameworkTypes.php @@ -41,10 +41,12 @@ final class TestFrameworkTypes { public const PHPUNIT = 'phpunit'; + public const PEST = 'pest'; public const PHPSPEC = 'phpspec'; public const CODECEPTION = 'codeception'; public const TYPES = [ + self::PEST, self::PHPUNIT, self::PHPSPEC, self::CODECEPTION, diff --git a/tests/e2e/PestTestFramework/README.md b/tests/e2e/PestTestFramework/README.md new file mode 100644 index 000000000..aa8e3fb07 --- /dev/null +++ b/tests/e2e/PestTestFramework/README.md @@ -0,0 +1,21 @@ +# Pest Test Framework integration with Infection + +* https://github.com/pestphp/pest/pull/291 +* https://github.com/infection/infection/issues/1476 + +## Summary + +This test ensures Pest is working correctly with Infection. + +To manually check it, run: + +``` +git clone git@github.com:infection/infection.git +cd infection +git checkout feature/pest-adapter + +cd tests/e2e/PestTestFramework +composer install + +XDEBUG_MODE=coverage ../../../bin/infection --test-framework=pest --log-verbosity=all -s +``` diff --git a/tests/e2e/PestTestFramework/composer.json b/tests/e2e/PestTestFramework/composer.json new file mode 100644 index 000000000..47abc58b7 --- /dev/null +++ b/tests/e2e/PestTestFramework/composer.json @@ -0,0 +1,21 @@ +{ + "require-dev": { + "pestphp/pest": "dev-feat-junit" + }, + "autoload": { + "psr-4": { + "PestTestFramework\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "PestTestFramework\\Test\\": "tests/" + } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/olivernybroe/pest-1" + } + ] +} diff --git a/tests/e2e/PestTestFramework/expected-output.txt b/tests/e2e/PestTestFramework/expected-output.txt new file mode 100644 index 000000000..598ec1191 --- /dev/null +++ b/tests/e2e/PestTestFramework/expected-output.txt @@ -0,0 +1,8 @@ +Total: 5 + +Killed: 4 +Errored: 0 +Escaped: 1 +Timed Out: 0 +Skipped: 0 +Not Covered: 0 diff --git a/tests/e2e/PestTestFramework/infection.json b/tests/e2e/PestTestFramework/infection.json new file mode 100644 index 000000000..7029746fb --- /dev/null +++ b/tests/e2e/PestTestFramework/infection.json @@ -0,0 +1,12 @@ +{ + "timeout": 25, + "source": { + "directories": [ + "src" + ] + }, + "logs": { + "summary": "infection.log" + }, + "tmpDir": "." +} diff --git a/tests/e2e/PestTestFramework/phpunit.xml b/tests/e2e/PestTestFramework/phpunit.xml new file mode 100644 index 000000000..df138fe3f --- /dev/null +++ b/tests/e2e/PestTestFramework/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + + + + ./src/ + + + diff --git a/tests/e2e/PestTestFramework/src/Calculator.php b/tests/e2e/PestTestFramework/src/Calculator.php new file mode 100644 index 000000000..8f6f3c5c7 --- /dev/null +++ b/tests/e2e/PestTestFramework/src/Calculator.php @@ -0,0 +1,15 @@ +sub(3, 1))->toBe(2); +}); + diff --git a/tests/e2e/PestTestFramework/tests/SourceClassTest.php b/tests/e2e/PestTestFramework/tests/SourceClassTest.php new file mode 100644 index 000000000..71d1fb8f1 --- /dev/null +++ b/tests/e2e/PestTestFramework/tests/SourceClassTest.php @@ -0,0 +1,21 @@ +hello())->toBe('hello'); +}); + +test('Another test from SourceTest.php', function () { + $sourceClass = new SourceClass(); + + expect($sourceClass->hello())->toBe('hello'); +}); + +test('Third test from SourceTest.php', function () { + $sourceClass = new SourceClass(); + + expect($sourceClass->add(5, 3))->toBeGreaterThan(0); +}); From 839ff540d97edd93270118458c8a5a45e5eccbf4 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Mon, 3 May 2021 08:22:57 +0300 Subject: [PATCH 2/8] Add Pest, PhpUnit and mixed tested classes --- tests/e2e/PestTestFramework/README.md | 6 ++++++ .../e2e/PestTestFramework/src/Calculator.php | 5 +++++ .../src/{SourceClass.php => ForPest.php} | 2 +- .../e2e/PestTestFramework/src/ForPhpUnit.php | 15 ++++++++++++++ ...culatorTest.php => CalculatorPestTest.php} | 0 .../tests/CalculatorPhpUnitTest.php | 20 +++++++++++++++++++ .../{SourceClassTest.php => ForPestTest.php} | 8 ++++---- .../tests/ForPhpUnitTest.php | 20 +++++++++++++++++++ 8 files changed, 71 insertions(+), 5 deletions(-) rename tests/e2e/PestTestFramework/src/{SourceClass.php => ForPest.php} (91%) create mode 100644 tests/e2e/PestTestFramework/src/ForPhpUnit.php rename tests/e2e/PestTestFramework/tests/{CalculatorTest.php => CalculatorPestTest.php} (100%) create mode 100644 tests/e2e/PestTestFramework/tests/CalculatorPhpUnitTest.php rename tests/e2e/PestTestFramework/tests/{SourceClassTest.php => ForPestTest.php} (69%) create mode 100644 tests/e2e/PestTestFramework/tests/ForPhpUnitTest.php diff --git a/tests/e2e/PestTestFramework/README.md b/tests/e2e/PestTestFramework/README.md index aa8e3fb07..9e2839843 100644 --- a/tests/e2e/PestTestFramework/README.md +++ b/tests/e2e/PestTestFramework/README.md @@ -7,6 +7,12 @@ This test ensures Pest is working correctly with Infection. +Cases: + +- `ForPest.php` file is tested only by Pest +- `ForPhpUnit.php` file is tested only by PhpUnit +- `Calculator.php` file is tested by Pest and PhpUnit + To manually check it, run: ``` diff --git a/tests/e2e/PestTestFramework/src/Calculator.php b/tests/e2e/PestTestFramework/src/Calculator.php index 8f6f3c5c7..8692051ab 100644 --- a/tests/e2e/PestTestFramework/src/Calculator.php +++ b/tests/e2e/PestTestFramework/src/Calculator.php @@ -12,4 +12,9 @@ public function sub(int $a, int $b): int { return $a - $b; } + + public function mul(int $a, int $b) + { + return $a * $b; + } } diff --git a/tests/e2e/PestTestFramework/src/SourceClass.php b/tests/e2e/PestTestFramework/src/ForPest.php similarity index 91% rename from tests/e2e/PestTestFramework/src/SourceClass.php rename to tests/e2e/PestTestFramework/src/ForPest.php index 513e1ce7a..e8ce3d184 100644 --- a/tests/e2e/PestTestFramework/src/SourceClass.php +++ b/tests/e2e/PestTestFramework/src/ForPest.php @@ -2,7 +2,7 @@ namespace PestTestFramework; -class SourceClass +class ForPest { public function hello(): string { diff --git a/tests/e2e/PestTestFramework/src/ForPhpUnit.php b/tests/e2e/PestTestFramework/src/ForPhpUnit.php new file mode 100644 index 000000000..0130e592a --- /dev/null +++ b/tests/e2e/PestTestFramework/src/ForPhpUnit.php @@ -0,0 +1,15 @@ +mul(2, 4)); + } +} diff --git a/tests/e2e/PestTestFramework/tests/SourceClassTest.php b/tests/e2e/PestTestFramework/tests/ForPestTest.php similarity index 69% rename from tests/e2e/PestTestFramework/tests/SourceClassTest.php rename to tests/e2e/PestTestFramework/tests/ForPestTest.php index 71d1fb8f1..6592b9748 100644 --- a/tests/e2e/PestTestFramework/tests/SourceClassTest.php +++ b/tests/e2e/PestTestFramework/tests/ForPestTest.php @@ -1,21 +1,21 @@ hello())->toBe('hello'); }); test('Another test from SourceTest.php', function () { - $sourceClass = new SourceClass(); + $sourceClass = new ForPest(); expect($sourceClass->hello())->toBe('hello'); }); test('Third test from SourceTest.php', function () { - $sourceClass = new SourceClass(); + $sourceClass = new ForPest(); expect($sourceClass->add(5, 3))->toBeGreaterThan(0); }); diff --git a/tests/e2e/PestTestFramework/tests/ForPhpUnitTest.php b/tests/e2e/PestTestFramework/tests/ForPhpUnitTest.php new file mode 100644 index 000000000..3eeab791e --- /dev/null +++ b/tests/e2e/PestTestFramework/tests/ForPhpUnitTest.php @@ -0,0 +1,20 @@ +getArray()); + } +} From 0138da1e7eea2d27bf30892c333c51e3af973f17 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Mon, 3 May 2021 08:35:22 +0300 Subject: [PATCH 3/8] Add inline and shared datasets for Pest --- tests/e2e/PestTestFramework/expected-output.txt | 6 +++--- .../src/ForPestWithDataProvider.php | 15 +++++++++++++++ .../PestTestFramework/tests/Datasets/Floats.php | 6 ++++++ .../tests/ForPestWithDataProviderTest.php | 17 +++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/PestTestFramework/src/ForPestWithDataProvider.php create mode 100644 tests/e2e/PestTestFramework/tests/Datasets/Floats.php create mode 100644 tests/e2e/PestTestFramework/tests/ForPestWithDataProviderTest.php diff --git a/tests/e2e/PestTestFramework/expected-output.txt b/tests/e2e/PestTestFramework/expected-output.txt index 598ec1191..0a408ead3 100644 --- a/tests/e2e/PestTestFramework/expected-output.txt +++ b/tests/e2e/PestTestFramework/expected-output.txt @@ -1,8 +1,8 @@ -Total: 5 +Total: 11 -Killed: 4 +Killed: 9 Errored: 0 -Escaped: 1 +Escaped: 2 Timed Out: 0 Skipped: 0 Not Covered: 0 diff --git a/tests/e2e/PestTestFramework/src/ForPestWithDataProvider.php b/tests/e2e/PestTestFramework/src/ForPestWithDataProvider.php new file mode 100644 index 000000000..a9d4ba7d2 --- /dev/null +++ b/tests/e2e/PestTestFramework/src/ForPestWithDataProvider.php @@ -0,0 +1,15 @@ +div($a, $b))->toBe($expectedResult); +})->with([ + [2.0, 4.0, 0.5] +]); + +test('tests division with shared dataset', function (float $a, float $b, float $expectedResult) { + $sourceClass = new ForPestWithDataProvider(); + + expect($sourceClass->div($a, $b))->toBe($expectedResult); +})->with('floats'); From af1223306a86fb2f6f60ef93cc991c97c4ab1191 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Mon, 3 May 2021 23:43:05 +0300 Subject: [PATCH 4/8] Add tests --- devTools/phpstan-src-baseline.neon | 5 + .../PhpUnit/Adapter/PestAdapter.php | 34 +- .../PhpUnit/Adapter/PestAdapterFactory.php | 2 +- .../PhpUnit/Adapter/PhpUnitAdapterFactory.php | 2 +- .../Config/Builder/MutationConfigBuilder.php | 1 - .../Adapter/PestAdapterFactoryTest.php | 71 +++++ .../PhpUnit/Adapter/PestAdapterTest.php | 300 ++++++++++++++++++ 7 files changed, 409 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterFactoryTest.php create mode 100644 tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php diff --git a/devTools/phpstan-src-baseline.neon b/devTools/phpstan-src-baseline.neon index 91cb14cb7..687b28cb7 100644 --- a/devTools/phpstan-src-baseline.neon +++ b/devTools/phpstan-src-baseline.neon @@ -305,6 +305,11 @@ parameters: count: 1 path: ../src/TestFramework/Coverage/XmlReport/XPathFactory.php + - + message: "#^Method Infection\\\\TestFramework\\\\PhpUnit\\\\Adapter\\\\PestAdapterFactory\\:\\:create\\(\\) has parameter \\$sourceDirectories with no value type specified in iterable type array\\.$#" + count: 1 + path: ../src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php + - message: "#^Only booleans are allowed in an if condition, int given\\.$#" count: 3 diff --git a/src/TestFramework/PhpUnit/Adapter/PestAdapter.php b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php index 68a77f5bd..a27b43b13 100644 --- a/src/TestFramework/PhpUnit/Adapter/PestAdapter.php +++ b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php @@ -35,13 +35,20 @@ namespace Infection\TestFramework\PhpUnit\Adapter; +use Infection\AbstractTestFramework\MemoryUsageAware; use Infection\AbstractTestFramework\TestFrameworkAdapter; +use Infection\PhpParser\Visitor\IgnoreNode\PhpUnitCodeCoverageAnnotationIgnorer; +use Infection\TestFramework\IgnoresAdditionalNodes; +use Infection\TestFramework\ProvidesInitialRunOnlyOptions; use function Safe\preg_match; -use function sprintf; +use function Safe\sprintf; -class PestAdapter implements TestFrameworkAdapter +/** + * @internal + */ +final class PestAdapter implements IgnoresAdditionalNodes, MemoryUsageAware, ProvidesInitialRunOnlyOptions, TestFrameworkAdapter { - private const NAME = 'pest'; + private const NAME = 'Pest'; private PhpUnitAdapter $phpUnitAdapter; @@ -101,4 +108,25 @@ public function getInitialTestsFailRecommendations(string $commandLine): string { return $this->phpUnitAdapter->getInitialTestsFailRecommendations($commandLine); } + + public function getMemoryUsed(string $output): float + { + return $this->phpUnitAdapter->getMemoryUsed($output); + } + + /** + * @return PhpUnitCodeCoverageAnnotationIgnorer[] + */ + public function getNodeIgnorers(): array + { + return $this->phpUnitAdapter->getNodeIgnorers(); + } + + /** + * @return string[] + */ + public function getInitialRunOnlyOptions(): array + { + return $this->phpUnitAdapter->getInitialRunOnlyOptions(); + } } diff --git a/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php b/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php index c455f0d45..591b5d6f9 100644 --- a/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php +++ b/src/TestFramework/PhpUnit/Adapter/PestAdapterFactory.php @@ -66,7 +66,7 @@ public static function create( array $sourceDirectories, bool $skipCoverage ): TestFrameworkAdapter { - Assert::string($testFrameworkConfigDir, 'Config dir is not allowed to be `null` for the phpunit adapter'); + Assert::string($testFrameworkConfigDir, 'Config dir is not allowed to be `null` for the Pest adapter'); $testFrameworkConfigContent = file_get_contents($testFrameworkConfigPath); diff --git a/src/TestFramework/PhpUnit/Adapter/PhpUnitAdapterFactory.php b/src/TestFramework/PhpUnit/Adapter/PhpUnitAdapterFactory.php index ebbc7f827..cbc52b6c1 100644 --- a/src/TestFramework/PhpUnit/Adapter/PhpUnitAdapterFactory.php +++ b/src/TestFramework/PhpUnit/Adapter/PhpUnitAdapterFactory.php @@ -69,7 +69,7 @@ public static function create( array $sourceDirectories, bool $skipCoverage ): TestFrameworkAdapter { - Assert::string($testFrameworkConfigDir, 'Config dir is not allowed to be `null` for the phpunit adapter'); + Assert::string($testFrameworkConfigDir, 'Config dir is not allowed to be `null` for the Pest adapter'); $testFrameworkConfigContent = file_get_contents($testFrameworkConfigPath); diff --git a/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php b/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php index 28779020a..bc9adb3a7 100644 --- a/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php +++ b/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php @@ -115,7 +115,6 @@ public function build( $this->setCustomBootstrapPath($customAutoloadFilePath, $xPath); $this->setFilteredTestsToRun($tests, $dom, $xPath); - // todo where is caching? we had it before I guess file_put_contents( $customAutoloadFilePath, $this->createCustomAutoloadWithInterceptor( diff --git a/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterFactoryTest.php b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterFactoryTest.php new file mode 100644 index 000000000..43066aa5b --- /dev/null +++ b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterFactoryTest.php @@ -0,0 +1,71 @@ +assertSame('Pest', $adapter->getName()); + } + + public function test_it_has_a_name(): void + { + $this->assertSame('pest', PestAdapterFactory::getAdapterName()); + } + + public function test_it_has_an_executable(): void + { + $this->assertSame('pest', PestAdapterFactory::getExecutableName()); + } +} diff --git a/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php new file mode 100644 index 000000000..38787733b --- /dev/null +++ b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php @@ -0,0 +1,300 @@ +pcovDirectoryProvider = $this->createMock(PCOVDirectoryProvider::class); + $this->initialConfigBuilder = $this->createMock(InitialConfigBuilder::class); + $this->mutationConfigBuilder = $this->createMock(MutationConfigBuilder::class); + $this->cliArgumentsBuilder = $this->createMock(CommandLineArgumentsAndOptionsBuilder::class); + $this->commandLineBuilder = $this->createMock(CommandLineBuilder::class); + + $this->adapter = new PestAdapter( + new PhpUnitAdapter( + '/path/to/pest', + '/tmp', + '/tmp/infection/junit.xml', + $this->pcovDirectoryProvider, + $this->initialConfigBuilder, + $this->mutationConfigBuilder, + $this->cliArgumentsBuilder, + new VersionParser(), + $this->commandLineBuilder, + '1.1.0' + ) + ); + } + + public function test_it_has_a_name(): void + { + $this->assertSame('Pest', $this->adapter->getName()); + } + + public function test_it_supports_junit_reports(): void + { + $this->assertTrue($this->adapter->hasJUnitReport()); + } + + /** + * @dataProvider outputProvider + */ + public function test_it_can_tell_the_outcome_of_the_tests_from_the_output( + string $output, + bool $expected + ): void { + $actual = $this->adapter->testsPass($output); + + $this->assertSame($expected, $actual); + } + + /** + * @dataProvider memoryReportProvider + */ + public function test_it_can_tell_the_memory_usage_from_the_output(string $output, float $expectedResult): void + { + $result = $this->adapter->getMemoryUsed($output); + + $this->assertSame($expectedResult, $result); + } + + public function test_it_provides_initial_run_only_options(): void + { + $options = $this->adapter->getInitialRunOnlyOptions(); + + $this->assertSame( + ['--configuration', '--filter', '--testsuite'], + $options + ); + } + + public function test_it_provides_node_ignorers(): void + { + $nodeIgnorers = array_map('get_class', $this->adapter->getNodeIgnorers()); + + $this->assertSame( + [PhpUnitCodeCoverageAnnotationIgnorer::class], + $nodeIgnorers + ); + } + + /** + * @group integration + */ + public function test_it_provides_initial_test_run_command_line_when_no_coverage_is_expected(): void + { + $this->cliArgumentsBuilder + ->expects($this->once()) + ->method('build') + ->with('', '--group=default') + ; + + $this->commandLineBuilder + ->expects($this->once()) + ->method('build') + ->with('/path/to/pest', ['-d', 'memory_limit=-1'], []) + ->willReturn(['/path/to/pest', '--dummy-argument']) + ; + + $this->pcovDirectoryProvider + ->expects($this->never()) + ->method($this->anything()) + ; + + $initialTestRunCommandLine = $this->adapter->getInitialTestRunCommandLine('--group=default', ['-d', 'memory_limit=-1'], true); + + $this->assertSame( + [ + '/path/to/pest', + '--dummy-argument', + ], + $initialTestRunCommandLine + ); + } + + /** + * @group integration + */ + public function test_it_provides_initial_test_run_command_line_when_coverage_report_is_requested(): void + { + $this->cliArgumentsBuilder + ->expects($this->once()) + ->method('build') + ->with('', '--group=default --coverage-xml=/tmp/coverage-xml --log-junit=/tmp/infection/junit.xml') + ->willReturn([ + '--group=default', '--coverage-xml=/tmp/coverage-xml', '--log-junit=/tmp/infection/junit.xml', + ]) + ; + + $this->commandLineBuilder + ->expects($this->once()) + ->method('build') + ->with('/path/to/pest', ['-d', 'memory_limit=-1'], [ + '--group=default', '--coverage-xml=/tmp/coverage-xml', '--log-junit=/tmp/infection/junit.xml', + ]) + ->willReturn([ + '/path/to/pest', + '--group=default', + '--coverage-xml=/tmp/coverage-xml', + '--log-junit=/tmp/infection/junit.xml', + ]) + ; + + $this->pcovDirectoryProvider + ->expects($this->once()) + ->method('shallProvide') + ->willReturn(false) + ; + + $this->pcovDirectoryProvider + ->expects($this->never()) + ->method('getDirectory') + ; + + $initialTestRunCommandLine = $this->adapter->getInitialTestRunCommandLine('--group=default', ['-d', 'memory_limit=-1'], false); + + $this->assertSame( + [ + '/path/to/pest', + '--group=default', + '--coverage-xml=/tmp/coverage-xml', + '--log-junit=/tmp/infection/junit.xml', + ], + $initialTestRunCommandLine + ); + } + + /** + * @group integration + */ + public function test_it_provides_initial_test_run_command_line_when_coverage_report_is_requested_and_pcov_is_in_use(): void + { + $this->cliArgumentsBuilder + ->expects($this->once()) + ->method('build') + ->with('', '--group=default --coverage-xml=/tmp/coverage-xml --log-junit=/tmp/infection/junit.xml') + ->willReturn([ + '--group=default', '--coverage-xml=/tmp/coverage-xml', '--log-junit=/tmp/infection/junit.xml', + ]) + ; + + $this->commandLineBuilder + ->expects($this->once()) + ->method('build') + ->with('/path/to/pest', [ + '-d', + 'memory_limit=-1', + '-d', + '\\' === DIRECTORY_SEPARATOR ? 'pcov.directory="."' : "pcov.directory='.'", + ], [ + '--group=default', '--coverage-xml=/tmp/coverage-xml', '--log-junit=/tmp/infection/junit.xml', + ]) + ->willReturn([ + '/path/to/pest', + '--group=default', + '--coverage-xml=/tmp/coverage-xml', + '--log-junit=/tmp/infection/junit.xml', + ]) + ; + + $this->pcovDirectoryProvider + ->expects($this->once()) + ->method('shallProvide') + ->willReturn(true) + ; + + $this->pcovDirectoryProvider + ->expects($this->once()) + ->method('getDirectory') + ->willReturn('.') + ; + + $initialTestRunCommandLine = $this->adapter->getInitialTestRunCommandLine('--group=default', ['-d', 'memory_limit=-1'], false); + + $this->assertSame( + [ + '/path/to/pest', + '--group=default', + '--coverage-xml=/tmp/coverage-xml', + '--log-junit=/tmp/infection/junit.xml', + ], + $initialTestRunCommandLine + ); + } + + public function outputProvider(): iterable + { + yield ['Tests: 1 risked', true]; + + yield ['Tests: 9 passed', true]; + + yield ['Tests: 1 failed, 8 passed', false]; + } + + public function memoryReportProvider(): iterable + { + yield ['Memory: 8.00MB', 8.0]; + + yield ['Memory: 68.00MB', 68.0]; + + yield ['Memory: 68.00 MB', 68.0]; + + yield ['Time: 2.51 seconds', -1.0]; + } +} From 6ed4cdaf3f8cfd72d66d246140aca69bb6c30bf8 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Tue, 4 May 2021 00:18:28 +0300 Subject: [PATCH 5/8] Add custom `run_tests.bash` to run Pest test framework --- tests/e2e/PestTestFramework/run_tests.bash | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/e2e/PestTestFramework/run_tests.bash diff --git a/tests/e2e/PestTestFramework/run_tests.bash b/tests/e2e/PestTestFramework/run_tests.bash new file mode 100644 index 000000000..949873a44 --- /dev/null +++ b/tests/e2e/PestTestFramework/run_tests.bash @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +readonly INFECTION="../../../bin/infection --test-framework=pest" + +set -e pipefail + +if [ "$DRIVER" = "phpdbg" ] +then + phpdbg -qrr $INFECTION +else + php $INFECTION +fi + +diff -w expected-output.txt infection.log From e64900cc05666bc9623fb63dc454c5a7c33d3b44 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Tue, 4 May 2021 22:18:04 +0300 Subject: [PATCH 6/8] Remove code coverage node ignorer --- src/TestFramework/PhpUnit/Adapter/PestAdapter.php | 12 +----------- .../PhpUnit/Adapter/PestAdapterTest.php | 12 ------------ 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/TestFramework/PhpUnit/Adapter/PestAdapter.php b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php index a27b43b13..3fb1dc642 100644 --- a/src/TestFramework/PhpUnit/Adapter/PestAdapter.php +++ b/src/TestFramework/PhpUnit/Adapter/PestAdapter.php @@ -37,8 +37,6 @@ use Infection\AbstractTestFramework\MemoryUsageAware; use Infection\AbstractTestFramework\TestFrameworkAdapter; -use Infection\PhpParser\Visitor\IgnoreNode\PhpUnitCodeCoverageAnnotationIgnorer; -use Infection\TestFramework\IgnoresAdditionalNodes; use Infection\TestFramework\ProvidesInitialRunOnlyOptions; use function Safe\preg_match; use function Safe\sprintf; @@ -46,7 +44,7 @@ /** * @internal */ -final class PestAdapter implements IgnoresAdditionalNodes, MemoryUsageAware, ProvidesInitialRunOnlyOptions, TestFrameworkAdapter +final class PestAdapter implements MemoryUsageAware, ProvidesInitialRunOnlyOptions, TestFrameworkAdapter { private const NAME = 'Pest'; @@ -114,14 +112,6 @@ public function getMemoryUsed(string $output): float return $this->phpUnitAdapter->getMemoryUsed($output); } - /** - * @return PhpUnitCodeCoverageAnnotationIgnorer[] - */ - public function getNodeIgnorers(): array - { - return $this->phpUnitAdapter->getNodeIgnorers(); - } - /** * @return string[] */ diff --git a/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php index 38787733b..b46876f9a 100644 --- a/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php +++ b/tests/phpunit/TestFramework/PhpUnit/Adapter/PestAdapterTest.php @@ -35,10 +35,8 @@ namespace Infection\Tests\TestFramework\PhpUnit\Adapter; -use function array_map; use const DIRECTORY_SEPARATOR; use Infection\Config\ValueProvider\PCOVDirectoryProvider; -use Infection\PhpParser\Visitor\IgnoreNode\PhpUnitCodeCoverageAnnotationIgnorer; use Infection\TestFramework\CommandLineArgumentsAndOptionsBuilder; use Infection\TestFramework\CommandLineBuilder; use Infection\TestFramework\PhpUnit\Adapter\PestAdapter; @@ -124,16 +122,6 @@ public function test_it_provides_initial_run_only_options(): void ); } - public function test_it_provides_node_ignorers(): void - { - $nodeIgnorers = array_map('get_class', $this->adapter->getNodeIgnorers()); - - $this->assertSame( - [PhpUnitCodeCoverageAnnotationIgnorer::class], - $nodeIgnorers - ); - } - /** * @group integration */ From 2057d6a676875f55abc17e1d6b9e6269ae3026ac Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Wed, 12 May 2021 22:37:55 +0300 Subject: [PATCH 7/8] Use `master` branch of Pest instead of fork --- tests/e2e/PestTestFramework/composer.json | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/e2e/PestTestFramework/composer.json b/tests/e2e/PestTestFramework/composer.json index 47abc58b7..b76e6c4d2 100644 --- a/tests/e2e/PestTestFramework/composer.json +++ b/tests/e2e/PestTestFramework/composer.json @@ -1,6 +1,6 @@ { "require-dev": { - "pestphp/pest": "dev-feat-junit" + "pestphp/pest": "master" }, "autoload": { "psr-4": { @@ -12,10 +12,6 @@ "PestTestFramework\\Test\\": "tests/" } }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/olivernybroe/pest-1" - } - ] + "minimum-stability": "dev", + "prefer-stable": true } From 7f206d91263e31abbf9ad239284b4e4809759eb8 Mon Sep 17 00:00:00 2001 From: maks-rafalko Date: Thu, 13 May 2021 09:47:27 +0300 Subject: [PATCH 8/8] Use 1.2.0 release of Pest --- tests/e2e/PestTestFramework/composer.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/e2e/PestTestFramework/composer.json b/tests/e2e/PestTestFramework/composer.json index b76e6c4d2..c6aa92a55 100644 --- a/tests/e2e/PestTestFramework/composer.json +++ b/tests/e2e/PestTestFramework/composer.json @@ -1,6 +1,6 @@ { "require-dev": { - "pestphp/pest": "master" + "pestphp/pest": "^1.2.0" }, "autoload": { "psr-4": { @@ -11,7 +11,5 @@ "psr-4": { "PestTestFramework\\Test\\": "tests/" } - }, - "minimum-stability": "dev", - "prefer-stable": true + } }