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

MessageTraitTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\MessageTrait;
6
use Koded\Http\Stream;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\StreamInterface;
9
10
class MessageTraitTest extends TestCase
11
{
12
    private TestMessage $SUT;
13
14
    public function test_should_deal_with_unsupported_protocol()
15
    {
16
        $this->expectException(\InvalidArgumentException::class);
17
        $this->expectExceptionMessage('Unsupported HTTP protocol version 3');
18
        (new TestMessage)->withProtocolVersion('3');
19
    }
20
21
    public function test_should_set_supported_protocol_versions()
22
    {
23
        $this->assertSame('1.0', $this->SUT->withProtocolVersion('1.0')->getProtocolVersion());
24
        $this->assertSame('1.1', $this->SUT->withProtocolVersion('1.1')->getProtocolVersion());
25
        $this->assertSame('2', $this->SUT->withProtocolVersion('2')->getProtocolVersion());
26
    }
27
28
    public function test_should_always_return_instance_of_stream()
29
    {
30
        $this->assertInstanceOf(StreamInterface::class, $this->SUT->getBody());
31
        $this->assertSame('', (string)$this->SUT->getBody(), 'Returns an empty stream if not initialized or created');
32
    }
33
34
    public function test_should_assign_a_new_body_object()
35
    {
36
        $stream   = new Stream(fopen('php://temp', 'r'));
37
        $instance = $this->SUT->withBody($stream);
38
39
        $this->assertNotSame($instance, $this->SUT);
40
        $this->assertSame($stream, $instance->getBody());
41
    }
42
43
    public function test_magic_set_is_disabled()
44
    {
45
        $this->SUT->fubar = 'this-is-not-set';
0 ignored issues
show
Bug Best Practice introduced by
The property fubar does not exist on Tests\Koded\Http\TestMessage. Since you implemented __set, consider adding a @property annotation.
Loading history...
46
        $this->assertFalse(property_exists($this->SUT, 'fubar'));
47
    }
48
49
    protected function setUp(): void
50
    {
51
        $this->SUT = new TestMessage;
52
    }
53
}
54
55
56
class TestMessage
57
{
58
    use MessageTrait;
59
60
    public function setContent($content)
61
    {
62
        $this->content = $content;
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
    }
64
}
65