|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Dev\Generator; |
|
10
|
|
|
|
|
11
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Dev\Document\SwaggerDocumentTest; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Dev\Generator\ResourceGenerator; |
|
14
|
|
|
use KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\PetStoreBundle; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author John Kleijn <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResourceGeneratorJmsSerializerCompatibilityTest extends \PHPUnit_Framework_TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
protected function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
$bundle = new PetStoreBundle(); |
|
24
|
|
|
$document = SwaggerDocumentTest::getPetStoreDocument(); |
|
25
|
|
|
$generator = new ResourceGenerator(); |
|
26
|
|
|
$generator->setSkeletonDirs('src/Dev/Resources/skeleton'); |
|
|
|
|
|
|
27
|
|
|
$generator->generate($bundle, $document, 'Model\Jms'); |
|
28
|
|
|
|
|
29
|
|
|
require_once $bundle->getPath() . '/Model/Jms/Pet.php'; |
|
30
|
|
|
require_once $bundle->getPath() . '/Model/Jms/Tag.php'; |
|
31
|
|
|
require_once $bundle->getPath() . '/Model/Jms/Category.php'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @test |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
public function canSerializeAPet() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$pet = new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Pet(); |
|
40
|
|
|
$pet |
|
41
|
|
|
->setId(1234567) |
|
42
|
|
|
->setName('doggie') |
|
43
|
|
|
->setPhotourls(['/a/b/c', '/d/e/f']) |
|
44
|
|
|
->setTags([ |
|
45
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Tag())->setName('purebreeds'), |
|
46
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Tag())->setName('puppies') |
|
47
|
|
|
]) |
|
48
|
|
|
->setCategory( |
|
49
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Category())->setName('Dogs') |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$serializer = SerializerBuilder::create()->build(); |
|
53
|
|
|
$actual = json_decode($serializer->serialize($pet, 'json'), true); |
|
54
|
|
|
$expected = [ |
|
55
|
|
|
'id' => 1234567, |
|
56
|
|
|
'category' => ['name' => 'Dogs'], |
|
57
|
|
|
'name' => 'doggie', |
|
58
|
|
|
'photo_urls' => ['/a/b/c', '/d/e/f'], |
|
59
|
|
|
'tags' => [ |
|
60
|
|
|
['name' => 'purebreeds'], |
|
61
|
|
|
['name' => 'puppies'], |
|
62
|
|
|
] |
|
63
|
|
|
|
|
64
|
|
|
]; |
|
65
|
|
|
$this->assertSame($expected, $actual); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @test |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function canDeserializeAPet() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$data = [ |
|
74
|
|
|
'id' => 1234567, |
|
75
|
|
|
'category' => ['name' => 'Dogs'], |
|
76
|
|
|
'name' => 'doggie', |
|
77
|
|
|
'photo_urls' => ['/a/b/c', '/d/e/f'], |
|
78
|
|
|
'tags' => [ |
|
79
|
|
|
['name' => 'purebreeds'], |
|
80
|
|
|
['name' => 'puppies'], |
|
81
|
|
|
] |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
$serializer = SerializerBuilder::create()->build(); |
|
85
|
|
|
$actual = $serializer->deserialize( |
|
86
|
|
|
json_encode($data), |
|
87
|
|
|
'KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Pet', |
|
88
|
|
|
'json' |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$expected = new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Pet(); |
|
92
|
|
|
$expected |
|
93
|
|
|
->setId(1234567) |
|
94
|
|
|
->setName('doggie') |
|
95
|
|
|
->setPhotourls(['/a/b/c', '/d/e/f']) |
|
96
|
|
|
->setTags([ |
|
97
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Tag())->setName('purebreeds'), |
|
98
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Tag())->setName('puppies') |
|
99
|
|
|
]) |
|
100
|
|
|
->setCategory( |
|
101
|
|
|
(new \KleijnWeb\SwaggerBundle\Tests\Functional\PetStore\Model\Jms\Category())->setName('Dogs') |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertEquals($expected, $actual); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: