-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
TestCase.php
266 lines (236 loc) · 8.39 KB
/
TestCase.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?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 <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 4.0.0
*/
namespace Tmdb\Tests;
use Http\Discovery\Psr17FactoryDiscovery;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Tmdb\Event\BeforeRequestEvent;
use Tmdb\Event\Listener\RequestListener;
use Tmdb\Event\RequestEvent;
use Tmdb\Token\Api\ApiToken;
use Tmdb\Client;
use Tmdb\Common\ObjectHydrator;
use Tmdb\Common\ParameterBag;
use Tmdb\Event\Listener\Request\AcceptJsonRequestListener;
use Tmdb\Event\Listener\Request\ApiTokenRequestListener;
use Tmdb\Event\Listener\Request\ContentTypeJsonRequestListener;
use Tmdb\HttpClient\HttpClient;
use Tmdb\HttpClient\Request;
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
protected $psr18mock = null;
protected $eventDispatcher = null;
/**
* Assert that an array of methods and corresponding classes match
*
* @param $subject
* @param array $instances
* @throws \Exception
*/
protected function assertInstancesOf($subject, array $instances = [])
{
foreach ($instances as $method => $instance) {
try {
$this->assertInstanceOf($instance, $subject->$method());
} catch (\Exception $e) {
throw new \Exception(sprintf(
'Failed asserting that calling "%s" returns an instance of expected "%s".',
sprintf('%s::%s', get_class($subject), $method),
$instance
));
}
}
}
/**
* Load an json file from the Resources directory
*
* @param $file
* @return mixed
*/
protected function loadByFile($file)
{
return json_decode(
file_get_contents(
sprintf(
'%s/%s',
'test/Tmdb/Tests/Resources/',
$file
)
),
true
);
}
/**
* Get a TMDB Client with an mocked HTTP dependency
*
* @param array $options
* @return \Tmdb\Client
*/
protected function getClientWithMockedHttpClient(array $options = array())
{
$options['event_dispatcher']['adapter'] = $this->eventDispatcher = new EventDispatcher();
$options['api_token'] = new ApiToken('abcdef');
$options['http']['client'] = new \Http\Mock\Client();
$response = $this->createMock('Psr\Http\Message\ResponseInterface');
$options['http']['client']->setDefaultResponse($response);
$client = new Client($options);
$requestListener = new RequestListener($client->getHttpClient(), $this->eventDispatcher);
$apiTokenListener = new ApiTokenRequestListener($options['api_token']);
$acceptJsonListener = new AcceptJsonRequestListener();
$jsonContentTypeListener = new ContentTypeJsonRequestListener();
$this->eventDispatcher->addListener(BeforeRequestEvent::class, $apiTokenListener);
$this->eventDispatcher->addListener(BeforeRequestEvent::class, $acceptJsonListener);
$this->eventDispatcher->addListener(BeforeRequestEvent::class, $jsonContentTypeListener);
$this->eventDispatcher->addListener(RequestEvent::class, $requestListener);
/**
* We do not need api keys being added to the requests here.
*
* @var EventDispatcher
*/
foreach ($client->getEventDispatcher()->getListeners() as $event => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof ApiTokenRequestListener) {
$client->getEventDispatcher()->removeListener($event, $listener);
}
}
}
return $client;
}
public function getAdapterMock()
{
return $this->createMock('Tmdb\HttpClient\Adapter\AdapterInterface');
}
/**
* Get TMDB Client
*
* @return Client
*/
protected function getMockedTmdbClient()
{
$adapter = new \Http\Mock\Client();
return $this->_client = new Client([
'api_token' => new ApiToken('abcdef'),
'http' => ['client' => $adapter],
'event_dispatcher' => ['adapter' => new EventDispatcher()]
]);
}
/**
* Get mocked http client
*
* @param string $baseUrl
* @param array $options
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getHttpClientWithMockedAdapter($baseUrl, array $options = [])
{
return $this->_client = new HttpClient(
$baseUrl,
$options,
$this->createMock('Tmdb\HttpClient\Adapter\AdapterInterface'),
$this->createMock('Symfony\Component\EventDispatcher\EventDispatcher')
);
}
/**
* Get mocked http client
*
* @param array $methods
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockedHttpClient(array $methods = [])
{
if (!in_array('send', $methods)) {
$methods[] = 'send';
}
return $this->getMockBuilder('Guzzle\Http\Client')->setMethods($methods)->getMock();
}
/**
* Get the expected request that will deliver a response
*
* @param string $url
* @param array $parameters
* @param string $method
* @param array $headers
* @param null $body
* @return Request
*/
protected function getRequest($url, $parameters = [], $method = 'GET', $headers = [], $body = null)
{
if (
$method == 'POST' ||
$method == 'PUT' ||
$method == 'PATCH' ||
$method == 'DELETE'
) {
$headers['Content-Type'] = 'application/json';
}
$parameters['api_key'] = 'abcdef';
$headers['Accept'] = 'application/json';
$headers['User-Agent'] = sprintf('php-tmdb/api (v%s)', Client::VERSION);
$baseUri = 'https://api.themoviedb.org/3/';
if (strpos($url, $baseUri) === 0) {
$path = substr($url, strlen($baseUri));
} else {
$path = $url;
}
$request = new Request(
$path,
$method,
new ParameterBag($parameters),
new ParameterBag($headers)
);
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
$request->setOptions(new ParameterBag([
'token' => new ApiToken('abcdef'),
'secure' => true,
'cache' => [
'enabled' => false,
// 'adapter' => new FilesystemCache(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-tmdb-api'),
'path' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-tmdb-api',
'subscriber' => null
],
'log' => [
'enabled' => false,
'level' => 'debug',
'adapter' => null,
'path' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-tmdb-api.log',
'subscriber' => null
],
'http' => [
'client' => new \Http\Mock\Client($responseFactory),
'request_factory' => Psr17FactoryDiscovery::findRequestFactory(),
'response_factory' => $responseFactory,
'stream_factory' => Psr17FactoryDiscovery::findStreamFactory(),
'uri_factory' => Psr17FactoryDiscovery::findUriFactory(),
],
'host' => 'api.themoviedb.org/3/',
'base_uri' => $baseUri,
'guest_session_token' => null,
'event_dispatcher' => ['adapter' => $this->eventDispatcher]
]));
if ($body !== null) {
$request->setBody(is_array($body) ? json_encode($body) : $body);
}
return $request;
}
/**
* Hydrate object
*
* @param $object
* @param $data
* @return \Tmdb\Model\AbstractModel
*/
protected function hydrate($object, array $data = [])
{
$objectHydrator = new ObjectHydrator();
return $objectHydrator->hydrate($object, $data);
}
}