-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathTestCase.php
More file actions
153 lines (130 loc) · 4.08 KB
/
TestCase.php
File metadata and controls
153 lines (130 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <michael@wtfz.net>
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/
namespace Tmdb\Tests\Api;
use PHPUnit_Framework_MockObject_MockObject;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Tmdb\Token\Api\ApiToken;
use Tmdb\Client;
use Tmdb\Common\ParameterBag;
use Tmdb\Tests\TestCase as Base;
use Tmdb\Token\Session\GuestSessionToken;
use function json_decode;
abstract class TestCase extends Base
{
protected $clonedInitialAdapter;
private $_api;
/**
* @var Client
*/
private $_client;
/**
* Return regular objects but replace the http adapter to not actually send requests
*
* @param array $options
* @return mixed
*/
protected function getApiWithMockedHttpAdapter(array $options = [])
{
$this->_client = $this->getClientWithMockedHttpClient($options);
$apiClass = $this->getApiClass();
return new $apiClass($this->_client);
}
abstract protected function getApiClass();
/**
* Mock the API methods themselfs
*
* @param array $methods
* @param array $clientMethods
* @param GuestSessionToken|null $sessionToken
* @return PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockedApi(array $methods = [], array $clientMethods = [], $guestSessionToken = null)
{
$this->_client = $this->getClientWithMockedHttpClient($clientMethods);
if ($guestSessionToken) {
$this->_client->setGuestSessionToken($guestSessionToken);
}
return $this->_api = $this->getMockBuilder($this->getApiClass())
->onlyMethods($methods)
->setConstructorArgs([$this->_client])
->getMock();
}
/**
* Provide the default query parameters to merge in
*
* @return array
*/
protected function getDefaultQueryParameters()
{
return [
'secure' => false,
'base_url' => 'http://api.themoviedb.org/3/',
'headers' => new ParameterBag(['accept' => 'application/json']),
'token' => new ApiToken('abcdef')
];
}
/**
* @param $path
* @param string $method
*/
protected function assertLastRequestIsWithPathAndMethod($path, $method = 'GET')
{
$lastRequest = $this->getLastRequest();
$this->assertEquals($path, $lastRequest->getUri()->getPath());
$this->assertEquals($method, $lastRequest->getMethod());
}
/**
* @return RequestInterface
*/
protected function getLastRequest()
{
$requests = $this->getPsr18Client()->getRequests();
$lastRequest = array_pop($requests);
return $lastRequest;
}
/**
* Shortcut to obtain the http client adapter
*
* @return ClientInterface|\Http\Mock\Client
*/
protected function getPsr18Client()
{
return clone $this->_client->getHttpClient()->getPsr18Client();
}
/**
* @param mixed $contents
*/
protected function assertRequestBodyHasContents($contents)
{
$lastRequest = $this->getLastRequest();
$lastRequest->getBody()->rewind();
$actualBody = $lastRequest->getBody()->getContents();
if ($lastRequest->hasHeader('content-type')) {
$contentType = $lastRequest->getHeader('content-type')[0];
if ('application/json' === $contentType) {
$actualBody = json_decode($actualBody, true);
}
}
$this->assertEquals($contents, $actualBody);
}
/**
* @param array $parameters
*/
protected function assertRequestHasQueryParameters(array $parameters = array())
{
$actualParameters = array();
parse_str($this->getLastRequest()->getUri()->getQuery(), $actualParameters);
$this->assertEquals($parameters, $actualParameters);
}
}