Completed
Push — master ( 96d434...f2324b )
by Daniel
04:52 queued 55s
created

tests/Functional/CollectionType/ObjectTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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