it_should_throw_exception_when_request_is_invalid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace spec\Nekland\PlacesApi\Api;
4
5
use Nekland\BaseApi\Http\AbstractHttpClient;
6
use PhpSpec\ObjectBehavior;
7
use Prophecy\Argument;
8
9
class AutocompleteSpec extends ObjectBehavior
10
{
11
    function it_is_initializable()
12
    {
13
        $this->shouldHaveType('Nekland\PlacesApi\Api\Autocomplete');
14
        $this->shouldHaveType('Nekland\BaseApi\Api\AbstractApi');
15
    }
16
17
    function let(AbstractHttpClient $client)
18
    {
19
        $this->beConstructedWith($client);
20
    }
21
22
    function it_should_return_request_result(AbstractHttpClient $client)
23
    {
24
        $client->send(Argument::cetera())->willReturn('{
25
  "status": "OK",
26
  "predictions" : [
27
      {
28
         "description" : "Paris, France",
29
         "id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
30
         "matched_substrings" : [
31
            {
32
               "length" : 5,
33
               "offset" : 0
34
            }
35
         ],
36
         "place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ",
37
         "reference" : "CjQlAAAA_KB6EEceSTfkteSSF6U0pvumHCoLUboRcDlAH05N1pZJLmOQbYmboEi0SwXBSoI2EhAhj249tFDCVh4R-PXZkPK8GhTBmp_6_lWljaf1joVs1SH2ttB_tw",
38
         "terms" : [
39
            {
40
               "offset" : 0,
41
               "value" : "Paris"
42
            },
43
            {
44
               "offset" : 7,
45
               "value" : "France"
46
            }
47
         ],
48
         "types" : [ "locality", "political", "geocode" ]
49
      },
50
      {
51
         "description" : "Paris Avenue, Earlwood, New South Wales, Australia",
52
         "id" : "359a75f8beff14b1c94f3d42c2aabfac2afbabad",
53
         "matched_substrings" : [
54
            {
55
               "length" : 5,
56
               "offset" : 0
57
            }
58
         ],
59
         "place_id" : "ChIJrU3KAHG6EmsR5Uwfrk7azrI",
60
         "reference" : "CkQ2AAAARbzLE-tsSQPgwv8JKBaVtbjY48kInQo9tny0k07FOYb3Z_z_yDTFhQB_Ehpu-IKhvj8Msdb1rJlX7xMr9kfOVRIQVuL4tOtx9L7U8pC0Zx5bLBoUTFbw9R2lTn_EuBayhDvugt8T0Oo",
61
         "terms" : [
62
            {
63
               "offset" : 0,
64
               "value" : "Paris Avenue"
65
            },
66
            {
67
               "offset" : 14,
68
               "value" : "Earlwood"
69
            },
70
            {
71
               "offset" : 24,
72
               "value" : "New South Wales"
73
            },
74
            {
75
               "offset" : 41,
76
               "value" : "Australia"
77
            }
78
         ],
79
         "types" : [ "route", "geocode" ]
80
      },
81
      {
82
         "description" : "Paris Street, Carlton, New South Wales, Australia",
83
         "id" : "bee539812eeda477dad282bcc8310758fb31d64d",
84
         "matched_substrings" : [
85
            {
86
               "length" : 5,
87
               "offset" : 0
88
            }
89
         ],
90
         "place_id" : "ChIJCfeffMi5EmsRp7ykjcnb3VY",
91
         "reference" : "CkQ1AAAAAERlxMXkaNPLDxUJFLm4xkzX_h8I49HvGPvmtZjlYSVWp9yUhQSwfsdveHV0yhzYki3nguTBTVX2NzmJDukq9RIQNcoFTuz642b4LIzmLgcr5RoUrZhuNqnFHegHsAjtoUUjmhy4_rA",
92
         "terms" : [
93
            {
94
               "offset" : 0,
95
               "value" : "Paris Street"
96
            },
97
            {
98
               "offset" : 14,
99
               "value" : "Carlton"
100
            },
101
            {
102
               "offset" : 23,
103
               "value" : "New South Wales"
104
            },
105
            {
106
               "offset" : 40,
107
               "value" : "Australia"
108
            }
109
         ],
110
         "types" : [ "route", "geocode" ]
111
      }
112
  ]
113
}');
114
        $this->complete('Paris')->shouldContainsPlaceId('ChIJD7fiBh9u5kcRYJSMaMOCCwQ');
115
    }
116
117
    function it_shoud_return_empty_array_when_no_results(AbstractHttpClient $client)
118
    {
119
        $client->send(Argument::cetera())->willReturn('{"status": "ZERO_RESULTS"}');
120
121
        $this->complete('arfzegreggdfgdsfd')->shouldReturn([]);
122
    }
123
124
    function it_should_throw_exception_when_quota_is_out(AbstractHttpClient $client)
125
    {
126
        $client->send(Argument::cetera())->willReturn('{"status": "OVER_QUERY_LIMIT"}');
127
128
        $this->shouldThrow('Nekland\BaseApi\Exception\QuotaLimitException')->duringComplete('Paris');
129
    }
130
131
    function it_should_throw_exception_when_request_is_denied(AbstractHttpClient $client)
132
    {
133
        $client->send(Argument::cetera())->willReturn('{"status": "REQUEST_DENIED"}');
134
135
        $this->shouldThrow('Nekland\BaseApi\Exception\AuthenticationException')->duringComplete('Paris');
136
    }
137
138
    function it_should_throw_exception_when_request_is_invalid(AbstractHttpClient $client)
139
    {
140
        $client->send(Argument::cetera())->willReturn('{"status": "INVALID_REQUEST"}');
141
142
        $this->shouldThrow('\InvalidArgumentException')->duringComplete('Paris', ['someweird_param' => 'foobar']);
143
    }
144
145
    public function getMatchers()
146
    {
147
        return [
148
            'containsPlaceId' => function ($subject, $param) {
149
                return isset($subject[0]) && $subject[0]['place_id'] == $param;
150
            }
151
        ];
152
    }
153
}
154