1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\PHPCR\ChildrenCollection; |
6
|
|
|
use Psi\Component\ContentType\Tests\Functional\Example\Model\Article; |
7
|
|
|
use Psi\Component\ContentType\Tests\Functional\Example\Model\Image; |
8
|
|
|
use Psi\Component\ContentType\Tests\Functional\Storage\StorageTestTrait; |
9
|
|
|
|
10
|
|
|
class GeneralTest extends PhpcrOdmTestCase |
11
|
|
|
{ |
12
|
|
|
use StorageTestTrait; |
13
|
|
|
|
14
|
|
|
private $documentManager; |
15
|
|
|
private $updater; |
16
|
|
|
|
17
|
|
|
public function initGeneralArticle() |
18
|
|
|
{ |
19
|
|
|
$container = $this->getContainer([ |
20
|
|
|
'mapping' => [ |
21
|
|
|
Article::class => [ |
22
|
|
|
'fields' => [ |
23
|
|
|
'title' => [ |
24
|
|
|
'type' => 'text', |
25
|
|
|
'role' => 'title', |
26
|
|
|
], |
27
|
|
|
'integer' => [ |
28
|
|
|
'type' => 'integer', |
29
|
|
|
], |
30
|
|
|
'image' => [ |
31
|
|
|
'type' => 'image', |
32
|
|
|
'role' => 'image', |
33
|
|
|
], |
34
|
|
|
'date' => [ |
35
|
|
|
'type' => 'datetime', |
36
|
|
|
], |
37
|
|
|
'referencedImage' => [ |
38
|
|
|
'type' => 'object_reference', |
39
|
|
|
], |
40
|
|
|
'boolean' => [ |
41
|
|
|
'type' => 'checkbox', |
42
|
|
|
], |
43
|
|
|
'double' => [ |
44
|
|
|
'type' => 'range', |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
], |
48
|
|
|
], |
49
|
|
|
]); |
50
|
|
|
$this->documentManager = $container->get('doctrine_phpcr.document_manager'); |
51
|
|
|
$this->initPhpcr($this->documentManager); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function initCollectionArticle() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$container = $this->getContainer([ |
57
|
|
|
'mapping' => [ |
58
|
|
|
Article::class => [ |
59
|
|
|
'fields' => [ |
60
|
|
|
'slideshow' => [ |
61
|
|
|
'type' => 'collection', |
62
|
|
|
'shared' => [ |
63
|
|
|
'field_type' => 'image', |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
'objectReferences' => [ |
67
|
|
|
'type' => 'collection', |
68
|
|
|
'shared' => [ |
69
|
|
|
'field_type' => 'object_reference', |
70
|
|
|
], |
71
|
|
|
], |
72
|
|
|
], |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
]); |
76
|
|
|
$this->documentManager = $container->get('doctrine_phpcr.document_manager'); |
77
|
|
|
$this->initPhpcr($this->documentManager); |
78
|
|
|
$this->updater = $container->get('psi_content_type.storage.doctrine.phpcr_odm.collection_updater'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function initScalarCollectionArticle() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$container = $this->getContainer([ |
84
|
|
|
'mapping' => [ |
85
|
|
|
Article::class => [ |
86
|
|
|
'fields' => [ |
87
|
|
|
'paragraphs' => [ |
88
|
|
|
'type' => 'collection', |
89
|
|
|
'shared' => [ |
90
|
|
|
'field_type' => 'text', |
91
|
|
|
'field_options' => [], |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
'numbers' => [ |
95
|
|
|
'type' => 'collection', |
96
|
|
|
'shared' => [ |
97
|
|
|
'field_type' => 'integer', |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
], |
103
|
|
|
]); |
104
|
|
|
$this->documentManager = $container->get('doctrine_phpcr.document_manager'); |
105
|
|
|
$this->initPhpcr($this->documentManager); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testString() |
109
|
|
|
{ |
110
|
|
|
$this->initGeneralArticle(); |
111
|
|
|
|
112
|
|
|
$article = new Article(); |
113
|
|
|
$article->title = 'Hello'; |
114
|
|
|
$article = $this->persistAndReloadArticle($article); |
115
|
|
|
$this->assertEquals('Hello', $article->title); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function testInteger() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->initGeneralArticle(); |
121
|
|
|
|
122
|
|
|
$article = new Article(); |
123
|
|
|
$article->integer = 45; |
124
|
|
|
$article = $this->persistAndReloadArticle($article); |
125
|
|
|
$this->assertEquals(45, $article->integer); |
126
|
|
|
$this->assertInternalType('int', $article->integer); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testDate() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->initGeneralArticle(); |
132
|
|
|
|
133
|
|
|
$article = new Article(); |
134
|
|
|
$article->date = new \DateTime('2016-01-01 00:00:00'); |
135
|
|
|
$article = $this->persistAndReloadArticle($article); |
136
|
|
|
$this->assertEquals(new \DateTime('2016-01-01 00:00:00'), $article->date); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function testBoolean() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$this->initGeneralArticle(); |
142
|
|
|
|
143
|
|
|
$article = new Article(); |
144
|
|
|
$article->boolean = true; |
145
|
|
|
$article = $this->persistAndReloadArticle($article); |
146
|
|
|
$this->assertEquals(true, $article->boolean); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function testDouble() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$this->initGeneralArticle(); |
152
|
|
|
|
153
|
|
|
$article = new Article(); |
154
|
|
|
$article->double = 12.5; |
155
|
|
|
$article = $this->persistAndReloadArticle($article); |
156
|
|
|
$this->assertEquals(12.5, $article->double); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
View Code Duplication |
public function testObject() |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->initGeneralArticle(); |
162
|
|
|
|
163
|
|
|
$article = new Article(); |
164
|
|
|
$article->image = $this->createImage( |
165
|
|
|
'/path/to/image', 100, 200, 'image/jpeg' |
166
|
|
|
); |
167
|
|
|
$article = $this->persistAndReloadArticle($article); |
168
|
|
|
$this->assertInstanceOf(Image::class, $article->image); |
169
|
|
|
$this->assertEquals(100, $article->image->width); |
170
|
|
|
$this->assertEquals('image/jpeg', $article->image->mimetype); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testObjectCollection() |
174
|
|
|
{ |
175
|
|
|
$this->initCollectionArticle(); |
176
|
|
|
|
177
|
|
|
$article = $this->createArticleSlideshow(); |
178
|
|
|
$this->documentManager->persist($article); |
179
|
|
|
$this->updater->update($this->documentManager, $article); |
180
|
|
|
$this->documentManager->flush(); |
181
|
|
|
$this->documentManager->clear(); |
182
|
|
|
|
183
|
|
|
$article = $this->documentManager->find(null, '/test/article'); |
184
|
|
|
$slideshow = $article->slideshow; |
185
|
|
|
$this->assertInstanceOf(ChildrenCollection::class, $slideshow); |
186
|
|
|
$slideshow = iterator_to_array($slideshow); |
187
|
|
|
$this->assertCount(3, $slideshow); |
188
|
|
|
|
189
|
|
|
$image1 = array_shift($slideshow); |
190
|
|
|
$image2 = array_shift($slideshow); |
191
|
|
|
$image3 = array_shift($slideshow); |
192
|
|
|
|
193
|
|
|
$this->assertEquals('/path/to/image1', $image1->path); |
194
|
|
|
$this->assertEquals('/path/to/image2', $image2->path); |
195
|
|
|
$this->assertEquals('/path/to/image3', $image3->path); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testReference() |
199
|
|
|
{ |
200
|
|
|
$this->initGeneralArticle(); |
201
|
|
|
|
202
|
|
|
$image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg'); |
203
|
|
|
$image->id = '/test/image'; |
204
|
|
|
$article = new Article(); |
205
|
|
|
$article->referencedImage = $image; |
206
|
|
|
|
207
|
|
|
$this->persistAndReloadArticle($article); |
208
|
|
|
|
209
|
|
|
$article = $this->documentManager->find(null, '/test/article'); |
210
|
|
|
$image = $article->referencedImage; |
211
|
|
|
$this->assertInstanceOf(Image::class, $image); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testReferenceCollection() |
215
|
|
|
{ |
216
|
|
|
$this->initCollectionArticle(); |
217
|
|
|
|
218
|
|
|
$article1 = new Article(); |
219
|
|
|
$article1->id = '/test/article1'; |
220
|
|
|
|
221
|
|
|
$article2 = new Article(); |
222
|
|
|
$article2->id = '/test/article2'; |
223
|
|
|
|
224
|
|
|
$this->documentManager->persist($article1); |
225
|
|
|
$this->documentManager->persist($article2); |
226
|
|
|
$this->documentManager->flush(); |
227
|
|
|
|
228
|
|
|
$article = new Article(); |
229
|
|
|
$article->objectReferences = [$article1, $article2]; |
230
|
|
|
$article = $this->persistAndReloadArticle($article); |
231
|
|
|
|
232
|
|
|
$this->assertCount(2, $article->objectReferences); |
233
|
|
|
$this->assertEquals('/test/article1', $article->objectReferences[0]->id); |
234
|
|
|
$this->assertEquals('/test/article2', $article->objectReferences[1]->id); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
View Code Duplication |
public function testScalarCollection() |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$this->initScalarCollectionArticle(); |
240
|
|
|
|
241
|
|
|
$article = new Article(); |
242
|
|
|
$article->id = '/test/article'; |
243
|
|
|
$article->paragraphs = ['one', 'two', 'three']; |
244
|
|
|
$article = $this->persistAndReloadArticle($article); |
245
|
|
|
|
246
|
|
|
$this->assertSame(['one', 'two', 'three'], $article->paragraphs); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
View Code Duplication |
public function testIntegerCollection() |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$this->initScalarCollectionArticle(); |
252
|
|
|
|
253
|
|
|
$article = new Article(); |
254
|
|
|
$article->id = '/test/article'; |
255
|
|
|
$article->numbers = ['12', '13', '14']; |
256
|
|
|
$article = $this->persistAndReloadArticle($article); |
257
|
|
|
|
258
|
|
|
$this->assertSame([12, 13, 14], $article->numbers); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
private function persistAndReloadArticle(Article $article) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
$article->id = '/test/article'; |
264
|
|
|
$this->documentManager->persist($article); |
265
|
|
|
$this->documentManager->flush(); |
266
|
|
|
|
267
|
|
|
$this->documentManager->clear(); |
268
|
|
|
|
269
|
|
|
return $this->documentManager->find(Article::class, '/test/article'); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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.