Completed
Pull Request — master (#1)
by Marcelo
16:13 queued 06:08
created

JsonTest::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BrofistTest\ApiClient;
4
5
use Brofist\ApiClient\Json;
6
use GuzzleHttp\Client as HttpClient;
7
use PHPUnit_Framework_TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
10
class JsonTest extends PHPUnit_Framework_TestCase
11
{
12
    /** @var Json */
13
    protected $client;
14
15
    /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */
16
    protected $mockClient;
17
18
    /**
19
     * @before
20
     */
21
    public function initialize()
22
    {
23
        $this->setClient();
24
    }
25
26
    /**
27
     * @test
28
     * @expectedException \InvalidArgumentException
29
     * @expectedExceptionMessage Endpoint not set
30
     */
31
    public function throwsExceptionWhenNoEndpointIsGiven()
32
    {
33
        new Json();
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function canMutateEndpoint()
40
    {
41
        $this->client = new Json(['endpoint' => 'https://test.foo.bar/v3/']);
42
        $this->assertEquals('https://test.foo.bar/v3', $this->client->getEndpoint());
43
    }
44
45
    /**
46
     * @test
47
     */
48 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...
49
    {
50
        $query = ['foo' => 'bar'];
51
52
        $this->mockClient()->request(
53
            'GET',
54
            $this->url('/foo'),
55
            [
56
                'query' => $query,
57
            ]
58
        )->willReturn($this->fooBarResponse());
59
60
        $data = $this->client->get('/foo', $query);
61
62
        $this->assertFooBarResponse($data);
63
    }
64
65
    /**
66
     * @test
67
     */
68 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...
69
    {
70
        $this->setClient(['authToken' => 'myToken']);
71
72
        $query = ['foo' => 'bar'];
73
74
        $this->mockClient()->request(
75
            'GET',
76
            $this->url('/foo'),
77
            [
78
                'auth'  => ['myToken', ''],
79
                'query' => $query,
80
            ]
81
        )->willReturn($this->fooBarResponse());
82
83
        $data = $this->client->get('/foo', $query);
84
85
        $this->assertFooBarResponse($data);
86
    }
87
88
    /**
89
     * @test
90
     */
91 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...
92
    {
93
        $this->setClient(['basicAuth' => ['username', 'password']]);
94
95
        $query = ['foo' => 'bar'];
96
97
        $this->mockClient()->request(
98
            'GET',
99
            $this->url('/foo'),
100
            [
101
                'auth'  => ['username', 'password'],
102
                'query' => $query,
103
            ]
104
        )->willReturn($this->fooBarResponse());
105
106
        $data = $this->client->get('/foo', $query);
107
108
        $this->assertFooBarResponse($data);
109
    }
110
111
    /**
112
     * @test
113
     */
114 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...
115
    {
116
        $postData = ['foo' => 'bar'];
117
118
        $this->mockClient()->request(
119
            'POST',
120
            $this->url('/foo'),
121
            [
122
                'form_params' => $postData,
123
            ]
124
        )->willReturn($this->fooBarResponse());
125
126
        $data = $this->client->post('/foo', $postData);
127
128
        $this->assertFooBarResponse($data);
129
    }
130
131
    /**
132
     * @test
133
     */
134 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...
135
    {
136
        $putData = ['foo' => 'bar'];
137
138
        $this->mockClient()->request(
139
            'PUT',
140
            $this->url('/foo'),
141
            [
142
                'json' => $putData,
143
            ]
144
        )->willReturn($this->fooBarResponse());
145
146
        $data = $this->client->put('/foo', $putData);
147
148
        $this->assertFooBarResponse($data);
149
    }
150
151
    /**
152
     * @return \Prophecy\Prophecy\ObjectProphecy
153
     */
154
    private function mockClient()
155
    {
156
        if ($this->mockClient === null) {
157
            $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...
158
        }
159
160
        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 160 which is incompatible with the return type documented by BrofistTest\ApiClient\JsonTest::mockClient of type Prophecy\Prophecy\ObjectProphecy.
Loading history...
161
    }
162
163
    /**
164
     * @param string $path
165
     *
166
     * @return string
167
     */
168
    private function url($path = '/')
169
    {
170
        return 'https://endpoint/v1' . $path;
171
    }
172
173
    private function fooBarResponse()
174
    {
175
        $response = $this->prophesize(ResponseInterface::class);
176
        $response->getBody()->willReturn('{"foo":"bar"}');
177
178
        return $response->reveal();
179
    }
180
181
    /**
182
     * @param array $data
183
     */
184
    private function assertFooBarResponse($data)
185
    {
186
        $this->assertEquals(['foo' => 'bar'], $data);
187
    }
188
189
190
    private function setClient(array $params = [])
191
    {
192
        $default = [
193
            'endpoint'   => 'https://endpoint/v1/',
194
            'httpClient' => $this->mockClient()->reveal(),
195
        ];
196
        $this->client = new Json(array_merge($default, $params));
197
    }
198
}
199