Passed
Pull Request — master (#17)
by Mihail
15:10
created

test_should_throw_exception_when_write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\CallableStream;
6
use PHPUnit\Framework\TestCase;
7
use RuntimeException;
8
9
class CallableStreamTest extends TestCase
10
{
11
    use AssertionTestSupportTrait;
12
13
    public function test_should_initialize_the_stream()
14
    {
15
        $stream = new CallableStream(function() {});
16
        $this->assertFalse($stream->isWritable());
17
        $this->assertFalse($stream->isSeekable());
18
        $this->assertTrue($stream->isReadable());
19
20
        $this->assertSame(0, $stream->tell());
21
        $this->assertFalse($stream->eof());
22
    }
23
24
    public function test_should_reset_the_stream_on_destruct()
25
    {
26
        $callable = function() {};
27
        $stream   = new CallableStream($callable);
28
        $stream->__destruct();
29
30
        $properties = $this->getObjectProperties($stream, ['callable', 'position']);
31
        $this->assertSame(null, $properties['callable']);
32
        $this->assertSame(0, $properties['position']);
33
    }
34
35
    public function test_should_return_content_when_typecasted_to_string()
36
    {
37
        $stream = new CallableStream(function() {
38
            return 'lorem ipsum';
39
        });
40
41
        // not idempotent
42
        $this->assertSame('lorem ipsum', (string)$stream);
43
        $this->assertSame('', (string)$stream, 'After callable is consumed, the content is empty');
44
        $this->assertSame('', (string)$stream);
45
    }
46
47
    public function test_should_return_null_if_detached()
48
    {
49
        $stream = new CallableStream(function() {
50
            return 'lorem ipsum';
51
        });
52
53
        $result = $stream->detach();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $stream->detach() targeting Koded\Http\CallableStream::detach() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
        $this->assertNull($result);
55
    }
56
57
    public function test_should_return_null_for_stream_size()
58
    {
59
        $stream = new CallableStream(function() {
60
            return 'lorem ipsum';
61
        });
62
63
        $this->assertSame(null, $stream->getSize());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $stream->getSize() targeting Koded\Http\CallableStream::getSize() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
    }
65
66
    public function test_should_throw_exception_when_cannot_read()
67
    {
68
        $this->expectException(RuntimeException::class);
69
        $this->expectExceptionMessage('Cannot write to stream');
70
71
        $stream = new CallableStream(function() {
72
            return new \stdClass;
73
        });
74
75
        $stream->getContents();
76
    }
77
78
    public function test_should_return_empty_string_when_throws_exception_while_typecasted()
79
    {
80
        $stream = new CallableStream(function() {
81
            return new \stdClass;
82
        });
83
84
        $this->assertSame('', (string)$stream);
85
    }
86
87
    public function test_should_throw_exception_when_seek()
88
    {
89
        $this->expectException(RuntimeException::class);
90
        $this->expectExceptionMessage('Cannot seek in CallableStream');
91
92
        $stream = new CallableStream(function() {
93
            return 'lorem ipsum';
94
        });
95
96
        $stream->seek(20);
97
    }
98
99
    public function test_should_throw_exception_when_rewind()
100
    {
101
        $this->expectException(RuntimeException::class);
102
        $this->expectExceptionMessage('Cannot rewind the CallableStream');
103
104
        $stream = new CallableStream(function() {
105
            return 'lorem ipsum';
106
        });
107
108
        $stream->rewind();
109
    }
110
111
    public function test_should_throw_exception_when_write()
112
    {
113
        $this->expectException(RuntimeException::class);
114
        $this->expectExceptionMessage('Cannot write to CallableStream');
115
116
        $stream = new CallableStream(function() {
117
            return 'lorem ipsum';
118
        });
119
120
        $stream->write('');
121
    }
122
123
    public function test_should_read_the_whole_content_disregarding_the_length()
124
    {
125
        $stream = new CallableStream(function() {
126
            return 'lorem ipsum';
127
        });
128
129
        $this->assertSame('lorem ipsum', $stream->read(6));
130
    }
131
132
    public function test_should_yield_from_generator()
133
    {
134
        $stream = new CallableStream(function() {
135
            yield 1;
136
            yield 2;
137
            yield 3;
138
139
            return 4;
140
        });
141
142
        $this->assertSame('123', $stream->getContents());
143
    }
144
145
    public function test_metadata()
146
    {
147
        $stream = new CallableStream(function() {
148
            return 'lorem ipsum';
149
        });
150
151
        $metadata = $stream->getMetadata();
152
        $this->assertSame([], $metadata);
153
        $this->assertSame(null, $stream->getMetadata('junk'));
154
    }
155
}
156