Completed
Push — develop ( 0686ac...7ecc3a )
by Alejandro
18s queued 11s
created

ShortUrlTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 3
b 0
f 0
dl 0
loc 67
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A regenerateShortCodeProperlyChangesTheValueOnValidShortUrls() 0 8 1
A provideValidShortUrls() 0 5 1
A regenerateShortCodeThrowsExceptionIfStateIsInvalid() 0 8 1
A provideInvalidShortUrls() 0 9 1
A shortCodesHaveExpectedLength() 0 7 1
A provideLengths() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Entity;
6
7
use Cake\Chronos\Chronos;
8
use PHPUnit\Framework\TestCase;
9
use Shlinkio\Shlink\Core\Entity\ShortUrl;
10
use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException;
11
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
12
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
13
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
14
15
use function Functional\map;
16
use function range;
17
use function strlen;
18
19
use const Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH;
0 ignored issues
show
Bug introduced by
The constant Shlinkio\Shlink\Core\DEFAULT_SHORT_CODES_LENGTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
21
class ShortUrlTest extends TestCase
22
{
23
    /**
24
     * @test
25
     * @dataProvider provideInvalidShortUrls
26
     */
27
    public function regenerateShortCodeThrowsExceptionIfStateIsInvalid(
28
        ShortUrl $shortUrl,
29
        string $expectedMessage
30
    ): void {
31
        $this->expectException(ShortCodeCannotBeRegeneratedException::class);
32
        $this->expectExceptionMessage($expectedMessage);
33
34
        $shortUrl->regenerateShortCode();
35
    }
36
37
    public function provideInvalidShortUrls(): iterable
38
    {
39
        yield 'with custom slug' => [
40
            new ShortUrl('', ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug'])),
41
            'The short code cannot be regenerated on ShortUrls where a custom slug was provided.',
42
        ];
43
        yield 'already persisted' => [
44
            (new ShortUrl(''))->setId('1'),
45
            'The short code can be regenerated only on new ShortUrls which have not been persisted yet.',
46
        ];
47
    }
48
49
    /**
50
     * @test
51
     * @dataProvider provideValidShortUrls
52
     */
53
    public function regenerateShortCodeProperlyChangesTheValueOnValidShortUrls(ShortUrl $shortUrl): void
54
    {
55
        $firstShortCode = $shortUrl->getShortCode();
56
57
        $shortUrl->regenerateShortCode();
58
        $secondShortCode = $shortUrl->getShortCode();
59
60
        self::assertNotEquals($firstShortCode, $secondShortCode);
61
    }
62
63
    public function provideValidShortUrls(): iterable
64
    {
65
        yield 'no custom slug' => [new ShortUrl('')];
66
        yield 'imported with custom slug' => [
67
            ShortUrl::fromImport(new ImportedShlinkUrl('', '', [], Chronos::now(), null, 'custom-slug'), true),
68
        ];
69
    }
70
71
    /**
72
     * @test
73
     * @dataProvider provideLengths
74
     */
75
    public function shortCodesHaveExpectedLength(?int $length, int $expectedLength): void
76
    {
77
        $shortUrl = new ShortUrl('', ShortUrlMeta::fromRawData(
78
            [ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $length],
79
        ));
80
81
        self::assertEquals($expectedLength, strlen($shortUrl->getShortCode()));
82
    }
83
84
    public function provideLengths(): iterable
85
    {
86
        yield [null, DEFAULT_SHORT_CODES_LENGTH];
87
        yield from map(range(4, 10), fn (int $value) => [$value, $value]);
88
    }
89
}
90