Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

ResponsesTest::testAttachmentException()   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
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Http;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Files\Files;
16
use Spiral\Http\Config\HttpConfig;
17
use Spiral\Http\Exception\ResponseException;
18
use Spiral\Http\ResponseWrapper;
19
use Spiral\Tests\Http\Diactoros\ResponseFactory;
20
use Spiral\Tests\Http\Diactoros\StreamFactory;
21
use Laminas\Diactoros\Stream;
22
23
class ResponsesTest extends TestCase
24
{
25
    public function testRedirect(): void
26
    {
27
        $response = $this->getWrapper()->redirect('google.com');
28
        $this->assertSame('google.com', $response->getHeaderLine('Location'));
29
        $this->assertSame(302, $response->getStatusCode());
30
31
        $response = $this->getWrapper()->redirect('google.com', 301);
32
        $this->assertSame('google.com', $response->getHeaderLine('Location'));
33
        $this->assertSame(301, $response->getStatusCode());
34
    }
35
36
    public function testRedirectException(): void
37
    {
38
        $this->expectException(ResponseException::class);
39
        $this->getWrapper()->redirect(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type Psr\Http\Message\UriInterface|string expected by parameter $uri of Spiral\Http\ResponseWrapper::redirect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $this->getWrapper()->redirect(/** @scrutinizer ignore-type */ true);
Loading history...
40
    }
41
42
    public function testJson(): void
43
    {
44
        $response = $this->getWrapper()->json([
45
            'status'  => 300,
46
            'message' => 'hi'
47
        ]);
48
49
        $this->assertSame('{"status":300,"message":"hi"}', (string)$response->getBody());
50
        $this->assertSame(300, $response->getStatusCode());
51
        $this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
52
    }
53
54
    public function testHtml(): void
55
    {
56
        $response = $this->getWrapper()->html('hello world');
57
        $this->assertSame('hello world', (string)$response->getBody());
58
        $this->assertSame(200, $response->getStatusCode());
59
        $ff = $response->getHeader('Content-Type');
0 ignored issues
show
Unused Code introduced by
The assignment to $ff is dead and can be removed.
Loading history...
60
        $this->assertSame(['text/html; charset=utf-8'], $response->getHeader('Content-Type'));
61
    }
62
63
    public function testAttachment(): void
64
    {
65
        $response = $this->getWrapper()->attachment(__FILE__);
66
67
        $this->assertSame(200, $response->getStatusCode());
68
        $this->assertStringEqualsFile(__FILE__, (string)$response->getBody());
69
        $this->assertSame(filesize(__FILE__), $response->getBody()->getSize());
70
        $this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type'));
71
    }
72
73
    public function testAttachmentResource(): void
74
    {
75
        $response = $this->getWrapper()->attachment(fopen(__FILE__, 'r'), 'file.php');
0 ignored issues
show
Bug introduced by
fopen(__FILE__, 'r') of type false|resource is incompatible with the type Psr\Http\Message\StreamI...eamableInterface|string expected by parameter $filename of Spiral\Http\ResponseWrapper::attachment(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $response = $this->getWrapper()->attachment(/** @scrutinizer ignore-type */ fopen(__FILE__, 'r'), 'file.php');
Loading history...
76
77
        $this->assertSame(200, $response->getStatusCode());
78
        $this->assertStringEqualsFile(__FILE__, (string)$response->getBody());
79
        $this->assertSame(filesize(__FILE__), $response->getBody()->getSize());
80
        $this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type'));
81
    }
82
83
    public function testAttachmentStream(): void
84
    {
85
        $response = $this->getWrapper()->attachment(new Stream(fopen(__FILE__, 'r'), 'r'), 'file.php');
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'r') can also be of type false; however, parameter $stream of Laminas\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $response = $this->getWrapper()->attachment(new Stream(/** @scrutinizer ignore-type */ fopen(__FILE__, 'r'), 'r'), 'file.php');
Loading history...
86
87
        $this->assertSame(200, $response->getStatusCode());
88
        $this->assertStringEqualsFile(__FILE__, (string)$response->getBody());
89
        $this->assertSame(filesize(__FILE__), $response->getBody()->getSize());
90
        $this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type'));
91
    }
92
93
    public function testAttachmentStreamable(): void
94
    {
95
        $response = $this->getWrapper()->attachment(
96
            new Streamable(new Stream(fopen(__FILE__, 'r'), 'r')),
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'r') can also be of type false; however, parameter $stream of Laminas\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            new Streamable(new Stream(/** @scrutinizer ignore-type */ fopen(__FILE__, 'r'), 'r')),
Loading history...
97
            'file.php'
98
        );
99
100
        $this->assertSame(200, $response->getStatusCode());
101
        $this->assertStringEqualsFile(__FILE__, (string)$response->getBody());
102
        $this->assertSame(filesize(__FILE__), $response->getBody()->getSize());
103
        $this->assertSame('application/octet-stream', (string)$response->getHeaderLine('Content-Type'));
104
    }
105
106
    public function testCreate(): void
107
    {
108
        $response = $this->getWrapper()->create(400);
109
110
        $this->assertEquals(400, $response->getStatusCode());
111
    }
112
113
    public function testAttachmentStreamNoName(): void
114
    {
115
        $this->expectException(ResponseException::class);
116
        $this->getWrapper()->attachment(new Stream(fopen(__FILE__, 'rb'), 'r'));
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'rb') can also be of type false; however, parameter $stream of Laminas\Diactoros\Stream::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        $this->getWrapper()->attachment(new Stream(/** @scrutinizer ignore-type */ fopen(__FILE__, 'rb'), 'r'));
Loading history...
117
    }
118
119
    public function testAttachmentException(): void
120
    {
121
        $this->expectException(ResponseException::class);
122
        $this->getWrapper()->attachment('invalid');
123
    }
124
125
    protected function getWrapper(): ResponseWrapper
126
    {
127
        return new ResponseWrapper(
128
            new ResponseFactory(new HttpConfig(['headers' => []])),
129
            new StreamFactory(),
130
            new Files()
131
        );
132
    }
133
}
134