From abf6e554759d2df9d721b4a04145a108b77b27b6 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 18 Jul 2023 18:07:25 +0200 Subject: [PATCH] Upgraded for PHPUnit 10. --- .gitignore | 3 ++ composer.json | 6 ++-- phpunit.xml.dist | 32 ++++++++--------- src/PhpunitMerger/Command/CoverageCommand.php | 13 +++---- src/PhpunitMerger/Command/LogCommand.php | 4 +++ .../Command/AbstractCommandTest.php | 4 +-- .../Command/Coverage/CoverageCommandTest.php | 35 +++++++++---------- .../Command/Log/LogCommandTest.php | 4 +-- 8 files changed, 51 insertions(+), 50 deletions(-) diff --git a/.gitignore b/.gitignore index 55ad836..0d9d846 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /.Build /.idea +/.Log +/.phpunit.cache +/.phpunit.result.cache composer.lock diff --git a/composer.json b/composer.json index bf86f25..3b248e9 100644 --- a/composer.json +++ b/composer.json @@ -26,16 +26,16 @@ } ], "require": { - "php": "^7.0.8 || ^8.0", + "php": "^8.1", "ext-dom": "*", "ext-json": "*", "ext-simplexml": "*", - "phpunit/php-code-coverage": ">=5.0 <10.0", + "phpunit/php-code-coverage": "^10", "symfony/console": ">=2.7 <6.0", "symfony/finder": ">=2.7 <6.0" }, "require-dev": { - "phpunit/phpunit": ">=6.0 <10.0", + "phpunit/phpunit": "^10", "symfony/filesystem": ">=2.7 <6.0" }, "suggest": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 066b3e7..84b3155 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,17 @@ - - - - tests/PhpunitMerger/Command/Coverage - - - tests/PhpunitMerger/Command/Log - - + + + + tests/PhpunitMerger/Command/Coverage + + + tests/PhpunitMerger/Command/Log + + + + + src + + diff --git a/src/PhpunitMerger/Command/CoverageCommand.php b/src/PhpunitMerger/Command/CoverageCommand.php index c69aab8..11adebc 100644 --- a/src/PhpunitMerger/Command/CoverageCommand.php +++ b/src/PhpunitMerger/Command/CoverageCommand.php @@ -5,11 +5,12 @@ namespace Nimut\PhpunitMerger\Command; use SebastianBergmann\CodeCoverage\CodeCoverage; -use SebastianBergmann\CodeCoverage\Driver\Driver; +use SebastianBergmann\CodeCoverage\Driver\Selector; use SebastianBergmann\CodeCoverage\Filter as CodeCoverageFilter; use SebastianBergmann\CodeCoverage\Report\Clover; use SebastianBergmann\CodeCoverage\Report\Cobertura; use SebastianBergmann\CodeCoverage\Report\Html\Facade; +use SebastianBergmann\CodeCoverage\Report\Thresholds; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -94,12 +95,8 @@ protected function execute(InputInterface $input, OutputInterface $output) private function getCodeCoverage(OutputInterface $output, $coverageCache = null) { - $driver = null; - $filter = null; - if (method_exists(Driver::class, 'forLineCoverage')) { - $filter = new CodeCoverageFilter(); - $driver = Driver::forLineCoverage($filter); - } + $filter = new CodeCoverageFilter(); + $driver = (new Selector())->forLineCoverage($filter); $codeCoverage = new CodeCoverage($driver, $filter); @@ -131,7 +128,7 @@ private function writeCodeCoverage(CodeCoverage $codeCoverage, OutputInterface $ private function writeHtmlReport(CodeCoverage $codeCoverage, string $destination, int $lowUpperBound, int $highLowerBound) { - $writer = new Facade($lowUpperBound, $highLowerBound); + $writer = new Facade(thresholds: Thresholds::from($lowUpperBound, $highLowerBound)); $writer->process($codeCoverage, $destination); } } diff --git a/src/PhpunitMerger/Command/LogCommand.php b/src/PhpunitMerger/Command/LogCommand.php index e1fd152..b114721 100644 --- a/src/PhpunitMerger/Command/LogCommand.php +++ b/src/PhpunitMerger/Command/LogCommand.php @@ -60,6 +60,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } } catch (\Exception $exception) { // Initial fallthrough + // At the time of the initial test execution, the log file of the test is empty + // and therefore file_get_contents returns "" which leads to the ignored error. + // .travis.yml uses the app itself later to merge the junit-logs again and + // creates a new log.xml } } diff --git a/tests/PhpunitMerger/Command/AbstractCommandTest.php b/tests/PhpunitMerger/Command/AbstractCommandTest.php index 68d55a4..14b3556 100644 --- a/tests/PhpunitMerger/Command/AbstractCommandTest.php +++ b/tests/PhpunitMerger/Command/AbstractCommandTest.php @@ -24,7 +24,7 @@ public function assertOutputFileNotExists() $filesystem = new Filesystem(); $filesystem->remove($this->logDirectory . $this->outputFile); - $this->assertFileNotExists($this->logDirectory . $this->outputFile); + $this->assertFileDoesNotExist($this->logDirectory . $this->outputFile); } public function assertOutputDirectoryNotExists() @@ -32,6 +32,6 @@ public function assertOutputDirectoryNotExists() $filesystem = new Filesystem(); $filesystem->remove($this->logDirectory . dirname($this->outputFile)); - $this->assertDirectoryNotExists($this->logDirectory . dirname($this->outputFile)); + $this->assertDirectoryDoesNotExist($this->logDirectory . dirname($this->outputFile)); } } diff --git a/tests/PhpunitMerger/Command/Coverage/CoverageCommandTest.php b/tests/PhpunitMerger/Command/Coverage/CoverageCommandTest.php index 7f0ce02..6fec010 100644 --- a/tests/PhpunitMerger/Command/Coverage/CoverageCommandTest.php +++ b/tests/PhpunitMerger/Command/Coverage/CoverageCommandTest.php @@ -6,7 +6,6 @@ use Nimut\PhpunitMerger\Command\CoverageCommand; use Nimut\PhpunitMerger\Tests\Command\AbstractCommandTest; -use Prophecy\Argument; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\OutputInterface; @@ -28,11 +27,11 @@ public function testCoverageWritesOutputFile() $this->logDirectory . $this->outputFile, ] ); - $output = $this->prophesize(OutputInterface::class); - $output->write(Argument::any())->shouldNotBeCalled(); + $output = $this->createMock(OutputInterface::class); + $output->expects(self::never())->method('write'); $command = new CoverageCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); $this->assertFileExists($this->logDirectory . $this->outputFile); } @@ -47,11 +46,11 @@ public function testCoverageWritesStandardOutput() $this->logDirectory . 'coverage/', ] ); - $output = $this->prophesize(OutputInterface::class); - $output->write(Argument::type('string'))->shouldBeCalled(); + $output = $this->createMock(OutputInterface::class); + $output->expects(self::once())->method('write')->with(self::anything()); $command = new CoverageCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); } public function testCoverageWritesHtmlReport() @@ -66,11 +65,11 @@ public function testCoverageWritesHtmlReport() '--html=' . $this->logDirectory . dirname($this->outputFile), ] ); - $output = $this->prophesize(OutputInterface::class); - $output->write(Argument::type('string'))->shouldBeCalled(); + $output = $this->createMock(OutputInterface::class); + $output->expects(self::once())->method('write')->with(self::anything()); $command = new CoverageCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); $this->assertFileExists($this->logDirectory . $this->outputFile); } @@ -89,11 +88,11 @@ public function testCoverageWritesHtmlReportWithCustomBounds() '--highLowerBound=70', ] ); - $output = $this->prophesize(OutputInterface::class); - $output->write(Argument::type('string'))->shouldBeCalled(); + $output = $this->createMock(OutputInterface::class); + $output->expects(self::once())->method('write')->with(self::anything()); $command = new CoverageCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); $this->assertFileExists($this->logDirectory . $this->outputFile); @@ -103,8 +102,8 @@ public function testCoverageWritesHtmlReportWithCustomBounds() $this->assertStringContainsString('High: 70% to 100%', $content); } else { // Fallback for phpunit < 7.0 - $this->assertContains('Low: 0% to 20%', $content); - $this->assertContains('High: 70% to 100%', $content); + $this->assertStringContainsString('Low: 0% to 20%', $content); + $this->assertStringContainsString('High: 70% to 100%', $content); } } @@ -122,11 +121,11 @@ public function testCoverageWritesOutputFileAndHtmlReport() $this->logDirectory . $this->outputFile, ] ); - $output = $this->prophesize(OutputInterface::class); - $output->write(Argument::any())->shouldNotBeCalled(); + $output = $this->createMock(OutputInterface::class); + $output->expects(self::never())->method('write'); $command = new CoverageCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); $this->assertFileExists($this->logDirectory . $this->outputFile); $this->assertFileExists($this->logDirectory . dirname($this->outputFile) . '/index.html'); diff --git a/tests/PhpunitMerger/Command/Log/LogCommandTest.php b/tests/PhpunitMerger/Command/Log/LogCommandTest.php index 3ebd640..4206e7d 100644 --- a/tests/PhpunitMerger/Command/Log/LogCommandTest.php +++ b/tests/PhpunitMerger/Command/Log/LogCommandTest.php @@ -27,10 +27,10 @@ public function testRunMergesCoverage() $this->logDirectory . $this->outputFile, ] ); - $output = $this->prophesize(OutputInterface::class); + $output = $this->createMock(OutputInterface::class); $command = new LogCommand(); - $command->run($input, $output->reveal()); + $command->run($input, $output); $this->assertFileExists($this->logDirectory . $this->outputFile); }