HalJsonProcessorSpec::let()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace spec\TomPHP\HalClient\Processor;
4
5
use PhpSpec\ObjectBehavior;
6
use TomPHP\HalClient\Exception\FieldNotFoundException;
7
use TomPHP\HalClient\Exception\ProcessingException;
8
use TomPHP\HalClient\ResourceFetcher;
9
use Zend\Diactoros\Response;
10
11
class HalJsonProcessorSpec extends ObjectBehavior
12
{
13
    /** @var Response */
14
    private $response;
15
16
    function let()
17
    {
18
        $body = 'data://text/plain,{
19
            "_links": {
20
                "self": {
21
                    "href": "http://www.somewhere.com/",
22
                    "rel": "self"
23
                },
24
                "other": {
25
                    "href": "http://www.somewhere.com/other"
26
                }
27
            },
28
            "field1": "value1",
29
            "map": {
30
                "mapfield": "mapvalue"
31
            },
32
            "collection": [
33
                {"name": "item1"}
34
            ],
35
            "_embedded": {
36
                "resource1": {
37
                    "subfield": "subvalue"
38
                },
39
                "resourcecollection": [
40
                    {"name": "collectionvalue"}
41
                ]
42
            }
43
        }';
44
45
        $this->response = new Response($body, 200, ['content-type' => 'application/hal+json']);
46
    }
47
48
    function it_returns_the_hal_plus_json_content_type()
49
    {
50
        $this->getContentType()->shouldReturn('application/hal+json');
51
    }
52
53
    function it_throws_for_bad_json_error(ResourceFetcher $fetcher)
54
    {
55
        $json = '{bad, josn}';
56
        json_decode($json);
57
        $error = json_last_error_msg();
58
59
        $response = new Response("data://text/plain,$json", 200, ['content-type' => 'application/hal+json']);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $json instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
60
61
        $this->shouldThrow(ProcessingException::badJson($error))
62
             ->duringProcess($response, $fetcher);
63
    }
64
65
    function it_processes_simple_single_fields(ResourceFetcher $fetcher)
66
    {
67
        $resource = $this->process($this->response, $fetcher);
68
69
        $resource->field1->getValue()->shouldReturn('value1');
70
    }
71
72
    function it_processes_fields_which_are_maps(ResourceFetcher $fetcher)
73
    {
74
        $resource = $this->process($this->response, $fetcher);
75
76
        $resource->map->mapfield->getValue()->shouldReturn('mapvalue');
77
    }
78
79
    function it_processes_fields_which_are_collections(ResourceFetcher $fetcher)
80
    {
81
        $resource = $this->process($this->response, $fetcher);
82
83
        $resource->collection[0]->name->getValue()->shouldReturn('item1');
84
    }
85
86
    function it_processes_links(ResourceFetcher $fetcher)
87
    {
88
        $resource = $this->process($this->response, $fetcher);
89
90
        $resource->getLinks()->shouldReturn(['self', 'other']);
91
    }
92
93
    function it_does_not_add_links_as_a_field(ResourceFetcher $fetcher)
94
    {
95
        $resource = $this->process($this->response, $fetcher);
96
97
        $resource->shouldThrow(new FieldNotFoundException('_links'))
98
                 ->duringGetField('_links');
99
    }
100
101
    function it_does_not_add_embedded_as_a_field(ResourceFetcher $fetcher)
102
    {
103
        $resource = $this->process($this->response, $fetcher);
104
105
        $resource->shouldThrow(new FieldNotFoundException('_embedded'))
106
                 ->duringGetField('_embedded');
107
    }
108
109
    function it_processes_resources(ResourceFetcher $fetcher)
110
    {
111
        $resource = $this->process($this->response, $fetcher);
112
113
        $resource->getResource('resource1')->subfield->getValue()->shouldReturn('subvalue');
114
    }
115
116
    function it_processes_resources_collections(ResourceFetcher $fetcher)
117
    {
118
        $resource = $this->process($this->response, $fetcher);
119
120
        $resource->getResource('resourcecollection')[0]->name->getValue()->shouldReturn('collectionvalue');
121
    }
122
}
123