|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tpg\ExtjsBundle\Tests\Command\ODM; |
|
4
|
|
|
|
|
5
|
|
|
use Test\TestBundle\Document\Order; |
|
6
|
|
|
|
|
7
|
|
|
class GeneratedRestControllerReferenceAssociationTest extends BaseTestGeneratedRestController { |
|
8
|
|
|
public function testGetWithAssociation() { |
|
9
|
|
|
$filter = json_encode(array( |
|
10
|
|
|
array('property'=>'name','value'=>'Invoice 1') |
|
11
|
|
|
)); |
|
12
|
|
|
$this->client->request('GET', '/orders.json?filter='.$filter); |
|
13
|
|
|
$content = json_decode($this->client->getResponse()->getContent(), true); |
|
14
|
|
|
$this->assertArrayHasKey('client', $content[0]); |
|
15
|
|
|
$this->assertInternalType('array', $content[0]['client']); |
|
16
|
|
|
$this->assertEquals($this->records['0']->getClient()->getId(), $content[0]['client']['id']); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testPostWithNewAssociation() { |
|
20
|
|
|
$this->client->request('POST', '/orders.json', array(), array(), array(), json_encode(array( |
|
21
|
|
|
'name'=>'Invoice New', |
|
22
|
|
|
'totalPrice'=>1.99, |
|
23
|
|
|
'client' => array( |
|
24
|
|
|
'firstName' => 'James', |
|
25
|
|
|
'lastName' => 'Bond', |
|
26
|
|
|
), |
|
27
|
|
|
))); |
|
28
|
|
|
$this->assertEquals("201", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
|
29
|
|
|
$repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order'); |
|
30
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
|
31
|
|
|
/** @var Order $order */ |
|
32
|
|
|
$order = $repo->find($record['id']); |
|
33
|
|
|
$this->assertEquals('Invoice New', $order->getName()); |
|
34
|
|
|
$this->assertNotNull($order->getClient()); |
|
35
|
|
|
$this->assertNotNull($order->getClient()->getId()); |
|
36
|
|
|
$this->assertEquals('James', $order->getClient()->getFirstName()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testPutWithDifferentAssociation() { |
|
40
|
|
|
$record = $this->records[0]; |
|
41
|
|
|
$originalClient = $record->getClient(); |
|
|
|
|
|
|
42
|
|
|
$this->client->request('PUT', '/orders/'.$record->getId().'.json', array(), array(), array(), json_encode(array( |
|
43
|
|
|
'name'=>'Invoice 1', |
|
44
|
|
|
'client' => array( |
|
45
|
|
|
'id' => $this->clientDocument->getId() |
|
46
|
|
|
), |
|
47
|
|
|
))); |
|
48
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode(), $this->client->getResponse()->getContent()); |
|
49
|
|
|
$repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order'); |
|
50
|
|
|
/** @var Order $order */ |
|
51
|
|
|
$order = $repo->find($record->getId()); |
|
52
|
|
|
$this->assertEquals($this->clientDocument->getId(), $order->getClient()->getId()); |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
public function testPutWithNoAssocation() { |
|
|
|
|
|
|
57
|
|
|
$record = $this->records[0]; |
|
58
|
|
|
$this->client->request('PUT', '/orders/'.$record->getId().'.json', array(), array(), array(), json_encode(array( |
|
59
|
|
|
'name'=>'Invoice 1' |
|
60
|
|
|
))); |
|
61
|
|
|
$repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order'); |
|
62
|
|
|
$record = json_decode($this->client->getResponse()->getContent(), true); |
|
63
|
|
|
/** @var Order $order */ |
|
64
|
|
|
$order = $repo->find($record['id']); |
|
65
|
|
|
$this->assertNull($order->getClient()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testPatchWithDifferentAssociation() { |
|
69
|
|
|
$this->client->request('PATCH', '/orders/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array( |
|
70
|
|
|
'client' => array( |
|
71
|
|
|
'id' => $this->clientDocument->getId() |
|
72
|
|
|
), |
|
73
|
|
|
))); |
|
74
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode()); |
|
75
|
|
|
$repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order'); |
|
76
|
|
|
/** @var Order $order */ |
|
77
|
|
|
$order = $repo->find($this->records[0]->getId()); |
|
78
|
|
|
$this->assertEquals($this->records[0]->getLineItems(), $order->getLineItems()); |
|
79
|
|
|
$this->assertEquals($this->clientDocument->getId(), $order->getClient()->getId()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testPatchWithNoAssocation() { |
|
83
|
|
|
$this->client->request('PATCH', '/orders/'.$this->records[0]->getId().'.json', array(), array(), array(), json_encode(array( |
|
84
|
|
|
'client'=>null |
|
85
|
|
|
))); |
|
86
|
|
|
$this->assertEquals("200", $this->client->getResponse()->getStatusCode()); |
|
87
|
|
|
$repo = $this->client->getContainer()->get('doctrine_mongodb.odm.document_manager')->getRepository('TestTestBundle:Order'); |
|
88
|
|
|
/** @var Order $order */ |
|
89
|
|
|
$order = $repo->find($this->records[0]->getId()); |
|
90
|
|
|
$this->assertEquals($this->records[0]->getLineItems(), $order->getLineItems()); |
|
91
|
|
|
$this->assertNull($order->getClient()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.