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

GeneralTest::testInteger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional;
4
5
use Psi\Component\ContentType\Tests\Functional\Example\Model\Image;
6
use Psi\Component\ContentType\Tests\Functional\Storage\StorageTestTrait;
7
use Doctrine\ODM\PHPCR\ChildrenCollection;
8
use Psi\Component\ContentType\Tests\Functional\Example\Model\Article;
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);
0 ignored issues
show
Unused Code introduced by
$article is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
205
206
        $article = $this->documentManager->find(null, '/test/article');
207
        $this->assertCount(2, $article->objectReferences);
208
        $this->assertEquals('/test/article1', $article->objectReferences[0]->id);
209
        $this->assertEquals('/test/article2', $article->objectReferences[1]->id);
210
    }
211
212 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...
213
    {
214
        $this->initScalarCollectionArticle();
215
216
        $article = new Article();
217
        $article->id = '/test/article';
218
        $article->paragraphs = ['one', 'two', 'three'];
219
        $article = $this->persistAndReloadArticle($article);
220
221
        $this->assertSame(['one', 'two', 'three'], $article->paragraphs);
222
    }
223
224 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...
225
    {
226
        $this->initScalarCollectionArticle();
227
228
        $article = new Article();
229
        $article->id = '/test/article';
230
        $article->numbers = ['12', '13', '14'];
231
        $article = $this->persistAndReloadArticle($article);
232
233
        $this->assertSame([12, 13, 14], $article->numbers);
234
    }
235
236 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...
237
    {
238
        $article->id = '/test/article';
239
        $this->documentManager->persist($article);
240
        $this->documentManager->flush();
241
242
        $this->documentManager->clear();
243
244
        return $this->documentManager->find(Article::class, '/test/article');
245
    }
246
}
247