Completed
Pull Request — master (#33)
by Daniel
16:52 queued 13:40
created

ObjectTest::testCollectionTypeArrayKeys()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional;
4
5
use Doctrine\ODM\PHPCR\ChildrenCollection;
6
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Article;
7
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\ArticleWithRestrictedChildren;
8
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Image;
9
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\ImageNotAssignedGenerator;
10
11
class ObjectTest extends PhpcrOdmTestCase
12
{
13
    private $documentManager;
14
    private $updater;
15
16 View Code Duplication
    public function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
17
    {
18
        $container = $this->getContainer([
19
            'mapping' => [
20
                Article::class => [
21
                    'fields' => [
22
                        'slideshow' => [
23
                            'type' => 'collection',
24
                            'options' => [
25
                                'field' => 'image',
26
                            ],
27
                        ],
28
                        'objectReferences' => [
29
                            'type' => 'collection',
30
                            'options' => [
31
                                'field' => 'object_reference',
32
                            ],
33
                        ],
34
                    ],
35
                ],
36
            ],
37
        ]);
38
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
39
        $this->initPhpcr($this->documentManager);
40
        $this->updater = $container->get('psi_content_type.storage.doctrine.phpcr_odm.collection_updater');
41
    }
42
43
    /**
44
     * It should persist object colletions.
45
     */
46
    public function testCollectionType()
47
    {
48
        $article = $this->createArticleSlideshow();
49
        $this->documentManager->persist($article);
50
        $this->updater->update($this->documentManager, $article);
51
        $this->documentManager->flush();
52
        $this->documentManager->clear();
53
54
        $article = $this->documentManager->find(null, '/test/article');
55
        $slideshow = $article->slideshow;
56
        $this->assertInstanceOf(ChildrenCollection::class, $slideshow);
57
        $slideshow = iterator_to_array($slideshow);
58
        $this->assertCount(3, $slideshow);
59
60
        $image1 = array_shift($slideshow);
61
        $image2 = array_shift($slideshow);
62
        $image3 = array_shift($slideshow);
63
64
        $this->assertEquals('/path/to/image1', $image1->path);
65
        $this->assertEquals('/path/to/image2', $image2->path);
66
        $this->assertEquals('/path/to/image3', $image3->path);
67
    }
68
69
    /**
70
     * It should update an object collection.
71
     */
72
    public function testCollectionTypeUpdate()
73
    {
74
        $article = $this->createArticleSlideshow();
75
        $this->updater->update($this->documentManager, $article);
76
        $this->documentManager->persist($article);
77
        $this->documentManager->flush();
78
        $this->documentManager->clear();
79
80
        $image = $this->createImage('/path/to/image7', 100, 200, 'image/jpeg');
81
82
        $article = $this->documentManager->find(null, '/test/article');
83
        $article->slideshow->add($image);
84
        $this->assertCount(4, iterator_to_array($article->slideshow));
85
86
        $this->documentManager->persist($article);
87
        $this->updater->update($this->documentManager, $article);
88
        $this->documentManager->flush();
89
        $this->documentManager->clear();
90
        $article = $this->documentManager->find(null, '/test/article');
91
92
        $slideshow = iterator_to_array($article->slideshow);
93
        $this->assertCount(4, $slideshow);
94
    }
95
96
    /**
97
     * Children should be removed from collections.
98
     *
99
     * @depends testCollectionType
100
     */
101
    public function testCollectionTypeChildrenRemoved()
102
    {
103
        $article = $this->createArticleSlideshow();
104
        $this->updater->update($this->documentManager, $article);
105
        $this->documentManager->persist($article);
106
        $this->documentManager->flush();
107
        $this->documentManager->clear();
108
109
        $article = $this->documentManager->find(null, '/test/article');
110
        $slideshow = $article->slideshow;
111
        $slideshow->removeElement($slideshow->first());
112
        $slideshow->removeElement($slideshow->first());
113
114
        $this->documentManager->persist($article);
115
        $this->documentManager->flush();
116
        $this->documentManager->clear();
117
118
        $image0 = $this->documentManager->find(null, '/test/article/psict:slideshow-0');
119
        $image1 = $this->documentManager->find(null, '/test/article/psict:slideshow-1');
120
        $image2 = $this->documentManager->find(null, '/test/article/psict:slideshow-2');
121
122
        $this->assertNull($image0);
123
        $this->assertNull($image1);
124
        $this->assertNotNull($image2);
125
    }
126
127
    /**
128
     * It should not preserve array keys.
129
     */
130
    public function testCollectionTypeArrayKeys()
131
    {
132
        $article = new Article();
133
        $article->id = '/test/article';
134
        $article->title = 'Foo';
135
        $article->date = new \DateTime();
136
137
        $image0 = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
138
        $image1 = $this->createImage('/path/to/image2', 100, 200, 'image/jpeg');
139
140
        $article->slideshow = [
141
            'image_0' => $image0,
142
            'image_1' => $image1,
143
        ];
144
145
        $this->documentManager->persist($article);
146
        $this->updater->update($this->documentManager, $article);
147
        $this->documentManager->flush();
148
        $this->documentManager->clear();
149
150
        $image0 = $this->documentManager->find(null, '/test/article/psict:slideshow-0');
151
        $image1 = $this->documentManager->find(null, '/test/article/psict:slideshow-1');
152
153
        $this->assertNotNull($image0);
154
        $this->assertNotNull($image1);
155
    }
156
157
    /**
158
     * It should automatically add the child class to the list of valid children when
159
     * children are defined.
160
     */
161
    public function testCollectionAddToValidChildren()
162
    {
163
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
164
        $image->id = '/test/image';
165
        $article = new ArticleWithRestrictedChildren();
166
        $article->id = '/test/article';
167
        $article->title = 'Foo';
168
        $article->date = new \DateTime();
0 ignored issues
show
Bug introduced by
The property date does not seem to exist in Psi\Bridge\ContentType\D...eWithRestrictedChildren.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
169
        $article->image = $image;
170
171
        $this->documentManager->persist($article);
172
        $this->documentManager->flush();
173
        $this->documentManager->clear();
174
175
        $this->documentManager->find(null, '/test/article');
176
    }
177
178
    /**
179
     * It should throw an exception when persisting a collection and the "updater" has not been invoked on
180
     * the collection.
181
     *
182
     * @expectedException \InvalidArgumentException
183
     * @expectedExceptionMessage It is currently necessary to envoke the CollectionIdentifierUpdater on all documents (at least those which have collections) before they are persisted.
184
     */
185
    public function testCollectionPersistNoUpdater()
186
    {
187
        $article = $this->createArticleSlideshow();
188
        $this->documentManager->persist($article);
189
        $this->documentManager->flush();
190
        $this->documentManager->clear();
191
    }
192
193
    /**
194
     * It should throw an exception when a document in a collection does not have the "ASSIGNED" ID generator.
195
     *
196
     * @expectedException \InvalidArgumentException
197
     * @expectedExceptionMessage Currently, all documents which belong to a mapped collection must use the assigned ID generator strategy
198
     */
199
    public function testCollectionPersistNoAssignedGenerator()
200
    {
201
        $image1 = new ImageNotAssignedGenerator();
202
        $article = new Article();
203
        $article->id = '/test/article';
204
        $article->slideshow = [$image1];
205
        $this->updater->update($this->documentManager, $article);
206
        $this->documentManager->persist($article);
207
        $this->documentManager->flush();
208
    }
209
210
    /**
211
     * It should store arrays of references.
212
     */
213
    public function testStoreArrayOfReferences()
214
    {
215
        $article = new Article();
216
        $article->id = '/test/article';
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->objectReferences = [$article1, $article2];
229
        $this->documentManager->persist($article);
230
        $this->documentManager->flush();
231
        $this->documentManager->clear();
232
233
        $article = $this->documentManager->find(null, '/test/article');
234
        $this->assertCount(2, $article->objectReferences);
235
        $this->assertEquals('/test/article1', $article->objectReferences[0]->id);
236
        $this->assertEquals('/test/article2', $article->objectReferences[1]->id);
237
    }
238
239 View Code Duplication
    private function createArticleSlideshow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
240
    {
241
        $article = new Article();
242
        $article->id = '/test/article';
243
244
        $image1 = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
245
        $image2 = $this->createImage('/path/to/image2', 100, 200, 'image/jpeg');
246
        $image3 = $this->createImage('/path/to/image3', 100, 200, 'image/jpeg');
247
248
        $article->slideshow = [
249
            $image1,
250
            $image2,
251
            $image3,
252
        ];
253
254
        return $article;
255
    }
256
}
257