-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEndpointBase.php
66 lines (54 loc) · 1.43 KB
/
EndpointBase.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
<?php
namespace SWAPI\Tests\Endpoints;
use GuzzleHttp\Client;
use JsonMapper;
class EndpointBase extends \PHPUnit_Framework_TestCase
{
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* @var \JsonMapper
*/
protected $mapper;
public function setUp()
{
$this->client = $this->getClient();
$this->mapper = $this->getMapper();
}
protected function getClient()
{
return new Client([
'base_url' => 'http://swapi.co/api/',
'defaults' => [
'exceptions' => false,
'headers' => [
'User-Agent' => sprintf('php-swapi/%s', 'testing'),
'Accept' => 'application/json',
],
],
]);
}
protected function getMapper()
{
$mapper = new JsonMapper;
$mapper->bExceptionOnUndefinedProperty = true;
$mapper->bExceptionOnMissingData = true;
return $mapper;
}
protected function getMockResponse($method, $path)
{
// "get", "/vehicles/1/" becomes "GET_vehicles_1"
return file_get_contents(sprintf(
"%s/%s_%s",
$this->getMockResponsesPath(),
strtoupper($method),
trim(preg_replace("/[^A-Za-z0-9]+/", "_", $path), "_")
), "r");
}
protected function getMockResponsesPath()
{
return __DIR__ . '/responses/';
}
}