|
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'; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|