Skip to content

Commit cfab13b

Browse files
authored
Merge pull request #2533 from acelaya-forks/improve-coverage-2
Add more code coverage improvements
2 parents 039a58b + 9432a5b commit cfab13b

File tree

11 files changed

+149
-23
lines changed

11 files changed

+149
-23
lines changed

module/CLI/test/ApiKey/RoleResolverTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ public static function provideRoles(): iterable
9191
[RoleDefinition::forAuthoredShortUrls()],
9292
0,
9393
];
94-
yield 'both roles' => [
95-
$buildInput(
96-
[Role::DOMAIN_SPECIFIC->paramName() => 'example.com', Role::AUTHORED_SHORT_URLS->paramName() => true],
97-
),
98-
[RoleDefinition::forAuthoredShortUrls(), RoleDefinition::forDomain($domain)],
94+
yield 'all roles' => [
95+
$buildInput([
96+
Role::DOMAIN_SPECIFIC->paramName() => 'example.com',
97+
Role::AUTHORED_SHORT_URLS->paramName() => true,
98+
Role::NO_ORPHAN_VISITS->paramName() => true,
99+
]),
100+
[
101+
RoleDefinition::forAuthoredShortUrls(),
102+
RoleDefinition::forDomain($domain),
103+
RoleDefinition::forNoOrphanVisits(),
104+
],
99105
1,
100106
];
101107
}

module/CLI/test/Command/ShortUrl/CreateShortUrlCommandTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
6464
self::assertStringNotContainsString('but the real-time updates cannot', $output);
6565
}
6666

67+
#[Test]
68+
public function longUrlIsAskedIfNotProvided(): void
69+
{
70+
$shortUrl = ShortUrl::createFake();
71+
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willReturn(
72+
UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl),
73+
);
74+
$this->stringifier->expects($this->once())->method('stringify')->with($shortUrl)->willReturn(
75+
'stringified_short_url',
76+
);
77+
78+
$this->commandTester->setInputs([$shortUrl->getLongUrl()]);
79+
$this->commandTester->execute([]);
80+
}
81+
6782
#[Test]
6883
public function providingNonUniqueSlugOutputsError(): void
6984
{

module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public function noDateFlagsTriesToListWithoutDateRange(): void
5050
$this->commandTester->execute(['shortCode' => $shortCode]);
5151
}
5252

53+
#[Test]
54+
public function shortCodeIsAskedIfNotProvided(): void
55+
{
56+
$shortCode = 'abc123';
57+
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
58+
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
59+
$this->anything(),
60+
)->willReturn(new Paginator(new ArrayAdapter([])));
61+
62+
$this->commandTester->setInputs([$shortCode]);
63+
$this->commandTester->execute([]);
64+
}
65+
5366
#[Test]
5467
public function providingDateFlagsTheListGetsFiltered(): void
5568
{

module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public function correctShortCodeResolvesUrl(): void
4545
self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
4646
}
4747

48+
#[Test]
49+
public function shortCodeIsAskedIfNotProvided(): void
50+
{
51+
$shortCode = 'abc123';
52+
$shortUrl = ShortUrl::createFake();
53+
$this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
54+
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
55+
)->willReturn($shortUrl);
56+
57+
$this->commandTester->setInputs([$shortCode]);
58+
$this->commandTester->execute([]);
59+
}
60+
4861
#[Test]
4962
public function incorrectShortCodeOutputsErrorMessage(): void
5063
{

module/Core/src/Config/Options/AppOptions.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use Shlinkio\Shlink\Core\Config\EnvVars;
88

9-
use function sprintf;
10-
119
final class AppOptions
1210
{
1311
public function __construct(public string $name = 'Shlink', public string $version = '4.0.0')
@@ -19,9 +17,4 @@ public static function fromEnv(): self
1917
$version = EnvVars::isDevEnv() ? 'latest' : '%SHLINK_VERSION%';
2018
return new self(version: $version);
2119
}
22-
23-
public function __toString(): string
24-
{
25-
return sprintf('%s:v%s', $this->name, $this->version);
26-
}
2720
}

module/Core/src/EventDispatcher/Event/ShortUrlCreated.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public function __construct(public string $shortUrlId)
1515

1616
public function jsonSerialize(): array
1717
{
18-
return [
19-
'shortUrlId' => $this->shortUrlId,
20-
];
18+
return ['shortUrlId' => $this->shortUrlId];
2119
}
2220

2321
public static function fromPayload(array $payload): self

