forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPetApiTest.php
More file actions
462 lines (403 loc) · 16.8 KB
/
PetApiTest.php
File metadata and controls
462 lines (403 loc) · 16.8 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
namespace OpenAPI\Client;
use OpenAPI\Client\Api\PetApi;
use OpenAPI\Client\Model\ApiResponse;
use OpenAPI\Client\Model\Pet;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Assert;
class PetApiTest extends TestCase
{
/** @var PetApi */
private $api;
// add a new pet (id 10005) to ensure the pet object is available for all the tests
public static function setUpBeforeClass(): void
{
// increase memory limit to avoid fatal error due to findPetByStatus
// returning a lot of data
ini_set('memory_limit', '256M');
// enable debugging
//Configuration::$debug = true;
// new pet
$newPetId = 10005;
$newPet = new Model\Pet;
$newPet->setId($newPetId);
$newPet->setName("PHP Unit Test");
$newPet->setPhotoUrls(["http://test_php_unit_test.com"]);
// new tag
$tag = new Model\Tag;
$tag->setId($newPetId); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Model\Category;
$category->setId($newPetId); // use the same id as pet
$category->setName("test php category");
$newPet->setTags(array($tag));
$newPet->setCategory($category);
}
public function setUp(): void
{
$config = (new Configuration())->setHost('http://localhost/v2');
$this->api = new Api\PetApi(null, $config);
}
public function testGetPetById()
{
$petId = 10005;
$pet = $this->api->getPetById($petId);
$this->assertSame($petId, $pet->getId());
$this->assertSame('PHP Unit Test', $pet->getName());
$this->assertSame('http://test_php_unit_test.com', $pet->getPhotoUrls()[0]);
$this->assertSame($petId, $pet->getCategory()->getId());
$this->assertSame('test php category', $pet->getCategory()->getName());
$this->assertSame($petId, $pet->getTags()[0]->getId());
$this->assertSame('test php tag', $pet->getTags()[0]->getName());
}
/**
* comment out as we've removed invalid endpoints from the spec, we'll introduce something
* similar in the future when we've time to update the petstore server
*
* // test getPetById with a Pet object (id 10005)
* public function testGetPetByIdInObject()
* {
* // initialize the API client without host
* $pet_id = 10005; // ID of pet that needs to be fetched
* $pet_api = new Api\PetApi();
* $pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
* // return Pet (inline model)
* $response = $pet_api->getPetByIdInObject($pet_id);
* $this->assertInstanceOf('OpenAPI\Client\Model\InlineResponse200', $response);
* $this->assertSame($response->getId(), $pet_id);
* $this->assertSame($response->getName(), 'PHP Unit Test');
* $this->assertSame($response->getPhotoUrls()[0], 'http://test_php_unit_test.com');
*
* // category is type "object"
* $this->assertInternalType('array', $response->getCategory());
* $this->assertSame($response->getCategory()['id'], $pet_id);
* $this->assertSame($response->getCategory()['name'], 'test php category');
*
* $this->assertSame($response->getTags()[0]->getId(), $pet_id);
* $this->assertSame($response->getTags()[0]->getName(), 'test php tag');
* }
*/
// test getPetByIdWithHttpInfo with a Pet object (id 10005)
public function testGetPetByIdWithHttpInfo()
{
// initialize the API client without host
$petId = 10005; // ID of pet that needs to be fetched
/** @var $pet Pet */
list($pet, $status_code, $response_headers) = $this->api->getPetByIdWithHttpInfo($petId);
$this->assertSame($petId, $pet->getId());
$this->assertSame('PHP Unit Test', $pet->getName());
$this->assertSame($petId, $pet->getCategory()->getId());
$this->assertSame('test php category', $pet->getCategory()->getName());
$this->assertSame($petId, $pet->getTags()[0]->getId());
$this->assertSame('test php tag', $pet->getTags()[0]->getName());
$this->assertSame(200, $status_code);
$this->assertSame(['application/json'], $response_headers['Content-Type']);
}
public function testFindPetByStatus()
{
$response = $this->api->findPetsByStatus('available');
$this->assertGreaterThan(0, count($response)); // at least one object returned
$this->assertInstanceOf(Pet::class, $response[0]); // verify the object is Pet
foreach ($response as $pet) {
$this->assertSame('available', $pet->getStatus());
}
$response = $this->api->findPetsByStatus('unknown_and_incorrect_status');
$this->assertCount(0, $response);
}
public function testUpdatePet()
{
$petId = 10001;
$updatedPet = new Model\Pet;
$updatedPet->setId($petId);
$updatedPet->setName('updatePet');
$updatedPet->setStatus('pending');
$result = $this->api->updatePet($updatedPet);
$this->assertNull($result);
// verify updated Pet
$result = $this->api->getPetById($petId);
$this->assertSame($petId, $result->getId());
$this->assertSame('pending', $result->getStatus());
$this->assertSame('updatePet', $result->getName());
}
// test updatePetWithFormWithHttpInfo and verify by the "name" of the response
public function testUpdatePetWithFormWithHttpInfo()
{
$petId = 10001; // ID of pet that needs to be fetched
// update Pet (form)
list($update_response, $status_code, $http_headers) = $this->api->updatePetWithFormWithHttpInfo(
$petId,
'update pet with form with http info'
);
// return nothing (void)
$this->assertNull($update_response);
$this->assertSame(200, $status_code);
$this->assertSame(['application/json'], $http_headers['Content-Type']);
$response = $this->api->getPetById($petId);
$this->assertSame($petId, $response->getId());
$this->assertSame('update pet with form with http info', $response->getName());
}
// test updatePetWithForm and verify by the "name" and "status" of the response
public function testUpdatePetWithForm()
{
$pet_id = 10001; // ID of pet that needs to be fetched
$result = $this->api->updatePetWithForm($pet_id, 'update pet with form', 'sold');
// return nothing (void)
$this->assertNull($result);
$response = $this->api->getPetById($pet_id);
$this->assertSame($pet_id, $response->getId());
$this->assertSame('update pet with form', $response->getName());
$this->assertSame('sold', $response->getStatus());
}
// test addPet and verify by the "id" and "name" of the response
public function testAddPet()
{
$new_pet_id = 10005;
$newPet = new Model\Pet;
$newPet->setId($new_pet_id);
$newPet->setName("PHP Unit Test 2");
// add a new pet (model)
$add_response = $this->api->addPet($newPet);
// return nothing (void)
$this->assertNull($add_response);
// verify added Pet
$response = $this->api->getPetById($new_pet_id);
$this->assertSame($new_pet_id, $response->getId());
$this->assertSame('PHP Unit Test 2', $response->getName());
}
/*
* comment out as we've removed invalid endpoints from the spec, we'll introduce something
* similar in the future when we've time to update the petstore server
*
// test addPetUsingByteArray and verify by the "id" and "name" of the response
public function testAddPetUsingByteArray()
{
// initialize the API client
$config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new ApiClient($config);
$new_pet_id = 10005;
$new_pet = new Model\Pet;
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test 3");
// new tag
$tag= new Model\Tag;
$tag->setId($new_pet_id); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Model\Category;
$category->setId($new_pet_id); // use the same id as pet
$category->setName("test php category");
$new_pet->setTags(array($tag));
$new_pet->setCategory($category);
$pet_api = new Api\PetApi($api_client);
// add a new pet (model)
$object_serializer = new ObjectSerializer();
$pet_json_string = json_encode($object_serializer->sanitizeForSerialization($new_pet));
$add_response = $pet_api->addPetUsingByteArray($pet_json_string);
// return nothing (void)
$this->assertSame($add_response, NULL);
// verify added Pet
$response = $pet_api->getPetById($new_pet_id);
$this->assertSame($response->getId(), $new_pet_id);
$this->assertSame($response->getName(), 'PHP Unit Test 3');
}
*/
// test upload file
public function testUploadFile()
{
// upload file
$pet_id = 10001;
$response = $this->api->uploadFile($pet_id, 'test meta', __DIR__ . '/../composer.json');
// return ApiResponse
$this->assertInstanceOf(ApiResponse::class, $response);
}
/*
* comment out as we've removed invalid endpoints from the spec, we'll introduce something
* similar in the future when we've time to update the petstore server
*
// test byte array response
public function testGetPetByIdWithByteArray()
{
// initialize the API client
$config = new Configuration();
$config->setHost('http://petstore.swagger.io/v2');
$api_client = new APIClient($config);
$pet_api = new Api\PetApi($api_client);
// test getPetByIdWithByteArray
$pet_id = 10005;
$bytes = $pet_api->petPetIdtestingByteArraytrueGet($pet_id);
$json = json_decode($bytes, true);
$this->assertInternalType("string", $bytes);
$this->assertSame($json['id'], $pet_id);
// not testing name as it's tested by addPetUsingByteArray
//$this->assertSame($json['name'], 'PHP Unit Test');
$this->assertSame($json['category']['id'], $pet_id);
$this->assertSame($json['category']['name'], 'test php category');
$this->assertSame($json['tags'][0]['id'], $pet_id);
$this->assertSame($json['tags'][0]['name'], 'test php tag');
}
*/
// test empty object serialization
public function testEmptyPetSerialization()
{
$new_pet = new Model\Pet;
// the empty object should be serialised to {}
$this->assertSame("{}", "$new_pet");
}
// test inheritance in the model
public function testInheritance()
{
$new_dog = new Model\Dog;
// the object should be an instance of the derived class
$this->assertInstanceOf('OpenAPI\Client\Model\Dog', $new_dog);
// the object should also be an instance of the parent class
$this->assertInstanceOf('OpenAPI\Client\Model\Animal', $new_dog);
}
// test inheritance constructor is working with data
// initialization
public function testInheritanceConstructorDataInitialization()
{
// initialize the object with data in the constructor
$data = array(
'class_name' => 'Dog',
'breed' => 'Great Dane'
);
$new_dog = new Model\Dog($data);
// the property on the derived class should be set
$this->assertSame('Great Dane', $new_dog->getBreed());
// the property on the parent class should be set
$this->assertSame('Dog', $new_dog->getClassName());
}
// test if discriminator is initialized automatically
public function testDiscriminatorInitialization()
{
$new_dog = new Model\Dog();
$this->assertSame('Dog', $new_dog->getClassName());
}
// test if ArrayAccess interface works
public function testArrayStuff()
{
// create an array of Animal
$farm = array();
// add some animals to the farm to make sure the ArrayAccess
// interface works
$farm[] = new Model\Dog();
$farm[] = new Model\Cat();
$farm[] = new Model\Animal();
// assert we can look up the animals in the farm by array
// indices (let's try a random order)
$this->assertInstanceOf('OpenAPI\Client\Model\Cat', $farm[1]);
$this->assertInstanceOf('OpenAPI\Client\Model\Dog', $farm[0]);
$this->assertInstanceOf('OpenAPI\Client\Model\Animal', $farm[2]);
// let's try to `foreach` the animals in the farm and let's
// try to use the objects we loop through
foreach ($farm as $animal) {
$this->assertContains($animal->getClassName(), array('Dog', 'Cat', 'Animal'));
$this->assertInstanceOf('OpenAPI\Client\Model\Animal', $animal);
}
}
// test if default values works
public function testDefaultValues()
{
// add some animals to the farm to make sure the ArrayAccess
// interface works
$dog = new Model\Dog();
$animal = new Model\Animal();
// assert we can look up the animals in the farm by array
// indices (let's try a random order)
$this->assertSame('red', $dog->getColor());
$this->assertSame('red', $animal->getColor());
}
/**
* test invalid argument
*/
public function testInvalidArgument()
{
// the argument is required, and we must specify one or some from 'available', 'pending', 'sold'
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Missing the required parameter $status when calling findPetsByStatus');
$this->api->findPetsByStatus([]);
}
public function testObjectInFormData()
{
$category = (new Model\Category())
->setId(12345)
->setName("Category_Name");
$tags_1 = (new Model\Tag())
->setId(12345)
->setName("tag_1");
$tags_2 = (new Model\Tag())
->setId(98765)
->setName("tag_2");
/** @var Model\Tag[] $tags */
$tags = [
$tags_1,
$tags_2,
];
$pet_id = 56;
$name = "My pet name";
$photo_urls = [
"https://example.com/picture_1.jpg",
"https://example.com/picture_2.jpg",
];
$id = 12345;
$status = Model\Pet::STATUS_AVAILABLE;
$file = new \SplFileObject(__DIR__ . '/../.openapi-generator/VERSION');
$multiple_files = [
$file,
$file,
];
$request = (new Api\PetApi())->uploadImageFullFormDataRequest(
$pet_id,
$name,
$photo_urls,
$id,
$category,
$tags,
$status,
$file,
$multiple_files,
);
$contents = $request->getBody()->getContents();
$this->assertBodyContents('name', $name, $contents);
$this->assertBodyContents('photoUrls[0]', $photo_urls[0], $contents);
$this->assertBodyContents('photoUrls[1]', $photo_urls[1], $contents);
$this->assertBodyContents('category[id]', $category->getId(), $contents);
$this->assertBodyContents('category[name]', $category->getName(), $contents);
$this->assertBodyContents('tags[0][id]', $tags[0]->getId(), $contents);
$this->assertBodyContents('tags[0][name]', $tags[0]->getName(), $contents);
$this->assertBodyContents('tags[1][id]', $tags[1]->getId(), $contents);
$this->assertBodyContents('tags[1][name]', $tags[1]->getName(), $contents);
$this->assertBodyContents('status', $status, $contents);
}
private function assertBodyContents(
string $name,
mixed $value,
string $contents,
) {
$length = strlen((string) ($value));
$contents = implode("\n", array_map('trim', explode("\n", $contents)));
$expected = <<<END
Content-Disposition: form-data; name="{$name}"
Content-Length: {$length}
{$value}
END;
$this->assertStringContainsString($expected, $contents);
}
// Disabled as currently we don't have any endpoint that would return file
// For testing I just replaced url and return type in Api method.
// public function testDownloadingLargeFile()
// {
// $petId = 10005;
// $config = new Configuration();
// $config->setHost('https://getcomposer.org');
// $api = new PetApi(new Client(), $config);
// $result = $api->getPetById($petId);
// $this->assertInstanceOf(\SplFileObject::class, $result);
// var_dump([
// 'peak mem (MiB)' => memory_get_peak_usage(true)/1024/1024,
// 'file size (MiB)' => $result->getSize()/1024/1024,
// 'path' => sys_get_temp_dir() . '/' . $result->getFilename()
// ]);
// }
}