File: ResponseTest.php

package info (click to toggle)
php-slim-psr7 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: php: 5,274; makefile: 17; sh: 11; xml: 10
file content (200 lines) | stat: -rw-r--r-- 6,143 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php

/**
 * Slim Framework (https://slimframework.com)
 *
 * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
 */

declare(strict_types=1);

namespace Slim\Tests\Psr7;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Slim\Psr7\Environment;
use Slim\Psr7\Headers;
use Slim\Psr7\Response;
use Slim\Psr7\Stream;
use stdClass;

use function fopen;
use function property_exists;

class ResponseTest extends TestCase
{
    protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
    {
        // only if PHP version < 8.1
        if (PHP_VERSION_ID > 80100) {
            return;
        }
        $property->setAccessible($accessible);
    }

    public function testConstructorWithDefaultArgs()
    {
        $response = new Response();

        $headersReflection = new ReflectionProperty($response, 'headers');
        $this->setAccessible($headersReflection);

        $bodyReflection = new ReflectionProperty($response, 'body');
        $this->setAccessible($bodyReflection);

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertInstanceOf(Headers::class, $headersReflection->getValue($response));
        $this->assertInstanceOf(Stream::class, $bodyReflection->getValue($response));
    }

    public function testConstructorWithCustomArgs()
    {
        $headers = new Headers();
        $body = new Stream(fopen('php://temp', 'r+'));
        $response = new Response(404, $headers, $body);

        $headersReflection = new ReflectionProperty($response, 'headers');
        $this->setAccessible($headersReflection);

        $bodyReflection = new ReflectionProperty($response, 'body');
        $this->setAccessible($bodyReflection);

        $this->assertEquals(404, $response->getStatusCode());
        $this->assertSame($headers, $headersReflection->getValue($response));
        $this->assertSame($body, $bodyReflection->getValue($response));
    }

    public function testDeepCopyClone()
    {
        $headers = new Headers();
        $body = new Stream(fopen('php://temp', 'r+'));
        $response = new Response(404, $headers, $body);
        $clone = clone $response;

        $headersReflection = new ReflectionProperty($response, 'headers');
        $this->setAccessible($headersReflection);

        $this->assertEquals(404, $clone->getStatusCode());
        $this->assertEquals('1.1', $clone->getProtocolVersion());
        $this->assertNotSame($headers, $headersReflection->getValue($clone));
    }

    public function testDisableSetter()
    {
        $response = new Response();
        $response->foo = 'bar';

        $this->assertFalse(property_exists($response, 'foo'));
    }

    public function testGetStatusCode()
    {
        $response = new Response();
        $responseStatus = new ReflectionProperty($response, 'status');
        $this->setAccessible($responseStatus);
        $responseStatus->setValue($response, 404);

        $this->assertEquals(404, $response->getStatusCode());
    }

    public function testWithStatus()
    {
        $response = new Response();
        $clone = $response->withStatus(302);

        $this->assertEquals(302, $clone->getStatusCode());
    }

    public function testWithStatusInvalidStatusCodeThrowsException()
    {
        $this->expectException(InvalidArgumentException::class);

        $response = new Response();
        $response->withStatus(800);
    }

    public function testWithStatusInvalidReasonPhraseThrowsException()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Response reason phrase must be a string');

        $response = new Response();
        $response->withStatus(200, null);
    }

    public function testWithStatusEmptyReasonPhrase()
    {
        $responseWithNoMessage = new Response(310);

        $this->assertEquals('', $responseWithNoMessage->getReasonPhrase());
    }

    public function testReasonPhraseContainsCarriageReturn()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Reason phrase contains one of the following prohibited characters: \r \n');

        $response = new Response();
        $response = $response->withStatus(404, "Not Found\r");
    }

    public function testReasonPhraseContainsLineFeed()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Reason phrase contains one of the following prohibited characters: \r \n');

        $response = new Response();
        $response = $response->withStatus(404, "Not Found\n");
    }

    public function testWithStatusValidReasonPhraseObject()
    {
        $response = new Response();
        $response = $response->withStatus(200, new StringableTestObject('Slim OK'));
        $this->assertEquals('Slim OK', $response->getReasonPhrase());
    }

    public function testGetReasonPhrase()
    {
        $response = new Response(404);

        $this->assertEquals('Not Found', $response->getReasonPhrase());
    }

    public function testEmptyReasonPhraseForUnrecognisedCode()
    {
        $response = new Response();
        $response = $response->withStatus(199);

        $this->assertSame('', $response->getReasonPhrase());
    }

    public function testSetReasonPhraseForUnrecognisedCode()
    {
        $response = new Response();
        $response = $response->withStatus(199, 'Random Message');

        $this->assertEquals('Random Message', $response->getReasonPhrase());
    }

    public function testGetCustomReasonPhrase()
    {
        $response = new Response();
        $clone = $response->withStatus(200, 'Custom Phrase');

        $this->assertEquals('Custom Phrase', $clone->getReasonPhrase());
    }

    public function testResponseHeadersDoNotContainAuthorizationHeader()
    {
        $_SERVER = Environment::mock(
            [
                'PHP_AUTH_USER' => 'foo'
            ]
        );

        $response = new Response();
        $this->assertEmpty($response->getHeaders());
    }
}