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

HTTPErrorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_default_title_detail_and_message() 0 17 1
A test_extensions() 0 23 1
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\{HTTPBadRequest, HTTPError, HTTPMethodNotAllowed};
6
use PHPUnit\Framework\TestCase;
7
8
class HTTPErrorTest extends TestCase
9
{
10
    public function test_default_title_detail_and_message()
11
    {
12
        $exception = new HTTPError(409);
13
14
        $this->assertSame('Conflict', $exception->getTitle());
15
        $this->assertNotEmpty($exception->getDetail());
16
        $this->assertNotEmpty($exception->getMessage());
17
18
        $this->assertSame(
19
            $exception->getDetail(),
20
            $exception->getMessage(),
21
            'Default message and detail are the same'
22
        );
23
24
        $this->assertSame(
25
            'The request could not be completed due to a conflict with the current state of the target resource',
26
            $exception->getDetail()
27
        );
28
    }
29
30
    public function test_extensions()
31
    {
32
        $exception = (new HTTPBadRequest)
33
            ->setMember('accounts', [
34
                0 => 'account 1',
35
                1 => 'account 2'
36
            ])
37
            ->setMember('balance', 30.0);
38
39
        $expectedXml = <<<'XMLDOC'
40
<?xml version="1.0" encoding="UTF-8"?>
41
<problem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
42
    <status type="xsd:positiveInteger">400</status>
43
    <detail>The response means that server cannot understand the request due to invalid syntax</detail>
44
    <title>Bad Request</title>
45
    <type type="xsd:anyURI">https://httpstatuses.com/400</type>
46
    <accounts>account 1</accounts>
47
    <accounts>account 2</accounts>
48
    <balance type="xsd:float">30</balance>
49
</problem>
50
XMLDOC;
51
52
        $this->assertXmlStringEqualsXmlString($expectedXml, $exception->toXml());
53
    }
54
}
55