|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElevenLabs\ApiServiceBundle; |
|
4
|
|
|
|
|
5
|
|
|
use ElevenLabs\Api\Service\ApiService; |
|
6
|
|
|
use ElevenLabs\Api\Service\Resource\Collection; |
|
7
|
|
|
use Http\Message\MessageFactory; |
|
8
|
|
|
use Http\Mock\Client; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
10
|
|
|
|
|
11
|
|
|
class ServiceInstantiationTest extends WebTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
private $container; |
|
14
|
|
|
|
|
15
|
|
|
public function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
static::bootKernel(); |
|
18
|
|
|
$this->container = static::$kernel->getContainer(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testApiServiceWithDefaultClient() |
|
22
|
|
|
{ |
|
23
|
|
|
$apiService = $this->container->get('api_service.api.foo'); |
|
24
|
|
|
|
|
25
|
|
|
assertThat($apiService, isInstanceOf(ApiService::class)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testApiServiceWithASpecifiedClient() |
|
29
|
|
|
{ |
|
30
|
|
|
$apiService = $this->container->get('api_service.api.bar'); |
|
31
|
|
|
|
|
32
|
|
|
assertThat($apiService, isInstanceOf(ApiService::class)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function testAnApiServiceThatReturnResponseObjects() |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var Client $clientMock */ |
|
38
|
|
|
$clientMock = $this->container->get('httplug.client.mock'); |
|
39
|
|
|
/** @var MessageFactory $messageFactory */ |
|
40
|
|
|
$messageFactory = $this->container->get('httplug.message_factory'); |
|
41
|
|
|
/** @var ApiService $apiService */ |
|
42
|
|
|
$apiService = $this->container->get('api_service.api.bar'); |
|
43
|
|
|
|
|
44
|
|
|
$clientMock->addResponse( |
|
45
|
|
|
$messageFactory->createResponse(201) |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$response = $apiService->call('addFoo'); |
|
49
|
|
|
assertThat($response->getStatusCode(), equalTo(201)); |
|
50
|
|
|
|
|
51
|
|
|
$clientMock->addResponse( |
|
52
|
|
|
$messageFactory->createResponse( |
|
53
|
|
|
200, |
|
54
|
|
|
'', |
|
55
|
|
|
[ |
|
56
|
|
|
'Content-Type' => 'application/json' |
|
57
|
|
|
], |
|
58
|
|
|
json_encode( |
|
59
|
|
|
[ |
|
60
|
|
|
[ |
|
61
|
|
|
'id' => 1, |
|
62
|
|
|
'name' => 'foo' |
|
63
|
|
|
], |
|
64
|
|
|
[ |
|
65
|
|
|
'id' => 2, |
|
66
|
|
|
'name' => 'foo' |
|
67
|
|
|
] |
|
68
|
|
|
] |
|
69
|
|
|
) |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$response = $apiService->call('getFooCollection'); |
|
74
|
|
|
assertThat($response->getStatusCode(), equalTo(200)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testAnApiServiceThatReturnAResource() |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var Client $clientMock */ |
|
80
|
|
|
$clientMock = $this->container->get('httplug.client.mock'); |
|
81
|
|
|
/** @var MessageFactory $messageFactory */ |
|
82
|
|
|
$messageFactory = $this->container->get('httplug.message_factory'); |
|
83
|
|
|
/** @var ApiService $apiService */ |
|
84
|
|
|
$apiService = $this->container->get('api_service.api.foo'); |
|
85
|
|
|
|
|
86
|
|
|
$clientMock->addResponse( |
|
87
|
|
|
$messageFactory->createResponse( |
|
88
|
|
|
200, |
|
89
|
|
|
'', |
|
90
|
|
|
[ |
|
91
|
|
|
'Content-Type' => 'application/json', |
|
92
|
|
|
'X-Page' => '1', |
|
93
|
|
|
'X-Per-Page' => '2', |
|
94
|
|
|
'X-Total-Pages' => '4', |
|
95
|
|
|
'X-Total-Items' => '8', |
|
96
|
|
|
], |
|
97
|
|
|
json_encode( |
|
98
|
|
|
[ |
|
99
|
|
|
[ |
|
100
|
|
|
'id' => 1, |
|
101
|
|
|
'name' => 'foo' |
|
102
|
|
|
], |
|
103
|
|
|
[ |
|
104
|
|
|
'id' => 2, |
|
105
|
|
|
'name' => 'foo' |
|
106
|
|
|
] |
|
107
|
|
|
] |
|
108
|
|
|
) |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
/** @var Collection $resource */ |
|
113
|
|
|
$resource = $apiService->call('getFooCollection'); |
|
114
|
|
|
|
|
115
|
|
|
assertThat($resource, isInstanceOf(Collection::class)); |
|
116
|
|
|
assertThat($resource->getData(), countOf(2)); |
|
117
|
|
|
|
|
118
|
|
|
$pagination = $resource->getPagination(); |
|
119
|
|
|
assertThat($pagination->getPage(), equalTo('1')); |
|
120
|
|
|
assertThat($pagination->getPerPage(), equalTo('2')); |
|
121
|
|
|
assertThat($pagination->getTotalPages(), equalTo('4')); |
|
122
|
|
|
assertThat($pagination->getTotalItems(), equalTo('8')); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|