kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Tests\Koded\Http; |
||
| 4 | |||
| 5 | use Koded\Http\{HTTPError, HTTPMethodNotAllowed}; |
||
| 6 | use PHPUnit\Framework\TestCase; |
||
| 7 | use Psr\Http\Message\StreamInterface; |
||
| 8 | use function Koded\Http\create_stream; |
||
| 9 | use function serialize; |
||
| 10 | use function unserialize; |
||
| 11 | |||
| 12 | class HTTPErrorSerializationTest extends TestCase |
||
| 13 | { |
||
| 14 | public function test_default_object_serialization() |
||
| 15 | { |
||
| 16 | $expected = new HTTPError(424); |
||
| 17 | $actual = unserialize(serialize($expected)); |
||
| 18 | |||
| 19 | $this->assertEquals($expected, $actual); |
||
| 20 | $this->assertNotSame($expected, $actual, |
||
| 21 | '(the instances are not same)'); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function test_full_object_serialization() |
||
| 25 | { |
||
| 26 | $expected = new HTTPMethodNotAllowed(['PUT'], |
||
| 27 | instance: '/test', |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | title: 'HTTPError Test', |
||
| 29 | detail: 'A unit test for serializing the HTTPError object', |
||
| 30 | type: '/url/for/more/details', |
||
| 31 | headers: ['X-Test' => 'true'] |
||
| 32 | ); |
||
| 33 | $expected->setMember('foo', 'bar'); |
||
| 34 | $expected->setMember('bar', 'qux'); |
||
| 35 | |||
| 36 | $actual = unserialize(serialize($expected)); |
||
| 37 | $this->assertEquals($expected, $actual); |
||
| 38 | $this->assertNotSame($expected, $actual, |
||
| 39 | '(the instances are not same)'); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function test_stringable_implementation() |
||
| 43 | { |
||
| 44 | $err = new HTTPError( |
||
| 45 | status: 200, |
||
| 46 | title: 'Test Error', |
||
| 47 | detail: 'A unit test for serializing the HTTPError object', |
||
| 48 | instance: '/test', |
||
| 49 | type: '/test/errors', |
||
| 50 | headers: ['X-Test' => 'true'], |
||
| 51 | ); |
||
| 52 | |||
| 53 | $this->assertJson((string) $err); |
||
| 54 | $this->assertJsonStringEqualsJsonString( |
||
| 55 | '{"status":200,"instance":"/test","detail":"A unit test for serializing the HTTPError object","title":"Test Error","type":"/test/errors"}', |
||
| 56 | (string) $err, |
||
| 57 | 'headers are not serialized as expected'); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @depends test_stringable_implementation |
||
| 62 | */ |
||
| 63 | public function test_conversion_to_stream() |
||
| 64 | { |
||
| 65 | $err = new HTTPError( |
||
| 66 | status: 200, |
||
| 67 | title: 'Test Error', |
||
| 68 | detail: 'A unit test for serializing the HTTPError object', |
||
| 69 | instance: '/test', |
||
| 70 | type: '/test/errors', |
||
| 71 | headers: ['X-Test' => 'true'], |
||
| 72 | ); |
||
| 73 | |||
| 74 | $stream = create_stream($err); |
||
| 75 | |||
| 76 | $this->assertEquals( |
||
| 77 | '{"status":200,"instance":"/test","detail":"A unit test for serializing the HTTPError object","title":"Test Error","type":"/test/errors"}', |
||
| 78 | $stream->getContents(), |
||
| 79 | 'headers are not included as expected'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |