Skip to content

Commit eb6e4d6

Browse files
Copilotgarak
andcommitted
fix: correct mock setups in tests and improve SnippetGeneratorTest
Co-authored-by: garak <179866+garak@users.noreply.github.com>
1 parent a6d1955 commit eb6e4d6

File tree

3 files changed

+12
-30
lines changed

3 files changed

+12
-30
lines changed

tests/Badge/Model/UseCase/CreateCircleCiBadgeTest.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,15 @@ public static function shouldCreateCircleCiBadgeProvider(): array
8888

8989
public function testShouldCreateDefaultBadgeOnError(): void
9090
{
91-
$package = $this->createMockWithoutInvokingTheOriginalConstructor(
92-
Package::class,
93-
['hasStableVersion', 'getLatestStableVersion', 'getOriginalObject']
94-
);
91+
$package = $this->createMockWithoutInvokingTheOriginalConstructor(Package::class);
9592

9693
$this->repository
9794
->method('fetchByRepository')
9895
->willReturn($package);
9996

100-
$repo = $this->createMockWithoutInvokingTheOriginalConstructor(
101-
\Packagist\Api\Result\Package::class,
102-
['getRepository']
103-
);
104-
$repo->expects($this->once())
105-
->method('getRepository')
106-
->will($this->throwException(new \RuntimeException()));
107-
10897
$package->expects($this->once())
109-
->method('getOriginalObject')
110-
->willReturn($repo);
98+
->method('getRepository')
99+
->willThrowException(new \RuntimeException());
111100

112101
$repository = 'PUGX/badge-poser';
113102
$badge = $this->useCase->createCircleCiBadge($repository);

tests/Badge/Model/UseCase/CreateComposerLockBadgeTest.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use App\Badge\Service\ClientStrategy;
1818
use App\Badge\ValueObject\Repository;
1919
use GuzzleHttp\ClientInterface;
20-
use Packagist\Api\Result\Package;
2120
use PHPUnit\Framework\Attributes\DataProvider;
2221
use PHPUnit\Framework\MockObject\MockObject;
2322
use PHPUnit\Framework\TestCase;
@@ -55,15 +54,10 @@ public function testShouldCreateComposerLockBadge(int $returnCode, string $expec
5554
->method('fetchByRepository')
5655
->willReturn($package);
5756

58-
$repo = $this->createMock(Package::class);
59-
$repo->expects($this->once())
57+
$package->expects($this->once())
6058
->method('getRepository')
6159
->willReturn('https://github.com/user/repository');
6260

63-
$package->expects($this->once())
64-
->method('getOriginalObject')
65-
->willReturn($repo);
66-
6761
$package->expects($this->once())
6862
->method('getDefaultBranch')
6963
->willReturn('master');
@@ -90,14 +84,9 @@ public function testShouldCreateDefaultBadgeOnError(): void
9084
->method('fetchByRepository')
9185
->willReturn($package);
9286

93-
$repo = $this->createMock(Package::class);
94-
$repo->expects($this->once())
95-
->method('getRepository')
96-
->will($this->throwException(new \RuntimeException()));
97-
9887
$package->expects($this->once())
99-
->method('getOriginalObject')
100-
->willReturn($repo);
88+
->method('getRepository')
89+
->willThrowException(new \RuntimeException());
10190

10291
$repository = 'PUGX/badge-poser';
10392
$badge = $this->useCase->createComposerLockBadge($repository);

tests/Service/SnippetGeneratorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function testGenerateAllSnippetsWithAndWithoutFeaturedBadges(): void
169169
->willReturn($routeCollection);
170170
$router
171171
->method('generate')
172-
->willReturnCallback(function (string $name, array $params = []) {
172+
->willReturnCallback(function (string $name, array $params = [], int $referenceType = RouterInterface::ABSOLUTE_URL) {
173173
if ('pugx_badge_packagist' === $name) {
174174
return 'repo_url';
175175
}
@@ -206,7 +206,11 @@ public function testGenerateAllSnippetsWithAndWithoutFeaturedBadges(): void
206206
'pugx_badge_circleci' => 'img_url12',
207207
];
208208

209-
return $map[$name] ?? 'repo_url';
209+
if (!\array_key_exists($name, $map)) {
210+
throw new \InvalidArgumentException(\sprintf('Unexpected route name "%s" in SnippetGeneratorTest router mock.', $name));
211+
}
212+
213+
return $map[$name];
210214
});
211215

212216
$poser = $this->getMockBuilder(Poser::class)

0 commit comments

Comments
 (0)