Completed
Pull Request — master (#10)
by Alejandro
07:33
created

ResponseUtilsTraitTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
dl 0
loc 46
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertExpectedResponseForMethod() 0 10 1
A expectedBinaryResponsesAreGenerated() 0 7 1
A binaryResponsesIncludeExtraHeaders() 0 10 1
A provideFiles() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\Response;
5
6
use PHPUnit\Framework\TestCase;
7
use Shlinkio\Shlink\Common\Response\ResponseUtilsTrait;
8
9
class ResponseUtilsTraitTest extends TestCase
10
{
11
    use ResponseUtilsTrait;
12
13
    /**
14
     * @test
15
     * @dataProvider provideFiles
16
     */
17
    public function expectedBinaryResponsesAreGenerated(
18
        string $expectedType,
19
        string $expectedLength,
20
        string $path
21
    ): void {
22
        $this->assertExpectedResponseForMethod('generateBinaryResponse', $expectedType, $expectedLength, $path);
23
        $this->assertExpectedResponseForMethod('generateImageResponse', $expectedType, $expectedLength, $path);
24
    }
25
26
    public function provideFiles(): iterable
27
    {
28
        yield ['image/png', '2433', __DIR__ . '/../../test-resources/shlink-logo.png'];
29
        yield ['text/plain', '20', __DIR__ . '/../../test-resources/text-file.txt'];
30
    }
31
32
    private function assertExpectedResponseForMethod(
33
        string $method,
34
        string $expectedType,
35
        string $expectedLength,
36
        string $path
37
    ): void {
38
        $resp = $this->{$method}($path);
39
40
        $this->assertStringContainsString($expectedType, $resp->getHeaderLine('Content-Type'));
41
        $this->assertStringContainsString($expectedLength, $resp->getHeaderLine('Content-Length'));
42
    }
43
44
    /** @test */
45
    public function binaryResponsesIncludeExtraHeaders(): void
46
    {
47
        $resp = $this->generateBinaryResponse(__DIR__ . '/../../test-resources/shlink-logo.png', [
48
            'foo' => 'bar',
49
            'baz' => 'foo',
50
        ]);
51
        $headers = $resp->getHeaders();
52
53
        $this->assertArrayHasKey('foo', $headers);
54
        $this->assertArrayHasKey('baz', $headers);
55
    }
56
}
57