Completed
Pull Request — master (#42)
by Daniel
04:11
created

ObjectTest::createArticleSlideshow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\ArticleWithRestrictedChildren;
7
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Image;
8
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\ImageNotAssignedGenerator;
9
use Psi\Component\ContentType\Tests\Functional\Example\Model\Article;
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_type' => 'image',
26
                            ],
27
                        ],
28
                        'objectReferences' => [
29
                            'type' => 'collection',
30
                            'options' => [
31
                                'field_type' => '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 update an object collection.
45
     */
46
    public function testCollectionTypeUpdate()
47
    {
48
        $article = $this->createArticleSlideshow();
49
        $this->updater->update($this->documentManager, $article);
50
        $this->documentManager->persist($article);
51
        $this->documentManager->flush();
52
        $this->documentManager->clear();
53
54
        $image = $this->createImage('/path/to/image7', 100, 200, 'image/jpeg');
55
56
        $article = $this->documentManager->find(null, '/test/article');
57
        $article->slideshow->add($image);
58
        $this->assertCount(4, iterator_to_array($article->slideshow));
59
60
        $this->documentManager->persist($article);
61
        $this->updater->update($this->documentManager, $article);
62
        $this->documentManager->flush();
63
        $this->documentManager->clear();
64
        $article = $this->documentManager->find(null, '/test/article');
65
66
        $slideshow = iterator_to_array($article->slideshow);
67
        $this->assertCount(4, $slideshow);
68
    }
69
70
    /**
71
     * Children should be removed from collections.
72
     *
73
     * @depends testCollectionType
74
     */
75
    public function testCollectionTypeChildrenRemoved()
76
    {
77
        $article = $this->createArticleSlideshow();
78
        $this->updater->update($this->documentManager, $article);
79
        $this->documentManager->persist($article);
80
        $this->documentManager->flush();
81
        $this->documentManager->clear();
82
83
        $article = $this->documentManager->find(null, '/test/article');
84
        $slideshow = $article->slideshow;
85
        $slideshow->removeElement($slideshow->first());
86
        $slideshow->removeElement($slideshow->first());
87
88
        $this->documentManager->persist($article);
89
        $this->documentManager->flush();
90
        $this->documentManager->clear();
91
92
        $image0 = $this->documentManager->find(null, '/test/article/psict:slideshow-0');
93
        $image1 = $this->documentManager->find(null, '/test/article/psict:slideshow-1');
94
        $image2 = $this->documentManager->find(null, '/test/article/psict:slideshow-2');
95
96
        $this->assertNull($image0);
97
        $this->assertNull($image1);
98
        $this->assertNotNull($image2);
99
    }
100
101
    /**
102
     * It should not preserve array keys.
103
     */
104
    public function testCollectionTypeArrayKeys()
105
    {
106
        $article = new Article();
107
        $article->id = '/test/article';
108
        $article->title = 'Foo';
109
        $article->date = new \DateTime();
110
111
        $image0 = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
112
        $image1 = $this->createImage('/path/to/image2', 100, 200, 'image/jpeg');
113
114
        $article->slideshow = [
115
            'image_0' => $image0,
116
            'image_1' => $image1,
117
        ];
118
119
        $this->documentManager->persist($article);
120
        $this->updater->update($this->documentManager, $article);
121
        $this->documentManager->flush();
122
        $this->documentManager->clear();
123
124
        $image0 = $this->documentManager->find(null, '/test/article/psict:slideshow-0');
125
        $image1 = $this->documentManager->find(null, '/test/article/psict:slideshow-1');
126
127
        $this->assertNotNull($image0);
128
        $this->assertNotNull($image1);
129
    }
130
131
    /**
132
     * It should automatically add the child class to the list of valid children when
133
     * children are defined.
134
     */
135
    public function testCollectionAddToValidChildren()
136
    {
137
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
138
        $image->id = '/test/image';
139
        $article = new ArticleWithRestrictedChildren();
140
        $article->id = '/test/article';
141
        $article->title = 'Foo';
142
        $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...
143
        $article->image = $image;
144
145
        $this->documentManager->persist($article);
146
        $this->documentManager->flush();
147
        $this->documentManager->clear();
148
149
        $this->documentManager->find(null, '/test/article');
150
    }
151
152
    /**
153
     * It should throw an exception when persisting a collection and the "updater" has not been invoked on
154
     * the collection.
155
     *
156
     * @expectedException \InvalidArgumentException
157
     * @expectedExceptionMessage It is currently necessary to envoke the CollectionIdentifierUpdater on all documents (at least those which have collections) before they are persisted.
158
     */
159
    public function testCollectionPersistNoUpdater()
160
    {
161
        $article = $this->createArticleSlideshow();
162
        $this->documentManager->persist($article);
163
        $this->documentManager->flush();
164
        $this->documentManager->clear();
165
    }
166
167
    /**
168
     * It should throw an exception when a document in a collection does not have the "ASSIGNED" ID generator.
169
     *
170
     * @expectedException \InvalidArgumentException
171
     * @expectedExceptionMessage Currently, all documents which belong to a mapped collection must use the assigned ID generator strategy
172
     */
173
    public function testCollectionPersistNoAssignedGenerator()
174
    {
175
        $image1 = new ImageNotAssignedGenerator();
176
        $article = new Article();
177
        $article->id = '/test/article';
178
        $article->slideshow = [$image1];
179
        $this->updater->update($this->documentManager, $article);
180
        $this->documentManager->persist($article);
181
        $this->documentManager->flush();
182
    }
183
}
184