Completed
Pull Request — master (#6909)
by Damian
26:02 queued 17:19
created

HTTPStreamResponseTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testReplayStream() 0 18 1
B testDirectStream() 0 28 1
1
<?php
2
3
namespace SilverStripe\Control\Tests;
4
5
use SilverStripe\Control\HTTPStreamResponse;
6
use SilverStripe\Dev\SapphireTest;
7
8
class HTTPStreamResponseTest extends SapphireTest
9
{
10
    /**
11
     * Test replaying of stream from memory
12
     */
13
    public function testReplayStream()
14
    {
15
        $path = __DIR__ . '/HTTPStreamResponseTest/testfile.txt';
16
        $stream = fopen($path, 'r');
17
        $response = new HTTPStreamResponse($stream, filesize($path));
18
19
        // Test body (should parse stream directly into memory)
20
        $this->assertEquals("Test output\n", $response->getBody());
21
22
        // Test stream output
23
        ob_start();
24
        $response->output();
25
        $result = ob_get_contents();
26
        ob_end_clean();
27
28
        $this->assertEquals(12, $response->getHeader('Content-Length'));
29
        $this->assertEquals("Test output\n", $result);
30
    }
31
32
    /**
33
     * Test stream directly without loading into memory
34
     */
35
    public function testDirectStream()
36
    {
37
        $path = __DIR__ . '/HTTPStreamResponseTest/testfile.txt';
38
        $stream = fopen($path, 'r');
39
        $metadata = stream_get_meta_data($stream);
40
        $this->assertTrue($metadata['seekable']);
41
        $response = new HTTPStreamResponse($stream, filesize($path));
42
43
        // Test stream output
44
        ob_start();
45
        $response->output();
46
        $result = ob_get_contents();
47
        ob_end_clean();
48
49
        $this->assertEquals(12, $response->getHeader('Content-Length'));
50
        $this->assertEquals("Test output\n", $result);
51
        $this->assertEmpty($response->getSavedBody(), 'Body of seekable stream is un-cached');
52
53
        // Seekable stream can be repeated
54
        ob_start();
55
        $response->output();
56
        $result = ob_get_contents();
57
        ob_end_clean();
58
59
        $this->assertEquals(12, $response->getHeader('Content-Length'));
60
        $this->assertEquals("Test output\n", $result);
61
        $this->assertEmpty($response->getSavedBody(), 'Body of seekable stream is un-cached');
62
    }
63
}
64