Completed
Pull Request — master (#1)
by Daniel
14:01 queued 12s
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;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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
        $page->name = 'page';
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in Psi\Bridge\ObjectAgent\D...s\Functional\Model\Page.

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...
124
        $this->agent->setParent($page, $parent);
125
    }
126
127
    /**
128
     * It should say if it supports a given object.
129
     */
130
    public function testSupports()
131
    {
132
        $this->assertTrue($this->agent->supports(Article::class));
133
        $this->assertFalse($this->agent->supports(\stdClass::class));
134
    }
135
136
    public function testQuery()
137
    {
138
        $query = Query::create(Page::class, Query::composite(
139
            'and',
140
            Query::comparison('eq', 'title', 'Hello')
141
        ));
142
        $this->agent->query($query);
143
144
    }
145
146
    private function createPage()
147
    {
148
        $page = new Page();
149
        $page->path = '/test/foobar';
150
        $this->documentManager->persist($page);
151
        $this->documentManager->flush();
152
153
        return $page;
154
    }
155
}
156