Completed
Push — master ( d11b01...1c170b )
by Daniel
17:01 queued 13:33
created

testChildAddToValidChildren()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional;
4
5
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Article;
6
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\ArticleWithRestrictedChildren;
7
use Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Image;
8
9
class ObjectUnrestrictTest extends PhpcrOdmTestCase
10
{
11
    private $documentManager;
12
    private $updater;
13
14
    public function init(array $mapping)
15
    {
16
        $container = $this->getContainer([
17
            'mapping' => $mapping,
18
        ]);
19
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
20
        $this->initPhpcr($this->documentManager);
21
        $this->updater = $container->get('psi_content_type.storage.doctrine.phpcr_odm.collection_updater');
22
    }
23
24
    /**
25
     * It should automatically allow mapped content objects as children.
26
     */
27
    public function testChildAddToValidChildren()
28
    {
29
        $this->init([
30
            ArticleWithRestrictedChildren::class => [
31
                'fields' => [
32
                    'title' => [
33
                        'type' => 'text',
34
                        'role' => 'title',
35
                    ],
36
                    'image' => [
37
                        'type' => 'image',
38
                        'role' => 'image',
39
                        'options' => [
40
                            'class' => Image::class,
41
                        ],
42
                    ],
43
                ],
44
            ],
45
        ]);
46
47
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
48
        $image->id = '/test/image';
49
        $article = new ArticleWithRestrictedChildren();
50
        $article->id = '/test/article';
51
        $article->title = 'Foo';
52
        $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...
53
        $article->image = $image;
54
55
        $this->documentManager->persist($article);
56
        $this->updater->update($this->documentManager, $article);
57
        $this->documentManager->flush();
58
        $this->documentManager->clear();
59
60
        $this->documentManager->find(null, '/test/article');
61
    }
62
63
    /**
64
     * It should automatically allow mapped collection objects as children.
65
     */
66
    public function testCollectionAddToValidChildren()
67
    {
68
        $this->init([
69
            ArticleWithRestrictedChildren::class => [
70
                'fields' => [
71
                    'title' => [
72
                        'type' => 'text',
73
                        'role' => 'title',
74
                    ],
75
                    'slideshow' => [
76
                        'type' => 'collection',
77
                        'options' => [
78
                            'field' => 'image',
79
                        ],
80
                    ],
81
                ],
82
            ],
83
        ]);
84
85
        $image1 = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
86
        $image2 = $this->createImage('/path/to/image2', 100, 200, 'image/jpeg');
87
        $image3 = $this->createImage('/path/to/image3', 100, 200, 'image/jpeg');
88
89
        $article = new ArticleWithRestrictedChildren();
90
        $article->id = '/test/article';
91
        $article->title = 'Foo';
92
        $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...
93
        $article->slideshow = [$image1, $image2, $image3];
94
95
        $this->documentManager->persist($article);
96
        $this->updater->update($this->documentManager, $article);
97
        $this->documentManager->flush();
98
        $this->documentManager->clear();
99
100
        $this->documentManager->find(null, '/test/article');
101
    }
102
103 View Code Duplication
    private function createArticleSlideshow()
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...
104
    {
105
        $article = new Article();
106
        $article->id = '/test/article';
107
108
        $image1 = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
109
        $image2 = $this->createImage('/path/to/image2', 100, 200, 'image/jpeg');
110
        $image3 = $this->createImage('/path/to/image3', 100, 200, 'image/jpeg');
111
112
        $article->slideshow = [
113
            $image1,
114
            $image2,
115
            $image3,
116
        ];
117
118
        return $article;
119
    }
120
}
121