forked from jackalope/jackalope
/
ItemTest.php
169 lines (145 loc) · 4.81 KB
/
ItemTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Jackalope;
class ItemTest extends TestCase
{
/**
* create the item and mock any of the constructor parameters not specified explicitly
*/
protected function getItem($factory = null, $path = null, $session = null, $objectManager = null, $new = false)
{
if (! $factory) {
$factory = new \Jackalope\Factory;
}
if (! $path) {
$path = '/';
}
if (! $session) {
$session = $this->getSessionMock();
}
if (! $objectManager) {
$objectManager = $this->getObjectManagerMock();
}
return new TestItem($factory, $path, $session, $objectManager, $new);
}
/**
* a mock that will additionally expect getNodeByPath once with $path and return the string 'placeholder'
*/
protected function getObjectManagerMockWithPath($path)
{
$factory = new \Jackalope\Factory;
$om = $this->getMock('\Jackalope\ObjectManager', array('getNodeTypes', 'getNodeByPath'), array($factory, $this->getTransportStub('/jcr:root'), $this->getSessionMock()));
$om->expects($this->once())
->method('getNodeByPath')
->with($this->equalTo($path))
->will($this->returnValue('placeholder'));
return $om;
}
public function testPath()
{
$item = $this->getItem();
$item->setPath('/b');
$this->assertEquals('/b', $item->getPath());
}
public function testName()
{
$item = $this->getItem(null, '/path/itemname');
$this->assertEquals('itemname', $item->getName());
$item->setPath('/other/name');
$this->assertEquals('name', $item->getName());
}
public function testGetAncestor()
{
$om = $this->getObjectManagerMockWithPath('/path');
$item = $this->getItem(null, '/path/name', null, $om);
$self = $item->getAncestor(2);
$this->assertSame($item, $self);
$ancestor = $item->getAncestor(1);
$this->assertSame('placeholder', $ancestor);
}
public function testGetAncestorRoot()
{
$om = $this->getObjectManagerMockWithPath('/');
$item = $this->getItem(null, '/path/name', null, $om);
$ancestor = $item->getAncestor(0);
$this->assertSame('placeholder', $ancestor);
}
/**
* @expectedException \PHPCR\ItemNotFoundException
*/
public function testGetAncestorTooDeep()
{
$item = $this->getItem(null, '/path/name');
$ancestor = $item->getAncestor(3);
}
/**
* @expectedException \PHPCR\ItemNotFoundException
*/
public function testGetAncestorTooLow()
{
$item = $this->getItem(null, '/path/name');
$ancestor = $item->getAncestor(-1);
}
public function testGetParent()
{
$om = $this->getObjectManagerMockWithPath('/path');
$item = $this->getItem(null, '/path/name', null, $om);
$parent = $item->getParent();
$this->assertSame('placeholder', $parent);
}
public function testGetDepth()
{
$item = $this->getItem(null, '/path/name');
$this->assertEquals(2, $item->getDepth());
$item = $this->getItem(null, '/');
$this->assertEquals(0, $item->getDepth());
}
public function testGetSession()
{
$session = $this->getSessionMock();
$item = $this->getItem($session);
$this->assertEquals($session, $item->getSession());
}
public function testIsSame()
{
$this->markTestSkipped('TODO: do some mean stuff');
}
public function testAccept()
{
$item = $this->getItem();
$visitor = $this->getMock('\PHPCR\ItemVisitorInterface');
$visitor->expects($this->once())
->method('visit');
$item->accept($visitor);
}
public function testRemove()
{
$factory = new \Jackalope\Factory;
$om = $this->getMock('\Jackalope\ObjectManager', array('getNodeTypes', 'removeItem'), array($factory, $this->getTransportStub('/jcr:root'), $this->getSessionMock()));
$om->expects($this->once())
->method('removeItem')
->with($this->equalTo('/path'));
$item = $this->getItem(null, '/path', null, $om);
$item->remove();
$this->assertTrue($item->isDeleted());
}
/**
* @expectedException \PHPCR\RepositoryException
*/
public function testRemoveRoot()
{
$item = $this->getItem('/');
$item->remove();
}
// TODO: test the whole state model processes (probably in separate test case)
}
class TestItem extends Item
{
public function __construct($factory,$path,$session,$objectManager,$new)
{
parent::__construct($factory,$path,$session,$objectManager,$new);
}
public function refresh($keep)
{
// tested in extending classes
}
}