Skip to content

Commit

Permalink
Move away from deprecated MockObject#withConsecutive()
Browse files Browse the repository at this point in the history
More info: sebastianbergmann/phpunit#5063

Signed-off-by: Luís Cobucci <lcobucci@gmail.com>
  • Loading branch information
lcobucci committed Mar 2, 2023
1 parent 3013c0f commit 29ddca2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 136 deletions.
146 changes: 18 additions & 128 deletions test/unit/Command/AssertBackwardsCompatibleTest.php
Expand Up @@ -4,6 +4,7 @@

namespace RoaveTest\BackwardCompatibility\Command;

use LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psl\Env;
Expand All @@ -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;
Expand All @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -365,10 +276,6 @@ public function testExecuteWithDefaultRevisionsNotProvidedAndNoDetectedTags(): v
['sources-path', 'src'],
]);

$this
->performCheckout
->expects(self::never())
->method('checkout');
$this
->parseRevision
->expects(self::never())
Expand All @@ -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 */
Expand All @@ -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')
Expand Down Expand Up @@ -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[][] */
Expand Down
54 changes: 54 additions & 0 deletions test/unit/Command/PerformCheckoutOfRevisionForTests.php
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace RoaveTest\BackwardCompatibility\Command;

use Roave\BackwardCompatibility\Git\CheckedOutRepository;
use Roave\BackwardCompatibility\Git\PerformCheckoutOfRevision;
use Roave\BackwardCompatibility\Git\Revision;
use SplObjectStorage;

final class PerformCheckoutOfRevisionForTests implements PerformCheckoutOfRevision
{
/** @var SplObjectStorage<CheckedOutRepository, int> */
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;
}
}
18 changes: 10 additions & 8 deletions test/unit/Formatter/SymfonyConsoleTextFormatterTest.php
Expand Up @@ -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
Expand All @@ -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(),
);
}
}

0 comments on commit 29ddca2

Please sign in to comment.