Completed
Push — master ( d1b735...0bfc48 )
by Daniel
08:29
created

PhpcrOdmAgentTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional;
4
5
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model\Article;
6
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model\Page;
7
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model\PageNoUuid;
8
use Psi\Component\ObjectAgent\Query\Query;
9
10
class PhpcrOdmAgentTest extends PhpcrOdmTestCase
11
{
12
    private $agent;
13
    private $documentManager;
14
15
    public function setUp()
16
    {
17
        $container = $this->getContainer();
18
        $this->agent = $container->get('psi_object_agent.phpcr_odm');
19
        $this->documentManager = $container->get('phpcr_odm');
20
        $this->initPhpcr($this->documentManager);
21
    }
22
23
    /**
24
     * It should find a document.
25
     */
26
    public function testFind()
27
    {
28
        $page = $this->createPage();
29
30
        $document = $this->agent->find('/test/foobar');
31
        $this->assertSame($page, $document);
32
    }
33
34
    /**
35
     * It should throw an exception if the document was not found.
36
     *
37
     * @expectedException Psi\Component\ObjectAgent\Exception\ObjectNotFoundException
38
     * @expectedExceptionMessage Could not find document with identifier "/test/foobar" (class "<null>")
39
     */
40
    public function testFindNotFound()
41
    {
42
        $this->agent->find('/test/foobar');
43
    }
44
45
    /**
46
     * It should save.
47
     */
48
    public function testSave()
49
    {
50
        $page = new Page();
51
        $page->path = '/test/new';
52
        $this->agent->save($page);
53
54
        $document = $this->documentManager->find(null, '/test/new');
55
        $this->assertNotNull($document);
56
        $this->assertSame($page, $document);
57
    }
58
59
    /**
60
     * It should delete.
61
     */
62
    public function testDelete()
63
    {
64
        $page = $this->createPage();
65
        $this->agent->delete($page);
66
67
        $document = $this->documentManager->find(null, '/test/foobar');
68
        $this->assertNull($document);
69
    }
70
71
    /**
72
     * It should return a document's identifier (a UUID).
73
     */
74
    public function testGetIdentifier()
75
    {
76
        $page = $this->createPage();
77
        $identifier = $this->agent->getIdentifier($page);
78
        $this->assertNotNull($identifier);
79
    }
80
81
    /**
82
     * It should throw an exception if the document does not have a mapped UUID field.
83
     *
84
     * @expectedException \RuntimeException
85
     * @expectedExceptionMessage does not have a UUID-mapped property
86
     */
87
    public function testGetIdentifierNoMappedUuid()
88
    {
89
        $page = new PageNoUuid();
90
        $page->path = '/test/foobar';
91
92
        $identifier = $this->agent->getIdentifier($page);
93
        $this->assertNotNull($identifier);
94
    }
95
96
    /**
97
     * It should set the parent document on a given document.
98
     */
99
    public function testSetParent()
100
    {
101
        $parent = $this->createPage();
102
        $article = new Article();
103
        $article->name = 'article';
104
        $this->agent->setParent($article, $parent);
105
106
        $this->documentManager->persist($article);
107
        $this->documentManager->flush();
108
109
        $document = $this->documentManager->find(null, '/test/foobar/article');
110
        $this->assertNotNull($document);
111
    }
112
113
    /**
114
     * It should throw an exception if attempting to set parent on a document with no parent mapping.
115
     *
116
     * @expectedException \RuntimeException
117
     * @expectedExceptionMessage does not have a ParentDocument mapping
118
     */
119
    public function testSetParentNoParentMapping()
120
    {
121
        $parent = $this->createPage();
122
        $page = new Page();
123
        $this->agent->setParent($page, $parent);
124
    }
125
126
    /**
127
     * It should say if it supports a given object.
128
     */
129
    public function testSupports()
130
    {
131
        $this->assertTrue($this->agent->supports(Article::class));
132
        $this->assertFalse($this->agent->supports(\stdClass::class));
133
    }
134
135
    public function testQuery()
136
    {
137
        $query = Query::create(Page::class, Query::composite(
138
            'and',
139
            Query::comparison('eq', 'title', 'Hello')
140
        ));
141
        $this->agent->query($query);
142
    }
143
144
    private function createPage()
145
    {
146
        $page = new Page();
147
        $page->path = '/test/foobar';
148
        $this->documentManager->persist($page);
149
        $this->documentManager->flush();
150
151
        return $page;
152
    }
153
}
154