1
|
|
|
<?php |
2
|
|
|
namespace Tpg\ExtjsBundle\Tests\Command\ORM; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManager; |
5
|
|
|
use Test\TestBundle\Entity\Car; |
6
|
|
|
use Test\TestBundle\Entity\CarOwner; |
7
|
|
|
|
8
|
|
|
class GeneratedRestControllerAssociationTest extends BaseTestGeneratedRestController { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var CarOwner |
12
|
|
|
*/ |
13
|
|
|
protected $owner; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var CarOwner |
17
|
|
|
*/ |
18
|
|
|
protected $altOwner; |
19
|
|
|
|
20
|
|
|
protected function setUp() { |
21
|
|
|
parent::setUp(); |
22
|
|
|
/** @var EntityManager $manager */ |
23
|
|
|
$manager = $this->client->getContainer()->get("doctrine.orm.default_entity_manager"); |
24
|
|
|
$owner = new CarOwner(); |
25
|
|
|
$owner->setName("james"); |
26
|
|
|
foreach($this->records as $record) { |
27
|
|
|
$owner->addCar($record); |
28
|
|
|
} |
29
|
|
|
$manager->persist($owner); |
30
|
|
|
$altOwner = new CarOwner(); |
31
|
|
|
$altOwner->setName("david"); |
32
|
|
|
$manager->persist($altOwner); |
33
|
|
|
$manager->flush(); |
34
|
|
|
$this->owner = $owner; |
35
|
|
|
$this->altOwner = $altOwner; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function tearDown() { |
39
|
|
|
parent::tearDown(); |
40
|
|
|
/** @var EntityManager $manager */ |
41
|
|
|
$manager = $this->createClient()->getContainer()->get("doctrine.orm.default_entity_manager"); |
42
|
|
|
$manager->createQueryBuilder()->delete('Test\TestBundle\Entity\CarOwner', 'co')->getQuery()->execute(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testGetWithAssociation() { |
|
|
|
|
46
|
|
|
$filter = json_encode(array( |
47
|
|
|
array('property'=>'name','value'=>'Ford') |
48
|
|
|
)); |
49
|
|
|
$this->client->request('GET', '/cars.json?filter='.$filter); |
50
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true); |
51
|
|
|
$this->assertArrayHasKey('carOwner', $content[0]); |
52
|
|
|
$this->assertInternalType('array', $content[0]['carOwner']); |
53
|
|
|
$this->assertEquals($this->owner->getId(), $content[0]['carOwner']['id']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testPostWithAssociation() { |
57
|
|
|
$this->client->request('POST', '/cars.json', array(), array(), array(), json_encode(array( |
58
|
|
|
'name'=>'Toyota', |
59
|
|
|
'plateNumber'=>'BBC 234', |
60
|
|
|
'carOwner' => array( |
61
|
|
|
'id' => $this->owner->getId(), |
62
|
|
|
), |
63
|
|
|
))); |
64
|
|
|
$this->assertEquals("201", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
65
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
66
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
67
|
|
|
/** @var Car $car */ |
68
|
|
|
$car = $repo->find($record['id']); |
69
|
|
|
$this->assertEquals($this->owner, $car->getCarOwner()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testPostWithNewAssociation() { |
|
|
|
|
73
|
|
|
$this->client->request('POST', '/cars.json', array(), array(), array(), json_encode(array( |
74
|
|
|
'name'=>'Toyota', |
75
|
|
|
'plateNumber'=>'BBC 234', |
76
|
|
|
'carOwner' => array( |
77
|
|
|
'name'=>'Terry' |
78
|
|
|
), |
79
|
|
|
))); |
80
|
|
|
$this->assertEquals("201", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
81
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
82
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
83
|
|
|
/** @var Car $car */ |
84
|
|
|
$car = $repo->find($record['id']); |
85
|
|
|
$this->assertEquals('Terry', $car->getCarOwner()->getName()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testPostWithNoAssociation() { |
89
|
|
|
$this->client->request('POST', '/cars.json', array(), array(), array(), json_encode(array( |
90
|
|
|
'name'=>'Toyota', |
91
|
|
|
'plateNumber'=>'BBC 234', |
92
|
|
|
))); |
93
|
|
|
$this->assertEquals("201", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
94
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
95
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
96
|
|
|
/** @var Car $car */ |
97
|
|
|
$car = $repo->find($record['id']); |
98
|
|
|
$this->assertNull($car->getCarOwner()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function testPutWithSameAssociation() { |
|
|
|
|
102
|
|
|
$record = $this->records[0]; |
103
|
|
|
$this->client->request('PUT', '/cars/'.$record->getId().'.json', array(), array(), array(), json_encode(array( |
104
|
|
|
'name'=>'Mazda', |
105
|
|
|
'plateNumber'=>'AA00', |
106
|
|
|
'carOwner' => array( |
107
|
|
|
'id' => $this->owner->getId() |
108
|
|
|
) |
109
|
|
|
))); |
110
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
111
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
112
|
|
|
$car = $repo->find($record->getId()); |
113
|
|
|
$this->assertEquals($this->owner, $car->getCarOwner()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function testPutWithNoAssocation() { |
|
|
|
|
117
|
|
|
$record = $this->records[0]; |
118
|
|
|
$this->client->request('PUT', '/cars/'.$record->getId().'.json', array(), array(), array(), json_encode(array( |
119
|
|
|
'name'=>'Mazda', |
120
|
|
|
'plateNumber'=>'AA00' |
121
|
|
|
))); |
122
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
123
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
124
|
|
|
/** @var Car $car */ |
125
|
|
|
$car = $repo->find($record['id']); |
126
|
|
|
$this->assertNull($car->getCarOwner()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testPutWithDifferentAssociation() { |
|
|
|
|
130
|
|
|
$record = $this->records[0]; |
131
|
|
|
$this->client->request('PUT', '/cars/'.$record->getId().'.json', array(), array(), array(), json_encode(array( |
132
|
|
|
'name'=>'Mazda', |
133
|
|
|
'plateNumber'=>'AA00', |
134
|
|
|
'carOwner' => array( |
135
|
|
|
'id' => $this->altOwner->getId() |
136
|
|
|
) |
137
|
|
|
))); |
138
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
139
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
140
|
|
|
$car = $repo->find($record->getId()); |
141
|
|
|
$this->assertEquals($this->altOwner, $car->getCarOwner()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function testPatchWithDifferentAssociation() { |
|
|
|
|
145
|
|
|
$this->client->request('PATCH', '/cars/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array( |
146
|
|
|
'carOwner'=>array( |
147
|
|
|
'id'=>$this->altOwner->getId() |
148
|
|
|
) |
149
|
|
|
))); |
150
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode()); |
151
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
152
|
|
|
$car = $repo->find($this->records[0]->getId()); |
153
|
|
|
$this->assertEquals($this->altOwner, $car->getCarOwner()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public function testPatchWithNoAssocation() { |
|
|
|
|
157
|
|
|
$this->client->request('PATCH', '/cars/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array( |
158
|
|
|
'carOwner'=>null |
159
|
|
|
))); |
160
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode()); |
161
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
162
|
|
|
$car = $repo->find($this->records[0]->getId()); |
163
|
|
|
$this->assertNull($car->getCarOwner()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testPatchWithDifferentHasManyAssociation() { |
167
|
|
|
$repo = $this->client->getContainer()->get('doctrine.orm.default_entity_manager')->getRepository('TestTestBundle:Car'); |
168
|
|
|
/** @var Car $car */ |
169
|
|
|
$car = $repo->find($this->records[0]->getId()); |
170
|
|
|
$this->assertEquals(1, $car->getRelatedCars()->count()); |
171
|
|
|
$this->client->request('PATCH', '/cars/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array( |
172
|
|
|
'relatedCars'=>array( |
173
|
|
|
array('id'=>$this->records[1]->getId()) |
174
|
|
|
) |
175
|
|
|
))); |
176
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode()); |
177
|
|
|
/** @var Car $car */ |
178
|
|
|
$car = $repo->find($this->records[0]->getId()); |
179
|
|
|
$this->assertEquals(2, $car->getRelatedCars()->count()); |
180
|
|
|
} |
181
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.