1 | <?php |
||||
2 | namespace ElevenLabs\Api\Service\Functional; |
||||
3 | |||||
4 | use ElevenLabs\Api\Service\ApiServiceBuilder; |
||||
5 | use ElevenLabs\Api\Service\Exception\RequestViolations; |
||||
6 | use ElevenLabs\Api\Service\Exception\ResponseViolations; |
||||
7 | use ElevenLabs\Api\Service\Pagination\Pagination; |
||||
8 | use ElevenLabs\Api\Service\Pagination\Provider\PaginationHeader; |
||||
9 | use ElevenLabs\Api\Service\Resource\Collection; |
||||
10 | use ElevenLabs\Api\Service\Resource\Item; |
||||
11 | use GuzzleHttp\Psr7\Response; |
||||
12 | use Http\Discovery\MessageFactoryDiscovery; |
||||
13 | use Http\Mock\Client; |
||||
14 | use Http\Promise\Promise; |
||||
15 | use PHPUnit\Framework\TestCase; |
||||
16 | |||||
17 | class ApiServiceTest extends TestCase |
||||
18 | { |
||||
19 | /** @var string */ |
||||
20 | private $schemaFile; |
||||
21 | /** @var Client */ |
||||
22 | private $httpMockClient; |
||||
23 | |||||
24 | public function setUp() |
||||
25 | { |
||||
26 | $this->schemaFile = 'file://'.__DIR__.'/../fixtures/httpbin.yml'; |
||||
27 | $this->httpMockClient = new MockClient(); |
||||
28 | } |
||||
29 | |||||
30 | /** @test */ |
||||
31 | public function itCanMakeASynchronousCall() |
||||
32 | { |
||||
33 | $apiService = (new ApiServiceBuilder()) |
||||
34 | ->withHttpClient($this->httpMockClient) |
||||
35 | ->withMessageFactory($messageFactory = MessageFactoryDiscovery::find()) |
||||
36 | ->build($this->schemaFile); |
||||
37 | |||||
38 | $this->httpMockClient->addResponse( |
||||
39 | $messageFactory->createResponse( |
||||
40 | $statusCode = 200, |
||||
41 | $reasonPhrase = '', |
||||
42 | $headers = ['Content-Type' => 'application/json'], |
||||
43 | $body = json_encode( |
||||
44 | [ |
||||
45 | 'origin' => '127.0.0.1', |
||||
46 | 'url' => 'https://httpbin.org/get' |
||||
47 | ] |
||||
48 | ) |
||||
49 | ) |
||||
50 | ); |
||||
51 | |||||
52 | $response = $apiService->call('dumpGetRequest'); |
||||
53 | |||||
54 | assertThat($response, isInstanceOf(Item::class)); |
||||
55 | } |
||||
56 | |||||
57 | /** @test */ |
||||
58 | public function itCanMakeAnAsynchronousCall() |
||||
59 | { |
||||
60 | $apiService = (new ApiServiceBuilder()) |
||||
61 | ->withHttpClient($this->httpMockClient) |
||||
62 | ->withMessageFactory($messageFactory = MessageFactoryDiscovery::find()) |
||||
63 | ->build($this->schemaFile); |
||||
64 | |||||
65 | $this->httpMockClient->addResponse( |
||||
66 | $messageFactory->createResponse( |
||||
67 | $statusCode = 200, |
||||
68 | $reasonPhrase = '', |
||||
69 | $headers = ['Content-Type' => 'application/json'], |
||||
70 | $body = json_encode( |
||||
71 | [ |
||||
72 | 'origin' => '127.0.0.1', |
||||
73 | 'url' => 'https://httpbin.org/get' |
||||
74 | ] |
||||
75 | ) |
||||
76 | ) |
||||
77 | ); |
||||
78 | |||||
79 | $promise = $apiService->callAsync('dumpGetRequest'); |
||||
80 | |||||
81 | assertThat($promise, isInstanceOf(Promise::class)); |
||||
82 | assertThat($promise->wait(), isInstanceOf(Item::class)); |
||||
83 | } |
||||
84 | |||||
85 | /** @test */ |
||||
86 | public function itValidateTheRequestByDefault() |
||||
87 | { |
||||
88 | $this->expectException(RequestViolations::class); |
||||
89 | |||||
90 | $apiService = ApiServiceBuilder::create() |
||||
91 | ->build($this->schemaFile); |
||||
92 | |||||
93 | $apiService->call('dumpGetRequest', ['aDate' => 'notADateString']); |
||||
94 | } |
||||
95 | |||||
96 | /** @test */ |
||||
97 | public function itAllowTheRequestValidationToBeDisable() |
||||
98 | { |
||||
99 | $apiService = ApiServiceBuilder::create() |
||||
100 | ->disableRequestValidation() |
||||
101 | ->withHttpClient($this->httpMockClient) |
||||
102 | ->withBaseUri('https://domain.tld') |
||||
103 | ->build($this->schemaFile); |
||||
104 | |||||
105 | $this->httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], '{}')); |
||||
106 | |||||
107 | $apiService->call('dumpGetRequest', [ |
||||
108 | 'aPath' => 1, |
||||
109 | 'aDate' => 'notADateString', |
||||
110 | 'aBody' => ['foo' => 'bar'] |
||||
111 | ]); |
||||
112 | |||||
113 | $request = current($this->httpMockClient->getRequests()); |
||||
114 | |||||
115 | assertThat($request->getUri()->__toString(), equalTo('https://domain.tld/get/1?aDate=notADateString')); |
||||
116 | } |
||||
117 | |||||
118 | /** @test */ |
||||
119 | public function itAllowTheResponseValidationToBeEnabled() |
||||
120 | { |
||||
121 | $this->expectException(ResponseViolations::class); |
||||
122 | |||||
123 | $apiService = ApiServiceBuilder::create() |
||||
124 | ->enableResponseValidation() |
||||
125 | ->withHttpClient($this->httpMockClient) |
||||
126 | ->withBaseUri('https://domain.tld') |
||||
127 | ->build($this->schemaFile); |
||||
128 | |||||
129 | $this->httpMockClient->addResponse( |
||||
130 | new Response( |
||||
131 | 200, |
||||
132 | ['Content-Type' => 'application/json'], |
||||
133 | '{"notAValidProperty": "oups"}' |
||||
134 | ) |
||||
135 | ); |
||||
136 | |||||
137 | $apiService->call('dumpGetRequest'); |
||||
138 | } |
||||
139 | |||||
140 | public function itCanPaginate() |
||||
141 | { |
||||
142 | $apiService = ApiServiceBuilder::create() |
||||
0 ignored issues
–
show
|
|||||
143 | ->withHttpClient($this->httpMockClient) |
||||
144 | ->withBaseUri('https://domain.tld') |
||||
145 | ->withPaginationProvider(new PaginationHeader()) |
||||
146 | ->build($this->schemaFile); |
||||
147 | |||||
148 | $this->httpMockClient->addResponse( |
||||
149 | new Response( |
||||
150 | 200, |
||||
151 | [ |
||||
152 | 'Content-Type' => 'application/json', |
||||
153 | 'X-Page' => '1', |
||||
154 | 'X-Per-Page' => '10', |
||||
155 | 'X-Total-Pages' => '10', |
||||
156 | 'X-Total-Items' => '100', |
||||
157 | 'Link' => [ |
||||
158 | '<http://domain.tld?page=1>; rel="first"', |
||||
159 | '<http://domain.tld?page=10>; rel="last"', |
||||
160 | '<http://domain.tld?page=4>; rel="next"', |
||||
161 | '<http://domain.tld?page=2>; rel="prev"', |
||||
162 | ] |
||||
163 | ], |
||||
164 | '[{"foo": "value 1"}, {"foo": "value 2"}]' |
||||
165 | ) |
||||
166 | ); |
||||
167 | |||||
168 | $resource = $apiService->call('getFakeCollection'); |
||||
169 | |||||
170 | assertThat($resource, isInstanceOf(Collection::class)); |
||||
171 | assertThat($resource->hasPagination(), isTrue()); |
||||
172 | assertThat($resource->getPagination(), isInstanceOf(Pagination::class)); |
||||
173 | } |
||||
174 | |||||
175 | /** @test */ |
||||
176 | public function itDoesNotTryToValidateTheResponseBodyIfNoBodySchemaIsProvided() |
||||
177 | { |
||||
178 | $apiService = ApiServiceBuilder::create() |
||||
179 | ->enableResponseValidation() |
||||
180 | ->withHttpClient($this->httpMockClient) |
||||
181 | ->withMessageFactory($messageFactory = MessageFactoryDiscovery::find()) |
||||
182 | ->build($this->schemaFile); |
||||
183 | |||||
184 | $this->httpMockClient->addResponse( |
||||
185 | $messageFactory->createResponse(201) |
||||
186 | ); |
||||
187 | |||||
188 | $result = $apiService->call('postResponseWithoutBody'); |
||||
189 | |||||
190 | assertThat($result, isInstanceOf(Item::class)); |
||||
191 | assertThat($result->getData(), isEmpty()); |
||||
0 ignored issues
–
show
The method
getData() does not exist on Psr\Http\Message\ResponseInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
192 | assertThat($result->getMeta(), arrayHasKey('Host')); |
||||
0 ignored issues
–
show
The method
getMeta() does not exist on Psr\Http\Message\ResponseInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
193 | } |
||||
194 | } |
||||
195 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.