Completed
Pull Request — master (#33)
by Daniel
16:52 queued 13:40
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 Psi\Bridge\ContentType\Doctrine\PhpcrOdm\Tests\Functional\Example\Article;
6
use Psi\Component\ContentType\Tests\Functional\Example\Model\Image;
7
8
class GeneralTest extends PhpcrOdmTestCase
9
{
10
    private $documentManager;
11
12
    public function setUp()
13
    {
14
        $container = $this->getContainer([
15
            'mapping' => [
16
                Article::class => [
17
                    'fields' => [
18
                        'title' => [
19
                            'type' => 'text',
20
                            'role' => 'title',
21
                        ],
22
                        'image' => [
23
                            'type' => 'image',
24
                            'role' => 'image',
25
                        ],
26
                        'date' => [
27
                            'type' => 'datetime',
28
                        ],
29
                        'referencedImage' => [
30
                            'type' => 'object_reference',
31
                        ],
32
                    ],
33
                ],
34
            ],
35
        ]);
36
        $this->documentManager = $container->get('doctrine_phpcr.document_manager');
37
        $this->initPhpcr($this->documentManager);
38
    }
39
40
    /**
41
     * The user document should be persisted with the content-type data.
42
     */
43
    public function testMapping()
44
    {
45
        $article = new Article();
46
        $article->id = '/test/article';
47
        $article->title = 'Hello';
48
        $article->date = new \DateTime('2016-01-01 00:00:00');
49
50
        $article->image = $this->createImage(
51
            '/path/to/image', 100, 200, 'image/jpeg'
52
        );
53
54
        $this->documentManager->persist($article);
55
        $this->documentManager->flush();
56
57
        $this->documentManager->clear();
58
59
        $article = $this->documentManager->find(null, '/test/article');
60
61
        $this->assertInstanceOf(Article::class, $article);
62
        $this->assertEquals('Hello', $article->title);
63
        $this->assertInstanceOf(Image::class, $article->image);
64
        $this->assertEquals(100, $article->image->width);
65
        $this->assertEquals('image/jpeg', $article->image->mimetype);
66
        $this->assertEquals(new \DateTime('2016-01-01 00:00:00'), $article->date);
67
    }
68
69
    /**
70
     * It should map a reference.
71
     */
72
    public function testMapReference()
73
    {
74
        $image = $this->createImage('/path/to/image1', 100, 200, 'image/jpeg');
75
        $image->id = '/test/image';
76
        $article = new Article();
77
        $article->id = '/test/article';
78
        $article->title = 'Foo';
79
        $article->date = new \DateTime();
80
        $article->referencedImage = $image;
81
82
        $this->documentManager->persist($article);
83
        $this->documentManager->flush();
84
        $this->documentManager->clear();
85
86
        $article = $this->documentManager->find(null, '/test/article');
87
        $image = $article->referencedImage;
88
        $this->assertInstanceOf(Image::class, $image);
89
    }
90
}
91