-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitOfWorkTest.php
186 lines (151 loc) · 5.95 KB
/
UnitOfWorkTest.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Doctrine\Tests\ODM\PHPCR;
use Doctrine\ODM\PHPCR\Configuration;
use Doctrine\ODM\PHPCR\UnitOfWork;
use Doctrine\ODM\PHPCR\DocumentManager;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use Jackalope\Factory;
use Jackalope\Node;
/**
* TODO: remove Jackalope dependency
*
* @group unit
*/
class UnitOfWorkTest extends PHPCRTestCase
{
/** @var DocumentManager */
private $dm;
/** @var UnitOfWork */
private $uow;
/** @var Factory */
private $factory;
/** @var \PHPCR\SessionInterface */
private $session;
/** @var \Jackalope\ObjectManager */
private $objectManager;
/** @var string */
private $type;
public function setUp()
{
if (!class_exists('Jackalope\Factory', true)) {
$this->markTestSkipped('The Node needs to be properly mocked/stubbed. Remove dependency to Jackalope');
}
$this->factory = new Factory;
$this->session = $this->getMock('Jackalope\Session', array(), array($this->factory), '', false);
$this->objectManager = $this->getMock('Jackalope\ObjectManager', array(), array($this->factory), '', false);
$this->type = 'Doctrine\Tests\ODM\PHPCR\UoWUser';
$this->dm = DocumentManager::create($this->session);
$this->uow = new UnitOfWork($this->dm);
$cmf = $this->dm->getMetadataFactory();
$metadata = new ClassMetadata($this->type);
$metadata->initializeReflection($cmf->getReflectionService());
$metadata->mapId(array('fieldName' => 'id', 'id' => true));
$metadata->idGenerator = ClassMetadata::GENERATOR_TYPE_ASSIGNED;
$metadata->mapField(array('fieldName' => 'username', 'type' => 'string'));
$cmf->setMetadataFor($this->type, $metadata);
}
protected function createNode($id, $username)
{
$repository = $this->getMockBuilder('Jackalope\Repository')->disableOriginalConstructor()->getMock();
$this->session->expects($this->any())
->method('getRepository')
->with()
->will($this->returnValue($repository));
$nodeData = array(
"jcr:primaryType" => "rep:root",
"jcr:system" => array(),
'username' => $username,
);
return new Node($this->factory, $nodeData, $id, $this->session, $this->objectManager);
}
public function testGetOrCreateDocument()
{
$user = $this->uow->getOrCreateDocument($this->type, $this->createNode('/somepath', 'foo'));
$this->assertInstanceOf($this->type, $user);
$this->assertEquals('foo', $user->username);
$method = new \ReflectionMethod($this->uow, 'getDocumentState');
$method->setAccessible(true);
$state = $method->invoke($this->uow, $user);
$method->setAccessible(false);
$this->assertEquals(UnitOfWork::STATE_MANAGED, $state);
$this->assertEquals('/somepath', $this->uow->getDocumentId($user));
}
public function testGetOrCreateDocumentUsingIdentityMap()
{
$user1 = $this->uow->getOrCreateDocument($this->type, $this->createNode('/somepath', 'foo'));
$user2 = $this->uow->getOrCreateDocument($this->type, $this->createNode('/somepath', 'foo'));
$this->assertSame($user1, $user2);
}
public function testGetDocumentById()
{
$user1 = $this->uow->getOrCreateDocument($this->type, $this->createNode('/somepath', 'foo'));
$user2 = $this->uow->getDocumentById('/somepath', $this->type);
$this->assertSame($user1, $user2);
}
/**
* @covers Doctrine\ODM\PHPCR\UnitOfWork::scheduleInsert
* @covers Doctrine\ODM\PHPCR\UnitOfWork::doScheduleInsert
*/
public function testScheduleInsertion()
{
$object = new UoWUser();
$object->username = "bar";
$object->id = '/somepath';
$this->uow->scheduleInsert($object);
}
/**
* @covers Doctrine\ODM\PHPCR\UnitOfWork::scheduleRemove
* @covers Doctrine\ODM\PHPCR\UnitOfWork::scheduleInsert
* @covers Doctrine\ODM\PHPCR\UnitOfWork::doScheduleInsert
*/
public function testScheduleInsertCancelsScheduleRemove()
{
$object = new UoWUser();
$object->username = "bar";
$object->id = '/somepath';
$this->uow->scheduleInsert($object);
$this->uow->scheduleRemove($object);
$method = new \ReflectionMethod($this->uow, 'getDocumentState');
$method->setAccessible(true);
$state = $method->invoke($this->uow, $object);
$method->setAccessible(false);
$this->assertEquals(UnitOfWork::STATE_REMOVED, $state);
$this->uow->scheduleInsert($object);
$method->setAccessible(true);
$state = $method->invoke($this->uow, $object);
$method->setAccessible(false);
$this->assertEquals(UnitOfWork::STATE_MANAGED, $state);
}
public function testUuid()
{
$class = new \ReflectionClass('Doctrine\ODM\PHPCR\UnitOfWork');
$method = $class->getMethod('generateUuid');
$method->setAccessible(true);
$this->assertInternalType('string', $method->invoke($this->uow));
$config = new Configuration();
$config->setUuidGenerator(function () {
return 'like-a-uuid';
});
$dm = DocumentManager::create($this->session, $config);
$uow = new UnitOfWork($dm);
$this->assertEquals('like-a-uuid', $method->invoke($uow));
}
/**
* @see https://github.com/doctrine/phpcr-odm/issues/637
* @covers Doctrine\ODM\PHPCR\UnitOfWork::computeSingleDocumentChangeSet
*/
public function testComputeSingleDocumentChangeSetForRemovedDocument()
{
$object = new UoWUser();
$object->username = "bar";
$object->id = '/somepath';
$this->uow->scheduleRemove($object);
// Should not throw "InvalidArgumentException: Document has to be managed for single computation"
$this->uow->computeSingleDocumentChangeSet($object);
}
}
class UoWUser
{
public $id;
public $username;
}