Completed
Push — master ( 3ffe12...6e16cc )
by Marin
05:50
created

ApiTestCase::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace OnFleet\Tests;
4
5
use GuzzleHttp\Subscriber\History;
6
use GuzzleHttp\Subscriber\Mock;
7
use OnFleet\Client;
8
use PHPUnit\Framework\TestCase;
9
10
class ApiTestCase extends TestCase
11
{
12
    /**
13
     * Base URL for all paths to append to
14
     *
15
     * @var string
16
     */
17
    protected $baseUrl = 'https://onfleet.com/api/v2/';
18
19
    /**
20
     * @var Client
21
     */
22
    protected $client;
23
24
    /**
25
     * @var Mock
26
     */
27
    protected $mockedResponses;
28
29
    /**
30
     * @var History
31
     */
32
    protected $history;
33
34
    /**
35
     * Setup client with history and mocked responses
36
     */
37
    public function setUp()
38
    {
39
        $this->client = new Client(null);
40
        $this->mockedResponses = new Mock();
41
        $this->history = new History();
42
43
        $this->client->getEmitter()->attach($this->history);
44
        $this->client->getEmitter()->attach($this->mockedResponses);
45
    }
46
47
    /**
48
     * @param string $path
49
     */
50
    public function assertRequestIsGet($path)
51
    {
52
        $request = $this->history->getLastRequest();
53
        $this->assertEquals('GET', $request->getMethod());
54
55
        $this->assertEquals($this->baseUrl . $path, $request->getUrl());
56
    }
57
58
    /**
59
     * Assert request is post, has JSON content type and optionally check payload data
60
     *
61
     * @param string $path
62
     * @param array|null $data
63
     */
64
    public function assertRequestIsPost($path, array $data = null)
65
    {
66
        $request = $this->history->getLastRequest();
67
        $this->assertEquals('POST', $request->getMethod());
68
        $this->assertEquals('application/json', $request->getHeader('Content-type'));
69
70
        $this->assertEquals($this->baseUrl . $path, $request->getUrl());
71
72
        if (null !== $data) {
73
            $payload = $request->getBody()->__toString();
74
            $this->assertJsonStringEqualsJsonString(json_encode($data), $payload);
75
        }
76
    }
77
78
    /**
79
     * Assert request is put, has JSON content type and optionally check payload data
80
     *
81
     * @param string $path
82
     * @param array|null $data
83
     */
84
    public function assertRequestIsPut($path, array $data = null)
85
    {
86
        $request = $this->history->getLastRequest();
87
        $this->assertEquals('PUT', $request->getMethod());
88
        $this->assertEquals('application/json', $request->getHeader('Content-type'));
89
90
        $this->assertEquals($this->baseUrl . $path, $request->getUrl());
91
92
        if (null !== $data) {
93
            $payload = $request->getBody()->__toString();
94
            $this->assertJsonStringEqualsJsonString(json_encode($data), $payload);
95
        }
96
    }
97
}
98