From 29ddca22bde05c6086ef3bbee53966a6599463f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Thu, 2 Mar 2023 01:14:23 +0100 Subject: [PATCH] Move away from deprecated MockObject#withConsecutive() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit More info: https://github.com/sebastianbergmann/phpunit/issues/5063 Signed-off-by: Luís Cobucci --- .../Command/AssertBackwardsCompatibleTest.php | 146 +++--------------- .../PerformCheckoutOfRevisionForTests.php | 54 +++++++ .../SymfonyConsoleTextFormatterTest.php | 18 ++- 3 files changed, 82 insertions(+), 136 deletions(-) create mode 100644 test/unit/Command/PerformCheckoutOfRevisionForTests.php diff --git a/test/unit/Command/AssertBackwardsCompatibleTest.php b/test/unit/Command/AssertBackwardsCompatibleTest.php index 9481c8f7..dc532dde 100644 --- a/test/unit/Command/AssertBackwardsCompatibleTest.php +++ b/test/unit/Command/AssertBackwardsCompatibleTest.php @@ -4,6 +4,7 @@ namespace RoaveTest\BackwardCompatibility\Command; +use LogicException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psl\Env; @@ -20,7 +21,6 @@ use Roave\BackwardCompatibility\Git\CheckedOutRepository; use Roave\BackwardCompatibility\Git\GetVersionCollection; use Roave\BackwardCompatibility\Git\ParseRevision; -use Roave\BackwardCompatibility\Git\PerformCheckoutOfRevision; use Roave\BackwardCompatibility\Git\PickVersionFromVersionCollection; use Roave\BackwardCompatibility\Git\Revision; use Roave\BackwardCompatibility\LocateDependencies\LocateDependencies; @@ -46,8 +46,7 @@ final class AssertBackwardsCompatibleTest extends TestCase private ConsoleOutputInterface $output; /** @var OutputInterface&MockObject */ private OutputInterface $stdErr; - /** @var PerformCheckoutOfRevision&MockObject */ - private PerformCheckoutOfRevision $performCheckout; + private PerformCheckoutOfRevisionForTests $performCheckout; /** @var ParseRevision&MockObject */ private ParseRevision $parseRevision; /** @var GetVersionCollection&MockObject */ @@ -73,7 +72,7 @@ public function setUp(): void $this->input = $this->createMock(InputInterface::class); $this->output = $this->createMock(ConsoleOutputInterface::class); $this->stdErr = $this->createMock(OutputInterface::class); - $this->performCheckout = $this->createMock(PerformCheckoutOfRevision::class); + $this->performCheckout = new PerformCheckoutOfRevisionForTests(); $this->parseRevision = $this->createMock(ParseRevision::class); $this->getVersions = $this->createMock(GetVersionCollection::class); $this->pickVersion = $this->createMock(PickVersionFromVersionCollection::class); @@ -137,32 +136,9 @@ public function testExecuteWhenRevisionsAreProvidedAsOptions(): void ['sources-path', 'src'], ]); - $this->performCheckout->expects(self::exactly(2)) - ->method('checkout') - ->withConsecutive( - [$this->sourceRepository, $fromSha], - [$this->sourceRepository, $toSha], - )->willReturnOnConsecutiveCalls( - $this->sourceRepository, - $this->sourceRepository, - ); - - $this->performCheckout->expects(self::exactly(2)) - ->method('remove') - ->withConsecutive( - [$this->sourceRepository], - [$this->sourceRepository], - ); - $this->parseRevision->expects(self::exactly(2)) ->method('fromStringForRepository') - ->withConsecutive( - [$fromSha], - [$toSha], - )->willReturnOnConsecutiveCalls( - Revision::fromSha1($fromSha), - Revision::fromSha1($toSha), - ); + ->willReturnCallback(Revision::fromSha1(...)); $this ->locateDependencies @@ -173,6 +149,7 @@ public function testExecuteWhenRevisionsAreProvidedAsOptions(): void $this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty()); self::assertSame(0, $this->compare->execute($this->input, $this->output)); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } public function testExecuteWhenDevelopmentDependenciesAreRequested(): void @@ -189,32 +166,9 @@ public function testExecuteWhenDevelopmentDependenciesAreRequested(): void ['sources-path', 'src'], ]); - $this->performCheckout->expects(self::exactly(2)) - ->method('checkout') - ->withConsecutive( - [$this->sourceRepository, $fromSha], - [$this->sourceRepository, $toSha], - )->willReturnOnConsecutiveCalls( - $this->sourceRepository, - $this->sourceRepository, - ); - - $this->performCheckout->expects(self::exactly(2)) - ->method('remove') - ->withConsecutive( - [$this->sourceRepository], - [$this->sourceRepository], - ); - $this->parseRevision->expects(self::exactly(2)) ->method('fromStringForRepository') - ->withConsecutive( - [$fromSha], - [$toSha], - )->willReturnOnConsecutiveCalls( - Revision::fromSha1($fromSha), - Revision::fromSha1($toSha), - ); + ->willReturnCallback(Revision::fromSha1(...)); $this ->locateDependencies @@ -225,6 +179,7 @@ public function testExecuteWhenDevelopmentDependenciesAreRequested(): void $this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty()); self::assertSame(0, $this->compare->execute($this->input, $this->output)); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected(): void @@ -242,32 +197,9 @@ public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected(): void ['sources-path', 'src'], ]); - $this->performCheckout->expects(self::exactly(2)) - ->method('checkout') - ->withConsecutive( - [$this->sourceRepository, $fromSha], - [$this->sourceRepository, $toSha], - )->willReturnOnConsecutiveCalls( - $this->sourceRepository, - $this->sourceRepository, - ); - - $this->performCheckout->expects(self::exactly(2)) - ->method('remove') - ->withConsecutive( - [$this->sourceRepository], - [$this->sourceRepository], - ); - $this->parseRevision->expects(self::exactly(2)) ->method('fromStringForRepository') - ->withConsecutive( - [$fromSha], - [$toSha], - )->willReturnOnConsecutiveCalls( - Revision::fromSha1($fromSha), - Revision::fromSha1($toSha), - ); + ->willReturnCallback(Revision::fromSha1(...)); $this ->locateDependencies @@ -290,6 +222,7 @@ public function testExecuteReturnsNonZeroExitCodeWhenChangesAreDetected(): void )); self::assertSame(3, $this->compare->execute($this->input, $this->output)); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } public function testProvidingMarkdownOptionWritesMarkdownOutput(): void @@ -307,32 +240,9 @@ public function testProvidingMarkdownOptionWritesMarkdownOutput(): void ['sources-path', 'src'], ]); - $this->performCheckout->expects(self::exactly(2)) - ->method('checkout') - ->withConsecutive( - [$this->sourceRepository, $fromSha], - [$this->sourceRepository, $toSha], - )->willReturnOnConsecutiveCalls( - $this->sourceRepository, - $this->sourceRepository, - ); - - $this->performCheckout->expects(self::exactly(2)) - ->method('remove') - ->withConsecutive( - [$this->sourceRepository], - [$this->sourceRepository], - ); - $this->parseRevision->expects(self::exactly(2)) ->method('fromStringForRepository') - ->withConsecutive( - [$fromSha], - [$toSha], - )->willReturnOnConsecutiveCalls( - Revision::fromSha1($fromSha), - Revision::fromSha1($toSha), - ); + ->willReturnCallback(Revision::fromSha1(...)); $this ->locateDependencies @@ -353,6 +263,7 @@ public function testProvidingMarkdownOptionWritesMarkdownOutput(): void }); $this->compare->execute($this->input, $this->output); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags(): void @@ -365,10 +276,6 @@ public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags(): v ['sources-path', 'src'], ]); - $this - ->performCheckout - ->expects(self::never()) - ->method('checkout'); $this ->parseRevision ->expects(self::never()) @@ -391,6 +298,7 @@ public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags(): v $this->expectException(InvariantViolationException::class); $this->compare->execute($this->input, $this->output); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } /** @dataProvider validVersionCollections */ @@ -409,32 +317,13 @@ public function testExecuteWithDefaultRevisionsNotProvided(VersionCollection $ve ['sources-path', 'src'], ]); - $this->performCheckout->expects(self::exactly(2)) - ->method('checkout') - ->withConsecutive( - [$this->sourceRepository, $fromSha], - [$this->sourceRepository, $toSha], - )->willReturnOnConsecutiveCalls( - $this->sourceRepository, - $this->sourceRepository, - ); - - $this->performCheckout->expects(self::exactly(2)) - ->method('remove') - ->withConsecutive( - [$this->sourceRepository], - [$this->sourceRepository], - ); - $this->parseRevision->expects(self::exactly(2)) ->method('fromStringForRepository') - ->withConsecutive( - [(string) $pickedVersion], - ['HEAD'], - )->willReturnOnConsecutiveCalls( - Revision::fromSha1($fromSha), - Revision::fromSha1($toSha), - ); + ->willReturnCallback(static fn (string $input): Revision => match ($input) { + (string) $pickedVersion => Revision::fromSha1($fromSha), + 'HEAD' => Revision::fromSha1($toSha), + default => throw new LogicException(), + }); $this->getVersions->expects(self::once()) ->method('fromRepository') @@ -462,6 +351,7 @@ public function testExecuteWithDefaultRevisionsNotProvided(VersionCollection $ve $this->compareApi->expects(self::once())->method('__invoke')->willReturn(Changes::empty()); self::assertSame(0, $this->compare->execute($this->input, $this->output)); + self::assertSame(0, $this->performCheckout->nonRemovedRepositoryCount()); } /** @return VersionCollection[][] */ diff --git a/test/unit/Command/PerformCheckoutOfRevisionForTests.php b/test/unit/Command/PerformCheckoutOfRevisionForTests.php new file mode 100644 index 00000000..25c74067 --- /dev/null +++ b/test/unit/Command/PerformCheckoutOfRevisionForTests.php @@ -0,0 +1,54 @@ + */ + private SplObjectStorage $checkedOutRepositories; + + public function __construct() + { + $this->checkedOutRepositories = new SplObjectStorage(); + } + + public function checkout(CheckedOutRepository $sourceRepository, Revision $revision): CheckedOutRepository + { + if (! isset($this->checkedOutRepositories[$sourceRepository])) { + $this->checkedOutRepositories[$sourceRepository] = 0; + } + + $this->checkedOutRepositories[$sourceRepository] += 1; + + return $sourceRepository; + } + + public function remove(CheckedOutRepository $checkedOutRepository): void + { + $this->checkedOutRepositories[$checkedOutRepository] -= 1; + + if ($this->checkedOutRepositories[$checkedOutRepository] !== 0) { + return; + } + + unset($this->checkedOutRepositories[$checkedOutRepository]); + } + + public function nonRemovedRepositoryCount(): int + { + $sum = 0; + + foreach ($this->checkedOutRepositories as $repository) { + $sum += $this->checkedOutRepositories[$repository]; + } + + return $sum; + } +} diff --git a/test/unit/Formatter/SymfonyConsoleTextFormatterTest.php b/test/unit/Formatter/SymfonyConsoleTextFormatterTest.php index 2433ce3d..55f410a4 100644 --- a/test/unit/Formatter/SymfonyConsoleTextFormatterTest.php +++ b/test/unit/Formatter/SymfonyConsoleTextFormatterTest.php @@ -11,7 +11,9 @@ use Roave\BackwardCompatibility\Change; use Roave\BackwardCompatibility\Changes; use Roave\BackwardCompatibility\Formatter\SymfonyConsoleTextFormatter; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\BufferedOutput; + +use const PHP_EOL; /** @covers \Roave\BackwardCompatibility\Formatter\SymfonyConsoleTextFormatter */ final class SymfonyConsoleTextFormatterTest extends TestCase @@ -22,17 +24,17 @@ public function testWrite(): void $change1Text = SecureRandom\string(8); $change2Text = SecureRandom\string(8); - $output = $this->createMock(OutputInterface::class); - $output->expects(self::exactly(2)) - ->method('writeln') - ->withConsecutive( - [Str\format('[BC] REMOVED: %s', $change1Text)], - [Str\format(' ADDED: %s', $change2Text)], - ); + $output = new BufferedOutput(); (new SymfonyConsoleTextFormatter($output))->write(Changes::fromList( Change::removed($change1Text, true), Change::added($change2Text, false), )); + + self::assertSame( + Str\format('[BC] REMOVED: %s', $change1Text) . PHP_EOL . + Str\format(' ADDED: %s', $change2Text) . PHP_EOL, + $output->fetch(), + ); } }