module/Core/test/Config/NotFoundRedirectResolverTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Psr\Http\Message\ServerRequestInterface;
1717
use Psr\Http\Message\UriInterface;
18-
use Psr\Log\NullLogger;
18+
use Psr\Log\LoggerInterface;
1919
use Shlinkio\Shlink\Core\Action\RedirectAction;
2020
use Shlinkio\Shlink\Core\Config\NotFoundRedirectResolver;
2121
use Shlinkio\Shlink\Core\Config\Options\NotFoundRedirectOptions;
@@ -28,11 +28,14 @@ class NotFoundRedirectResolverTest extends TestCase
2828
{
2929
private NotFoundRedirectResolver $resolver;
3030
private MockObject & RedirectResponseHelperInterface $helper;
31+
private MockObject & LoggerInterface $logger;
3132

3233
protected function setUp(): void
3334
{
3435
$this->helper = $this->createMock(RedirectResponseHelperInterface::class);
35-
$this->resolver = new NotFoundRedirectResolver($this->helper, new NullLogger());
36+
$this->logger = $this->createMock(LoggerInterface::class);
37+
38+
$this->resolver = new NotFoundRedirectResolver($this->helper, $this->logger);
3639
}
3740

3841
#[Test, DataProvider('provideRedirects')]
@@ -123,6 +126,22 @@ public function noResponseIsReturnedIfNoConditionsMatch(): void
123126
self::assertNull($result);
124127
}
125128

129+
#[Test]
130+
public function warningMessageIsLoggedIfRedirectUrlIsMalformed(): void
131+
{
132+
$this->logger->expects($this->once())->method('warning')->with(
133+
'It was not possible to parse "{url}" as a valid URL: {e}',
134+
$this->isArray(),
135+
);
136+
137+
$uri = new Uri('/');
138+
$this->resolver->resolveRedirectResponse(
139+
self::notFoundType(ServerRequestFactory::fromGlobals()->withUri($uri)),
140+
new NotFoundRedirectOptions(baseUrlRedirect: 'http:///example.com'),
141+
$uri,
142+
);
143+
}
144+
126145
private static function notFoundType(ServerRequestInterface $req): NotFoundType
127146
{
128147
return NotFoundType::fromRequest($req, '');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Event;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use PHPUnit\Framework\Attributes\TestWith;
9+
use PHPUnit\Framework\TestCase;
10+
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
11+
12+
class ShortUrlCreatedTest extends TestCase
13+
{
14+
#[Test]
15+
public function jsonSerialization(): void
16+
{
17+
$shortUrlId = 'abc123';
18+
self::assertEquals(['shortUrlId' => $shortUrlId], new ShortUrlCreated($shortUrlId)->jsonSerialize());
19+
}
20+
21+
#[Test]
22+
#[TestWith([['shortUrlId' => '123'], '123'])]
23+
#[TestWith([[], ''])]
24+
public function creationFromPayload(array $payload, string $expectedShortUrlId): void
25+
{
26+
$event = ShortUrlCreated::fromPayload($payload);
27+
self::assertEquals($expectedShortUrlId, $event->shortUrlId);
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Event;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use PHPUnit\Framework\Attributes\TestWith;
9+
use PHPUnit\Framework\TestCase;
10+
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
11+
12+
class UrlVisitedTest extends TestCase
13+
{
14+
#[Test]
15+
public function jsonSerialization(): void
16+
{
17+
$visitId = 'abc123';
18+
self::assertEquals(
19+
['visitId' => $visitId, 'originalIpAddress' => null],
20+
new UrlVisited($visitId)->jsonSerialize(),
21+
);
22+
}
23+
24+
#[Test]
25+
#[TestWith([['visitId' => '123', 'originalIpAddress' => '1.2.3.4'], '123', '1.2.3.4'])]
26+
#[TestWith([['visitId' => '123'], '123', null])]
27+
#[TestWith([['originalIpAddress' => '1.2.3.4'], '', '1.2.3.4'])]
28+
#[TestWith([[], '', null])]
29+
public function creationFromPayload(array $payload, string $expectedVisitId, string|null $expectedIpAddress): void
30+
{
31+
$event = UrlVisited::fromPayload($payload);
32+
self::assertEquals($expectedVisitId, $event->visitId);
33+
self::assertEquals($expectedIpAddress, $event->originalIpAddress);
34+
}
35+
}

module/Rest/src/Exception/ApiKeyNotFoundException.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,4 @@ public static function forName(string $name): self
1212
{
1313
return new self(sprintf('API key with name "%s" not found', $name));
1414
}
15-
16-
/** @deprecated */
17-
public static function forKey(string $key): self
18-
{
19-
return new self(sprintf('API key with key "%s" not found', $key));
20-
}
2115
}

0 commit comments

Comments
 (0)