Completed
Push — master ( 1f04bf...cc1bbe )
by Daniel
07:04 queued 04:13
created

GeneralTest::testMapping()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
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\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
                    ],
41
                ],
42
            ],
43
        ]);
44
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
45
        $this->initPhpcr($this->documentManager);
46
    }
47
48 View Code Duplication
    public function initCollectionArticle()
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...
49
    {
50
        $container = $this->getContainer([
51
            'mapping' => [
52
                Article::class => [
53
                    'fields' => [
54
                        'slideshow' => [
55
                            'type' => 'collection',
56
                            'options' => [
57
                                'field_type' => 'image',
58
                            ],
59
                        ],
60
                        'objectReferences' => [
61
                            'type' => 'collection',
62
                            'options' => [
63
                                'field_type' => 'object_reference',
64
                            ],
65
                        ],
66
                    ],
67
                ],
68
            ],
69
        ]);
70
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
71
        $this->initPhpcr($this->documentManager);
72
        $this->updater = $container->get('psi_content_type.storage.doctrine.phpcr_odm.collection_updater');
73
    }
74
75 View Code Duplication
    public function initScalarCollectionArticle()
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...
76
    {
77
        $container = $this->getContainer([
78
            'mapping' => [
79
                Article::class => [
80
                    'fields' => [
81
                        'paragraphs' => [
82
                            'type' => 'collection',
83
                            'options' => [
84
                                'field_type' => 'text',
85
                                'field_options' => [],
86
                            ],
87
                        ],
88
                        'numbers' => [
89
                            'type' => 'collection',
90
                            'options' => [
91
                                'field_type' => 'integer',
92
                            ],
93
                        ],
94
                    ],
95
                ],
96
            ],
97
        ]);
98
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
99
        $this->initPhpcr($this->documentManager);
100
    }
101
102
    public function testString()
103
    {
104
        $this->initGeneralArticle();
105
106
        $article = new Article();
107
        $article->title = 'Hello';
108
        $article = $this->persistAndReloadArticle($article);
109
        $this->assertEquals('Hello', $article->title);
110
    }
111
112 View Code Duplication
    public function testInteger()
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...
113
    {
114
        $this->initGeneralArticle();
115
116
        $article = new Article();
117
        $article->integer = 45;
118
        $article = $this->persistAndReloadArticle($article);
119
        $this->assertEquals(45, $article->integer);
120
        $this->assertInternalType('int', $article->integer);
121
    }
122
123 View Code Duplication
    public function testDate()
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...
124
    {
125
        $this->initGeneralArticle();
126
127
        $article = new Article();
128
        $article->date = new \DateTime('2016-01-01 00:00:00');
129
        $article = $this->persistAndReloadArticle($article);
130
        $this->assertEquals(new \DateTime('2016-01-01 00:00:00'), $article->date);
131
    }
132
133 View Code Duplication
    public function testObject()
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...
134
    {
135
        $this->initGeneralArticle();
136
137
        $article = new Article();
138
        $article->image = $this->createImage(
139
            '/path/to/image', 100, 200, 'image/jpeg'
140
        );
141
        $article = $this->persistAndReloadArticle($article);
142
        $this->assertInstanceOf(Image::class, $article->image);
143
        $this->assertEquals(100, $article->image->width);
144
        $this->assertEquals('image/jpeg', $article->image->mimetype);
145
    }
146
147
    public function testObjectCollection()
148
    {
149
        $this->initCollectionArticle();
150
151
        $article = $this->createArticleSlideshow();
152
        $this->documentManager->persist($article);
153
        $this->updater->update($this->documentManager, $article);
154
        $this->documentManager->flush();
155
        $this->documentManager->clear();
156
157
        $article = $this->documentManager->find(null, '/test/article');
158
        $slideshow = $article->slideshow;
159
        $this->assertInstanceOf(ChildrenCollection::class, $slideshow);
160
        $slideshow = iterator_to_array($slideshow);
161
        $this->assertCount(3, $slideshow);
162
163
        $image1 = array_shift($slideshow);
164
        $image2 = array_shift($slideshow);
165
        $image3 = array_shift($slideshow);
166
167
        $this->assertEquals('/path/to/image1', $image1->path);
168
        $this->assertEquals('/path/to/image2', $image2->path);
169
        $this->assertEquals('/path/to/image3', $image3->path);
170
    }
171
172
    public function testReference()
173
    {
174
        $this->initGeneralArticle();
175
176
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
177
        $image->id = '/test/image';
178
        $article = new Article();
179
        $article->referencedImage = $image;
180
181
        $this->persistAndReloadArticle($article);
182
183
        $article = $this->documentManager->find(null, '/test/article');
184
        $image = $article->referencedImage;
185
        $this->assertInstanceOf(Image::class, $image);
186
    }
187
188
    public function testReferenceCollection()
189
    {
190
        $this->initCollectionArticle();
191
192
        $article1 = new Article();
193
        $article1->id = '/test/article1';
194
195
        $article2 = new Article();
196
        $article2->id = '/test/article2';
197
198
        $this->documentManager->persist($article1);
199
        $this->documentManager->persist($article2);
200
        $this->documentManager->flush();
201
202
        $article = new Article();
203
        $article->objectReferences = [$article1, $article2];
204
        $article = $this->persistAndReloadArticle($article);
205
206
        $this->assertCount(2, $article->objectReferences);
207
        $this->assertEquals('/test/article1', $article->objectReferences[0]->id);
208
        $this->assertEquals('/test/article2', $article->objectReferences[1]->id);
209
    }
210
211 View Code Duplication
    public function testScalarCollection()
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...
212
    {
213
        $this->initScalarCollectionArticle();
214
215
        $article = new Article();
216
        $article->id = '/test/article';
217
        $article->paragraphs = ['one', 'two', 'three'];
218
        $article = $this->persistAndReloadArticle($article);
219
220
        $this->assertSame(['one', 'two', 'three'], $article->paragraphs);
221
    }
222
223 View Code Duplication
    public function testIntegerCollection()
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...
224
    {
225
        $this->initScalarCollectionArticle();
226
227
        $article = new Article();
228
        $article->id = '/test/article';
229
        $article->numbers = ['12', '13', '14'];
230
        $article = $this->persistAndReloadArticle($article);
231
232
        $this->assertSame([12, 13, 14], $article->numbers);
233
    }
234
235 View Code Duplication
    private function persistAndReloadArticle(Article $article)
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...
236
    {
237
        $article->id = '/test/article';
238
        $this->documentManager->persist($article);
239
        $this->documentManager->flush();
240
241
        $this->documentManager->clear();
242
243
        return $this->documentManager->find(Article::class, '/test/article');
244
    }
245
}
246