Completed
Push — master ( 44d3cf...30d1d7 )
by Marcelo
02:50
created

JsonTest::canMakeGetRequestsWithAuthentication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace BrofistTest\ApiClient;
4
5
use Brofist\ApiClient\Json;
6
use Brofist\ApiClient\JsonInterface;
7
use GuzzleHttp\Client as HttpClient;
8
use PHPUnit_Framework_TestCase;
9
use Psr\Http\Message\ResponseInterface;
10
11
class JsonTest extends PHPUnit_Framework_TestCase
12
{
13
    /** @var Json */
14
    protected $client;
15
16
    /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */
17
    protected $mockClient;
18
19
    /**
20
     * @before
21
     */
22
    public function initialize()
23
    {
24
        $this->setClient();
25
    }
26
27
    /**
28
     * @test
29
     * @expectedException \InvalidArgumentException
30
     * @expectedExceptionMessage Endpoint not set
31
     */
32
    public function throwsExceptionWhenNoEndpointIsGiven()
33
    {
34
        new Json();
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function implementsTheCorrectInterface()
41
    {
42
        $this->assertInstanceOf(JsonInterface::class, $this->client);
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function canMutateEndpoint()
49
    {
50
        $this->client = new Json(['endpoint' => 'https://test.foo.bar/v3/']);
51
        $this->assertEquals('https://test.foo.bar/v3', $this->client->getEndpoint());
52
    }
53
54
    /**
55
     * @test
56
     */
57 View Code Duplication
    public function canMakeGetRequests()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $query = ['foo' => 'bar'];
60
61
        $this->mockClient()->request(
62
            'GET',
63
            $this->url('/foo'),
64
            [
65
                'query' => $query,
66
            ]
67
        )->willReturn($this->fooBarResponse());
68
69
        $data = $this->client->get('/foo', $query);
70
71
        $this->assertFooBarResponse($data);
72
    }
73
74
    /**
75
     * @test
76
     */
77 View Code Duplication
    public function canMakeGetRequestsWithAuthentication()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $this->setClient(['authToken' => 'myToken']);
80
81
        $query = ['foo' => 'bar'];
82
83
        $this->mockClient()->request(
84
            'GET',
85
            $this->url('/foo'),
86
            [
87
                'auth'  => ['myToken', ''],
88
                'query' => $query,
89
            ]
90
        )->willReturn($this->fooBarResponse());
91
92
        $data = $this->client->get('/foo', $query);
93
94
        $this->assertFooBarResponse($data);
95
    }
96
97
    /**
98
     * @test
99
     */
100 View Code Duplication
    public function canMakeGetRequestsWithBasicAuthentication()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->setClient(['basicAuth' => ['username', 'password']]);
103
104
        $query = ['foo' => 'bar'];
105
106
        $this->mockClient()->request(
107
            'GET',
108
            $this->url('/foo'),
109
            [
110
                'auth'  => ['username', 'password'],
111
                'query' => $query,
112
            ]
113
        )->willReturn($this->fooBarResponse());
114
115
        $data = $this->client->get('/foo', $query);
116
117
        $this->assertFooBarResponse($data);
118
    }
119
120
    /**
121
     * @test
122
     */
123 View Code Duplication
    public function canMakePostRequests()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $postData = ['foo' => 'bar'];
126
127
        $this->mockClient()->request(
128
            'POST',
129
            $this->url('/foo'),
130
            [
131
                'form_params' => $postData,
132
            ]
133
        )->willReturn($this->fooBarResponse());
134
135
        $data = $this->client->post('/foo', $postData);
136
137
        $this->assertFooBarResponse($data);
138
    }
139
140
    /**
141
     * @test
142
     */
143 View Code Duplication
    public function canMakePutRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        $putData = ['foo' => 'bar'];
146
147
        $this->mockClient()->request(
148
            'PUT',
149
            $this->url('/foo'),
150
            [
151
                'json' => $putData,
152
            ]
153
        )->willReturn($this->fooBarResponse());
154
155
        $data = $this->client->put('/foo', $putData);
156
157
        $this->assertFooBarResponse($data);
158
    }
159
160
    /**
161
     * @test
162
     * @expectedException \BadMethodCallException
163
     */
164
    public function deleteIsNotImplemented()
165
    {
166
        $this->client->delete('endpoint');
167
    }
168
169
    /**
170
     * @test
171
     * @expectedException \BadMethodCallException
172
     */
173
    public function patchIsNotImplemented()
174
    {
175
        $this->client->patch('endpoint');
176
    }
177
178
    /**
179
     * @return \Prophecy\Prophecy\ObjectProphecy
180
     */
181
    private function mockClient()
182
    {
183
        if ($this->mockClient === null) {
184
            $this->mockClient = $this->prophesize(HttpClient::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\GuzzleHttp\Client::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<GuzzleHttp\Client> of property $mockClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
185
        }
186
187
        return $this->mockClient;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->mockClient; of type Prophecy\Prophecy\ObjectProphecy|GuzzleHttp\Client adds the type GuzzleHttp\Client to the return on line 187 which is incompatible with the return type documented by BrofistTest\ApiClient\JsonTest::mockClient of type Prophecy\Prophecy\ObjectProphecy.
Loading history...
188
    }
189
190
    /**
191
     * @param string $path
192
     *
193
     * @return string
194
     */
195
    private function url($path = '/')
196
    {
197
        return 'https://endpoint/v1' . $path;
198
    }
199
200
    private function fooBarResponse()
201
    {
202
        $response = $this->prophesize(ResponseInterface::class);
203
        $response->getBody()->willReturn('{"foo":"bar"}');
204
205
        return $response->reveal();
206
    }
207
208
    /**
209
     * @param array $data
210
     */
211
    private function assertFooBarResponse($data)
212
    {
213
        $this->assertEquals(['foo' => 'bar'], $data);
214
    }
215
216
217
    private function setClient(array $params = [])
218
    {
219
        $default = [
220
            'endpoint'   => 'https://endpoint/v1/',
221
            'httpClient' => $this->mockClient()->reveal(),
222
        ];
223
        $this->client = new Json(array_merge($default, $params));
224
    }
225
}
226