ClientTest::canMakePostRequests()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace LeverTest\Api;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Lever\Api\Client;
7
use PHPUnit_Framework_TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * LeverTest\Api\ClientTest
12
 */
13
class ClientTest extends PHPUnit_Framework_TestCase
14
{
15
    /** @var Client */
16
    protected $client;
17
18
    /** @var HttpClient | \Prophecy\Prophecy\ObjectProphecy */
19
    protected $mockClient;
20
21
    public function setUp()
22
    {
23
        $this->client = new Client(
24
            [
25
                'authToken'  => 'myToken',
26
                'httpClient' => $this->mockClient()->reveal(),
27
            ]
28
        );
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function hasDefaultEndpoint()
35
    {
36
        $this->assertEquals('https://api.lever.co/v1', $this->client->getEndpoint());
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function canMutateEndpoint()
43
    {
44
        $this->client = new Client(['endpoint' => 'https://test.lever.co/v2/']);
45
        $this->assertEquals('https://test.lever.co/v2', $this->client->getEndpoint());
46
    }
47
48
    /**
49
     * @test
50
     */
51 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...
52
    {
53
        $query = ['foo' => 'bar'];
54
55
        $this->mockClient()->request(
56
            'GET',
57
            $this->url('/foo'),
58
            [
59
                'auth'  => ['myToken', ''],
60
                'query' => $query,
61
            ]
62
        )->willReturn($this->fooBarResponse());
63
64
        $data = $this->client->get('/foo', $query);
65
66
        $this->assertFooBarResponse($data);
67
    }
68
69
    /**
70
     * @test
71
     */
72 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...
73
    {
74
        $postData = ['foo' => 'bar'];
75
76
        $this->mockClient()->request(
77
            'POST',
78
            $this->url('/foo'),
79
            [
80
                'auth'        => ['myToken', ''],
81
                'form_params' => $postData,
82
            ]
83
        )->willReturn($this->fooBarResponse());
84
85
        $data = $this->client->post('/foo', $postData);
86
87
        $this->assertFooBarResponse($data);
88
    }
89
90
    /**
91
     * @test
92
     */
93 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...
94
    {
95
        $putData = ['foo' => 'bar'];
96
97
        $this->mockClient()->request(
98
            'PUT',
99
            $this->url('/foo'),
100
            [
101
                'auth' => ['myToken', ''],
102
                'json' => $putData,
103
            ]
104
        )->willReturn($this->fooBarResponse());
105
106
        $data = $this->client->put('/foo', $putData);
107
108
        $this->assertFooBarResponse($data);
109
    }
110
111
    /**
112
     * @return \Prophecy\Prophecy\ObjectProphecy
113
     */
114
    private function mockClient()
115
    {
116
        if ($this->mockClient === null) {
117
            $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...
118
        }
119
120
        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 120 which is incompatible with the return type documented by LeverTest\Api\ClientTest::mockClient of type Prophecy\Prophecy\ObjectProphecy.
Loading history...
121
    }
122
123
    /**
124
     * @param string $path
125
     *
126
     * @return string
127
     */
128
    private function url($path = '/')
129
    {
130
        return 'https://api.lever.co/v1' . $path;
131
    }
132
133
    private function fooBarResponse()
134
    {
135
        $response = $this->prophesize(ResponseInterface::class);
136
        $response->getBody()->willReturn('{"foo":"bar"}');
137
138
        return $response->reveal();
139
    }
140
141
    /**
142
     * @param array $data
143
     */
144
    private function assertFooBarResponse($data)
145
    {
146
        $this->assertEquals(['foo' => 'bar'], $data);
147
    }
148
}
149