Completed
Push — develop ( 4dbcf6...912f28 )
by Alejandro
17s queued 11s
created

RedirectResponseHelperTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Util;
6
7
use Laminas\Diactoros\Response\RedirectResponse;
8
use PHPUnit\Framework\TestCase;
9
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
10
use Shlinkio\Shlink\Core\Util\RedirectResponseHelper;
11
12
class RedirectResponseHelperTest extends TestCase
13
{
14
    private RedirectResponseHelper $helper;
15
    private UrlShortenerOptions $shortenerOpts;
16
17
    protected function setUp(): void
18
    {
19
        $this->shortenerOpts = new UrlShortenerOptions();
20
        $this->helper = new RedirectResponseHelper($this->shortenerOpts);
21
    }
22
23
    /**
24
     * @test
25
     * @dataProvider provideRedirectConfigs
26
     */
27
    public function expectedStatusCodeAndCacheIsReturnedBasedOnConfig(
28
        int $configuredStatus,
29
        int $configuredLifetime,
30
        int $expectedStatus,
31
        ?string $expectedCacheControl
32
    ): void {
33
        $this->shortenerOpts->redirectStatusCode = $configuredStatus;
0 ignored issues
show
Bug Best Practice introduced by
The property $redirectStatusCode is declared private in Shlinkio\Shlink\Core\Options\UrlShortenerOptions. Since you implement __set, consider adding a @property or @property-write.
Loading history...
34
        $this->shortenerOpts->redirectCacheLifetime = $configuredLifetime;
0 ignored issues
show
Bug Best Practice introduced by
The property $redirectCacheLifetime is declared private in Shlinkio\Shlink\Core\Options\UrlShortenerOptions. Since you implement __set, consider adding a @property or @property-write.
Loading history...
35
36
        $response = $this->helper->buildRedirectResponse('destination');
37
38
        self::assertInstanceOf(RedirectResponse::class, $response);
39
        self::assertEquals($expectedStatus, $response->getStatusCode());
40
        self::assertTrue($response->hasHeader('Location'));
41
        self::assertEquals('destination', $response->getHeaderLine('Location'));
42
        self::assertEquals($expectedCacheControl !== null, $response->hasHeader('Cache-Control'));
43
        self::assertEquals($expectedCacheControl ?? '', $response->getHeaderLine('Cache-Control'));
44
    }
45
46
    public function provideRedirectConfigs(): iterable
47
    {
48
        yield 'status 302' => [302, 20, 302, null];
49
        yield 'status over 302' => [400, 20, 302, null];
50
        yield 'status below 301' => [201, 20, 302, null];
51
        yield 'status 301 with valid expiration' => [301, 20, 301, 'private,max-age=20'];
52
        yield 'status 301 with zero expiration' => [301, 0, 301, 'private,max-age=30'];
53
        yield 'status 301 with negative expiration' => [301, -20, 301, 'private,max-age=30'];
54
    }
55
}
56