|
1
|
|
|
<?php |
|
2
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\Functional; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Tests\Models\Embedded\Embedded; |
|
5
|
|
|
use Doctrine\Tests\Models\Embedded\Embedder; |
|
6
|
|
|
use Doctrine\Tests\Models\Embedded\Nested; |
|
7
|
|
|
|
|
8
|
|
|
class EmbedManyTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private $dm; |
|
11
|
|
|
|
|
12
|
|
|
public function setUp() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->type = 'Doctrine\Tests\Models\Embedded\Embedder'; |
|
|
|
|
|
|
15
|
|
|
$this->embeddedType = 'Doctrine\Tests\Models\Embedded\Embedded'; |
|
|
|
|
|
|
16
|
|
|
$this->dm = $this->createDocumentManager(); |
|
17
|
|
|
|
|
18
|
|
|
$document = new Embedder(); |
|
19
|
|
|
$document->id = 1; |
|
20
|
|
|
$embedded1 = new Embedded(); |
|
21
|
|
|
$embedded1->name = 'embedded 1'; |
|
22
|
|
|
$embedded2 = new Embedded(); |
|
23
|
|
|
$embedded2->name = 'embedded 2'; |
|
24
|
|
|
|
|
25
|
|
|
$document->embeds[] = $embedded1; |
|
26
|
|
|
$document->embeds[] = $embedded2; |
|
27
|
|
|
|
|
28
|
|
|
$this->dm->persist($document); |
|
29
|
|
|
$this->dm->flush(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testFind() |
|
33
|
|
|
{ |
|
34
|
|
|
$embedder = $this->dm->find($this->type, 1); |
|
35
|
|
|
$this->assertInstanceOf($this->type, $embedder); |
|
36
|
|
|
$this->assertCount(2, $embedder->embeds); |
|
37
|
|
|
$this->assertEquals('embedded 1', $embedder->embeds[0]->name); |
|
38
|
|
|
$this->assertEquals('embedded 2', $embedder->embeds[1]->name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testShouldNotSaveUnchanged() |
|
42
|
|
|
{ |
|
43
|
|
|
$listener = new PreUpdateSubscriber; |
|
44
|
|
|
$this->dm->getEventManager()->addEventListener('preUpdate', $listener); |
|
45
|
|
|
|
|
46
|
|
|
$embedder = $this->dm->find($this->type, 1); |
|
|
|
|
|
|
47
|
|
|
$this->dm->flush(); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertCount(0, $listener->eventArgs); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testSave() |
|
53
|
|
|
{ |
|
54
|
|
|
$embedder = $this->dm->find($this->type, 1); |
|
55
|
|
|
// change the first element |
|
56
|
|
|
$embedder->embeds[0]->name = 'changed 1'; |
|
57
|
|
|
// add another one |
|
58
|
|
|
$newOne = new Embedded; |
|
59
|
|
|
$newOne->name = 'new one'; |
|
60
|
|
|
$embedder->embeds[] = $newOne; |
|
61
|
|
|
$this->dm->flush(); |
|
62
|
|
|
$this->dm->clear(); |
|
63
|
|
|
|
|
64
|
|
|
$embedder = $this->dm->find($this->type, 1); |
|
65
|
|
|
$this->assertCount(3, $embedder->embeds); |
|
66
|
|
|
$this->assertEquals('new one', $embedder->embeds[2]->name); |
|
67
|
|
|
|
|
68
|
|
|
$embedder->embeds[0]->name = 'changed'; |
|
69
|
|
|
$embedder->name = 'foo'; |
|
70
|
|
|
$embedder->embeds[0]->arrayField[] = 'bar'; |
|
71
|
|
|
$this->dm->flush(); |
|
72
|
|
|
$this->dm->clear(); |
|
73
|
|
|
|
|
74
|
|
|
$embedder = $this->dm->find($this->type, 1); |
|
75
|
|
|
$this->assertCount(3, $embedder->embeds); |
|
76
|
|
|
$this->assertEquals('foo', $embedder->name); |
|
77
|
|
|
$this->assertEquals('changed', $embedder->embeds[0]->name); |
|
78
|
|
|
$this->assertCount(1, $embedder->embeds[0]->arrayField); |
|
79
|
|
|
$this->assertEquals('bar', $embedder->embeds[0]->arrayField[0]); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
public function testCreate() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$newOne = new Embedder; |
|
86
|
|
|
$newOne->id = '2'; |
|
87
|
|
|
|
|
88
|
|
|
$embedded1 = new Embedded; |
|
89
|
|
|
$embedded1->name = 'newly embedded 1'; |
|
90
|
|
|
$embedded2 = new Embedded; |
|
91
|
|
|
$embedded2->name = 'newly embedded 2'; |
|
92
|
|
|
$newOne->embeds[] = $embedded1; |
|
93
|
|
|
$newOne->embeds[] = $embedded2; |
|
94
|
|
|
|
|
95
|
|
|
$this->dm->persist($newOne); |
|
96
|
|
|
$this->dm->flush(); |
|
97
|
|
|
$this->dm->clear(); |
|
98
|
|
|
|
|
99
|
|
|
$newOne = null; |
|
100
|
|
|
$this->assertNull($newOne); |
|
101
|
|
|
$newOne = $this->dm->find($this->type, 2); |
|
102
|
|
|
$this->assertNotNull($newOne); |
|
103
|
|
|
$this->assertCount(2, $newOne->embeds); |
|
104
|
|
|
$this->assertEquals('newly embedded 1', $newOne->embeds[0]->name); |
|
105
|
|
|
$this->assertEquals('newly embedded 2', $newOne->embeds[1]->name); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
public function testAssocCreate() |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$newOne = new Embedder; |
|
111
|
|
|
$newOne->id = '2'; |
|
112
|
|
|
|
|
113
|
|
|
$embedded1 = new Embedded; |
|
114
|
|
|
$embedded1->name = 'newly embedded 1'; |
|
115
|
|
|
$embedded2 = new Embedded; |
|
116
|
|
|
$embedded2->name = 'newly embedded 2'; |
|
117
|
|
|
$newOne->embeds['one'] = $embedded1; |
|
118
|
|
|
$newOne->embeds['two'] = $embedded2; |
|
119
|
|
|
|
|
120
|
|
|
$this->dm->persist($newOne); |
|
121
|
|
|
$this->dm->flush(); |
|
122
|
|
|
$this->dm->clear(); |
|
123
|
|
|
|
|
124
|
|
|
$newOne = null; |
|
125
|
|
|
$this->assertNull($newOne); |
|
126
|
|
|
$newOne = $this->dm->find($this->type, 2); |
|
127
|
|
|
$this->assertNotNull($newOne); |
|
128
|
|
|
$this->assertCount(2, $newOne->embeds); |
|
129
|
|
|
$this->assertEquals('newly embedded 1', $newOne->embeds['one']->name); |
|
130
|
|
|
$this->assertEquals('newly embedded 2', $newOne->embeds['two']->name); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testMetadataMapping() |
|
134
|
|
|
{ |
|
135
|
|
|
$metadata = $this->dm->getClassMetadata($this->type); |
|
136
|
|
|
$this->assertArrayHasKey('embeds', $metadata->fieldMappings); |
|
137
|
|
|
$mapping = $metadata->fieldMappings['embeds']; |
|
138
|
|
|
$this->assertEquals('mixed', $mapping['type']); |
|
139
|
|
|
$this->assertEquals('many', $mapping['embedded']); |
|
140
|
|
|
$this->assertEquals($this->embeddedType, $mapping['targetDocument']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// TODO testEmbeddedWithNonMappedData |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
View Code Duplication |
class PreUpdateSubscriber implements \Doctrine\Common\EventSubscriber |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
public $eventArgs = array(); |
|
151
|
|
|
public function getSubscribedEvents() |
|
152
|
|
|
{ |
|
153
|
|
|
return array(\Doctrine\ODM\CouchDB\Event::preUpdate); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function preUpdate(\Doctrine\ODM\CouchDB\Event\LifecycleEventArgs $args) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->eventArgs[] = $args; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: