Completed
Pull Request — master (#56)
by Daniel
04:19
created

GeneralTest::initScalarCollectionArticle()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 26
loc 26
rs 8.8571
cc 1
eloc 16
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
                        '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()
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...
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()
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...
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()
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...
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()
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...
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()
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...
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()
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...
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
160 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...
161
    {
162
        $this->initGeneralArticle();
163
164
        $article = new Article();
165
        $article->image = $this->createImage(
166
            '/path/to/image', 100, 200, 'image/jpeg'
167
        );
168
        $article = $this->persistAndReloadArticle($article);
169
        $this->assertInstanceOf(Image::class, $article->image);
170
        $this->assertEquals(100, $article->image->width);
171
        $this->assertEquals('image/jpeg', $article->image->mimetype);
172
    }
173
174
    public function testObjectCollection()
175
    {
176
        $this->initCollectionArticle();
177
178
        $article = $this->createArticleSlideshow();
179
        $this->documentManager->persist($article);
180
        $this->updater->update($this->documentManager, $article);
181
        $this->documentManager->flush();
182
        $this->documentManager->clear();
183
184
        $article = $this->documentManager->find(null, '/test/article');
185
        $slideshow = $article->slideshow;
186
        $this->assertInstanceOf(ChildrenCollection::class, $slideshow);
187
        $slideshow = iterator_to_array($slideshow);
188
        $this->assertCount(3, $slideshow);
189
190
        $image1 = array_shift($slideshow);
191
        $image2 = array_shift($slideshow);
192
        $image3 = array_shift($slideshow);
193
194
        $this->assertEquals('/path/to/image1', $image1->path);
195
        $this->assertEquals('/path/to/image2', $image2->path);
196
        $this->assertEquals('/path/to/image3', $image3->path);
197
    }
198
199
    public function testReference()
200
    {
201
        $this->initGeneralArticle();
202
203
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
204
        $image->id = '/test/image';
205
        $article = new Article();
206
        $article->referencedImage = $image;
207
208
        $this->persistAndReloadArticle($article);
209
210
        $article = $this->documentManager->find(null, '/test/article');
211
        $image = $article->referencedImage;
212
        $this->assertInstanceOf(Image::class, $image);
213
    }
214
215
    public function testReferenceCollection()
216
    {
217
        $this->initCollectionArticle();
218
219
        $article1 = new Article();
220
        $article1->id = '/test/article1';
221
222
        $article2 = new Article();
223
        $article2->id = '/test/article2';
224
225
        $this->documentManager->persist($article1);
226
        $this->documentManager->persist($article2);
227
        $this->documentManager->flush();
228
229
        $article = new Article();
230
        $article->objectReferences = [$article1, $article2];
231
        $article = $this->persistAndReloadArticle($article);
232
233
        $this->assertCount(2, $article->objectReferences);
234
        $this->assertEquals('/test/article1', $article->objectReferences[0]->id);
235
        $this->assertEquals('/test/article2', $article->objectReferences[1]->id);
236
    }
237
238 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...
239
    {
240
        $this->initScalarCollectionArticle();
241
242
        $article = new Article();
243
        $article->id = '/test/article';
244
        $article->paragraphs = ['one', 'two', 'three'];
245
        $article = $this->persistAndReloadArticle($article);
246
247
        $this->assertSame(['one', 'two', 'three'], $article->paragraphs);
248
    }
249
250 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...
251
    {
252
        $this->initScalarCollectionArticle();
253
254
        $article = new Article();
255
        $article->id = '/test/article';
256
        $article->numbers = ['12', '13', '14'];
257
        $article = $this->persistAndReloadArticle($article);
258
259
        $this->assertSame([12, 13, 14], $article->numbers);
260
    }
261
262 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...
263
    {
264
        $article->id = '/test/article';
265
        $this->documentManager->persist($article);
266
        $this->documentManager->flush();
267
268
        $this->documentManager->clear();
269
270
        return $this->documentManager->find(Article::class, '/test/article');
271
    }
272
}
273