File: RequestBodyTest.php

package info (click to toggle)
php-slim 3.12.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,396 kB
  • sloc: php: 11,581; makefile: 18; xml: 10; sh: 3
file content (331 lines) | stat: -rw-r--r-- 9,168 bytes parent folder | download | duplicates (3)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
 * Slim Framework (https://slimframework.com)
 *
 * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
 */

namespace Slim\Tests\Http;

use PHPUnit;
use ReflectionObject;
use ReflectionProperty;
use Slim\Http\RequestBody;

class RequestBodyTest extends PHPUnit\Framework\TestCase
{
    /** @var string */
    // @codingStandardsIgnoreStart
    protected $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
    // @codingStandardsIgnoreEnd
    /** @var resource */
    protected $stream;
    /** @var RequestBody */
    protected $body;

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp(): void
    {
        $this->body = new RequestBody();
        $this->body->write($this->text);
        $this->body->rewind();
    }

    protected function tearDown(): void
    {
        if (is_resource($this->stream) === true) {
            fclose($this->stream);
        }
        $this->body = null;
    }

    /**
     * This method creates a new resource, and it seeds
     * the resource with lorem ipsum text. The returned
     * resource is readable, writable, and seekable.
     *
     * @param string $mode
     *
     * @return resource
     */
    public function resourceFactory($mode = 'r+')
    {
        $stream = fopen('php://temp', $mode);
        fwrite($stream, $this->text);
        rewind($stream);

        return $stream;
    }

    public function testConstructorAttachesStream()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);

        $this->assertIsResource($bodyStream->getValue($this->body));
    }

    public function testConstructorSetsMetadata()
    {
        $bodyMetadata = new ReflectionProperty($this->body, 'meta');
        $bodyMetadata->setAccessible(true);

        $this->assertIsArray($bodyMetadata->getValue($this->body));
    }

    public function testGetMetadata()
    {
        $this->assertIsArray($this->body->getMetadata());
    }

    public function testGetMetadataKey()
    {
        $this->assertEquals('php://temp', $this->body->getMetadata('uri'));
    }

    public function testGetMetadataKeyNotFound()
    {
        $this->assertNull($this->body->getMetadata('foo'));
    }

    public function testDetach()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);

        $bodyMetadata = new ReflectionProperty($this->body, 'meta');
        $bodyMetadata->setAccessible(true);

        $bodyReadable = new ReflectionProperty($this->body, 'readable');
        $bodyReadable->setAccessible(true);

        $bodyWritable = new ReflectionProperty($this->body, 'writable');
        $bodyWritable->setAccessible(true);

        $bodySeekable = new ReflectionProperty($this->body, 'seekable');
        $bodySeekable->setAccessible(true);

        $result = $this->body->detach();

        $this->assertIsResource($result);
        $this->assertNull($bodyStream->getValue($this->body));
        $this->assertNull($bodyMetadata->getValue($this->body));
        $this->assertNull($bodyReadable->getValue($this->body));
        $this->assertNull($bodyWritable->getValue($this->body));
        $this->assertNull($bodySeekable->getValue($this->body));
    }

    public function testToStringAttached()
    {
        $this->assertEquals($this->text, (string)$this->body);
    }

    public function testToStringAttachedRewindsFirst()
    {
        $this->assertEquals($this->text, (string)$this->body);
        $this->assertEquals($this->text, (string)$this->body);
        $this->assertEquals($this->text, (string)$this->body);
    }

    public function testToStringDetached()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);
        $bodyStream->setValue($this->body, null);

        $this->assertEquals('', (string)$this->body);
    }

    public function testClose()
    {
        $this->body->close();

        $reflector = new ReflectionObject($this->body);
        $attribute = $reflector->getProperty('stream');
        $attribute->setAccessible(true);
        $value = $attribute->getValue($this->body);
        $attribute->setAccessible(false);

        $this->assertEquals(null, $value);
        $this->assertFalse($this->body->isReadable());
        $this->assertFalse($this->body->isWritable());
        $this->assertEquals('', (string)$this->body);

        $this->expectException('RuntimeException');
        $this->body->tell();
    }

    public function testGetSizeAttached()
    {
        $this->assertEquals(mb_strlen($this->text), $this->body->getSize());
    }

    public function testGetSizeDetached()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);
        $bodyStream->setValue($this->body, null);

        $this->assertNull($this->body->getSize());
    }

    public function testTellAttached()
    {
        $this->body->seek(10);

        $this->assertEquals(10, $this->body->tell());
    }

    public function testTellDetachedThrowsRuntimeException()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);
        $bodyStream->setValue($this->body, null);

        $this->expectException('\RuntimeException');
        $this->body->tell();
    }

    public function testEofAttachedFalse()
    {
        $this->body->seek(10);

        $this->assertFalse($this->body->eof());
    }

    public function testEofAttachedTrue()
    {
        while ($this->body->eof() === false) {
            $this->body->read(1024);
        }

        $this->assertTrue($this->body->eof());
    }

    public function testEofDetached()
    {
        $bodyStream = new ReflectionProperty($this->body, 'stream');
        $bodyStream->setAccessible(true);
        $bodyStream->setValue($this->body, null);

        $this->assertTrue($this->body->eof());
    }

    public function testIsReadableAttachedTrue()
    {
        $this->assertTrue($this->body->isReadable());
    }

    public function testIsReadableDetached()
    {
        $this->body->detach();

        $this->assertFalse($this->body->isReadable());
    }

    public function testIsWritableAttachedTrue()
    {
        $this->assertTrue($this->body->isWritable());
    }

    public function testIsWritableDetached()
    {
        $this->body->detach();

        $this->assertFalse($this->body->isWritable());
    }

    public function isSeekableAttachedTrue()
    {
        $this->assertTrue($this->body->isSeekable());
    }

    // TODO: Is seekable is false when attached... how?

    public function testIsSeekableDetached()
    {
        $this->body->detach();

        $this->assertFalse($this->body->isSeekable());
    }

    public function testSeekAttached()
    {
        $this->body->seek(10);

        $this->assertEquals(10, $this->body->tell());
    }

    public function testSeekDetachedThrowsRuntimeException()
    {
        $this->body->detach();

        $this->expectException('\RuntimeException');
        $this->body->seek(10);
    }

    public function testRewindAttached()
    {
        $this->body->seek(10);
        $this->body->rewind();

        $this->assertEquals(0, $this->body->tell());
    }

    public function testRewindDetachedThrowsRuntimeException()
    {
        $this->body->detach();

        $this->expectException('\RuntimeException');
        $this->body->rewind();
    }

    public function testReadAttached()
    {
        $this->assertEquals(substr($this->text, 0, 10), $this->body->read(10));
    }

    public function testReadDetachedThrowsRuntimeException()
    {
        $this->body->detach();

        $this->expectException('\RuntimeException');
        $this->body->read(10);
    }

    public function testWriteAttached()
    {
        while ($this->body->eof() === false) {
            $this->body->read(1024);
        }
        $this->body->write('foo');

        $this->assertEquals($this->text . 'foo', (string)$this->body);
    }

    public function testWriteDetachedThrowsRuntimeException()
    {
        $this->body->detach();

        $this->expectException('\RuntimeException');
        $this->body->write('foo');
    }

    public function testGetContentsAttached()
    {
        $this->body->seek(10);

        $this->assertEquals(substr($this->text, 10), $this->body->getContents());
    }

    public function testGetContentsDetachedThrowsRuntimeException()
    {
        $this->body->detach();

        $this->expectException('\RuntimeException');
        $this->body->getContents();
    }
}