Passed
Push — master ( 55f92d...5fc33e )
by Dan Michael O.
05:59
created

ClientSpec::it_can_make_GET_requests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace spec\Scriptotek\Alma;
4
5
use GuzzleHttp\Psr7\Response;
6
use function GuzzleHttp\Psr7\stream_for;
7
use Http\Mock\Client as MockHttp;
8
use PhpSpec\ObjectBehavior;
9
use Scriptotek\Alma\Zones;
10
11
function str_random()
12
{
13
    return strval(rand(10000, 99999));
14
}
15
16
class ClientSpec extends ObjectBehavior
17
{
18
    public function let()
19
    {
20
        $http = new MockHttp();
21
        $apiKey = 'DummyApiKey';
22
        $region = 'eu';
23
        $this->beConstructedWith($apiKey, $region, Zones::INSTITUTION, $http);
24
25
        return $http;
26
    }
27
28
    protected function httpWithResponseBody($body)
29
    {
30
        $http = $this->let();
31
        $response = new Response();
32
        $response = $response->withBody(stream_for($body));
33
        $http->addResponse($response);
34
        return $http;
35
    }
36
37
    public function it_is_initializable()
38
    {
39
        $this->shouldHaveType('Scriptotek\Alma\Client');
40
    }
41
42
    public function it_only_accepts_valid_regions()
43
    {
44
        $this->shouldThrow('Scriptotek\Alma\Exception\ClientException')
45
            ->duringSetRegion('ux');
46
    }
47
48
    public function it_can_make_GET_requests()
49
    {
50
        $responseBody = str_random();
51
        $this->httpWithResponseBody($responseBody);
52
        $this->get(str_random())->shouldBe($responseBody);
53
    }
54
55
    public function it_sends_an_API_key_with_each_request()
56
    {
57
        $http = $this->httpWithResponseBody(str_random());
58
        $this->getJSON(str_random());
59
60
        // Query string should include apikey
61
        expect($http->getRequests()[0])->getUri()->getQuery()->toContain('apikey=DummyApiKey');
62
    }
63
64
    public function it_can_request_and_parse_JSON()
65
    {
66
        $responseBody = json_encode(['some_key' => 'some_value']);
67
        $http = $this->httpWithResponseBody($responseBody);
68
69
        $this->getJSON(str_random())->some_key->shouldBe('some_value');
70
71
        $request = $http->getRequests()[0];
72
        expect($request->getHeader('Accept')[0])->toBe('application/json');
73
    }
74
75
    public function it_can_request_and_parse_XML()
76
    {
77
        $responseBody = "<?xml version=\"1.0\"?>\n<some_key>some_value</some_key>\n";
78
        $http = $this->httpWithResponseBody($responseBody);
79
80
        $xml = $this->getXML(str_random());
81
82
        // Request headers should include Accept: application/xml
83
        expect($http->getRequests()[0])->getHeader('Accept')[0]->toBe('application/xml');
84
85
        // Response should be of type QuiteSimpleXMLElement
86
        $xml->shouldHaveType('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement');
87
88
        // and have the expected value
89
        $xml->asXML()->shouldBe($responseBody);
90
    }
91
92
    public function it_can_make_PUT_requests()
93
    {
94
        $responseBody = str_random();
95
        $http = $this->httpWithResponseBody($responseBody);
96
97
        $this->put(str_random(), str_random(), 'application/json')->shouldBe(true);
98
99
        expect($http->getRequests())->toHaveCount(1);
100
        expect($http->getRequests()[0])->getMethod()->toBe('PUT');
101
    }
102
103
    public function it_can_get_redirect_locations()
104
    {
105
        $http = $this->let();
106
        $response = new Response();
107
        $response = $response->withHeader('Location', 'http://test.test');
108
        $http->addResponse($response);
109
110
        $this->getRedirectLocation('/')->shouldBe('http://test.test');
111
    }
112
}
113