|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ODM\CouchDB\UnitOfWork; |
|
6
|
|
|
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata; |
|
7
|
|
|
use Doctrine\ODM\CouchDB\Id\idGenerator; |
|
8
|
|
|
|
|
9
|
|
|
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; |
|
10
|
|
|
|
|
11
|
|
|
class UnitOfWorkTest extends CouchDBTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
private $dm; |
|
14
|
|
|
private $uow; |
|
15
|
|
|
|
|
16
|
|
|
public function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->type = 'Doctrine\Tests\ODM\CouchDB\UoWUser'; |
|
|
|
|
|
|
19
|
|
|
$this->dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test')); |
|
20
|
|
|
$this->uow = new UnitOfWork($this->dm); |
|
21
|
|
|
|
|
22
|
|
|
$metadata = new \Doctrine\ODM\CouchDB\Mapping\ClassMetadata($this->type); |
|
23
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'id' => true)); |
|
24
|
|
|
$metadata->mapField(array('fieldName' => 'username', 'type' => 'string')); |
|
25
|
|
|
$metadata->idGenerator = \Doctrine\ODM\CouchDB\Mapping\ClassMetadata::IDGENERATOR_ASSIGNED; |
|
26
|
|
|
|
|
27
|
|
|
$metadata->initializeReflection(new RuntimeReflectionService()); |
|
28
|
|
|
$metadata->wakeupReflection(new RuntimeReflectionService()); |
|
29
|
|
|
|
|
30
|
|
|
$cmf = $this->dm->getClassMetadataFactory(); |
|
31
|
|
|
$cmf->setMetadataFor($this->type, $metadata); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCreateDocument() |
|
35
|
|
|
{ |
|
36
|
|
|
$user = $this->uow->createDocument($this->type, array( |
|
37
|
|
|
'_id' => '1', |
|
38
|
|
|
'_rev' => 23, |
|
39
|
|
|
'username' => 'foo', |
|
40
|
|
|
'type' => $this->type |
|
41
|
|
|
)); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertInstanceOf($this->type, $user); |
|
44
|
|
|
$this->assertEquals('1', $user->id); |
|
45
|
|
|
$this->assertEquals('foo', $user->username); |
|
46
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $this->uow->getDocumentState($user)); |
|
47
|
|
|
$this->assertEquals(1, $this->uow->getDocumentIdentifier($user)); |
|
48
|
|
|
$this->assertEquals(23, $this->uow->getDocumentRevision($user)); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals(array('id' => '1', 'username' => 'foo'), $this->uow->getOriginalData($user)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testCreateDocument_UseIdentityMap() |
|
54
|
|
|
{ |
|
55
|
|
|
$user1 = $this->uow->createDocument($this->type, array('_id' => '1', '_rev' => 1, 'username' => 'foo', 'type' => $this->type)); |
|
56
|
|
|
$user2 = $this->uow->createDocument($this->type, array('_id' => '1', '_rev' => 1, 'username' => 'foo', 'type' => $this->type)); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertSame($user1, $user2); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testTryGetById() |
|
62
|
|
|
{ |
|
63
|
|
|
$user1 = $this->uow->createDocument($this->type, array('_id' => '1', '_rev' => 1, 'username' => 'foo', 'type' => $this->type)); |
|
64
|
|
|
|
|
65
|
|
|
$user2 = $this->uow->tryGetById(1, $this->type); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$this->assertSame($user1, $user2); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testScheduleInsertion() |
|
71
|
|
|
{ |
|
72
|
|
|
$httpClient = $this->getMock('Doctrine\CouchDB\HTTP\Client', array(), array(), '', false); |
|
73
|
|
|
$httpClient->expects($this->once()) |
|
74
|
|
|
->method('request') |
|
75
|
|
|
->will($this->returnValue(new \Doctrine\CouchDB\HTTP\Response(404, array(), "{}"))); |
|
76
|
|
|
$this->dm->getCouchDBClient()->setHttpClient($httpClient); |
|
77
|
|
|
|
|
78
|
|
|
$object = new UoWUser(); |
|
79
|
|
|
$object->id = "1"; |
|
80
|
|
|
$object->username = "bar"; |
|
81
|
|
|
|
|
82
|
|
|
$this->uow->scheduleInsert($object); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testScheduleInsert_ForAssignedIdGenerator_WithoutId() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->setExpectedException('Doctrine\ODM\CouchDB\CouchDBException'); |
|
88
|
|
|
|
|
89
|
|
|
$object = new UoWUser(); |
|
90
|
|
|
$object->username = "bar"; |
|
91
|
|
|
|
|
92
|
|
|
$this->uow->scheduleInsert($object); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testScheduleInsert_ForUuidGenerator_QueriesUuidGenerator() |
|
96
|
|
|
{ |
|
97
|
|
|
$uuids = array( |
|
98
|
|
|
"4db492fb9e96682601d3f62b0797a8c0", |
|
99
|
|
|
"c3cee9c45f2fc2a3803ed26fdbceb3b4", |
|
100
|
|
|
"691f868266b6b45a867bfcb4b41a694e", |
|
101
|
|
|
"e2c4783e9ff922eefe869998a01828b2" |
|
102
|
|
|
); |
|
103
|
|
|
$uuidResponse = new \Doctrine\CouchDB\HTTP\Response(200, array(), json_encode(array('uuids' => $uuids))); |
|
104
|
|
|
|
|
105
|
|
|
$client = $this->getMock('Doctrine\CouchDB\HTTP\Client'); |
|
106
|
|
|
$client->expects($this->once()) |
|
107
|
|
|
->method('request') |
|
108
|
|
|
->with($this->equalTo('GET'), $this->equalTo('/_uuids?count=20')) |
|
109
|
|
|
->will($this->returnValue($uuidResponse)); |
|
110
|
|
|
$this->dm->getCouchDBClient()->setHttpClient($client); |
|
111
|
|
|
|
|
112
|
|
|
$object = new UoWUser(); |
|
113
|
|
|
$object->username = "bar"; |
|
114
|
|
|
|
|
115
|
|
|
$this->dm->getClassMetadata(get_class($object))->idGenerator = ClassMetadata::IDGENERATOR_UUID; |
|
116
|
|
|
$this->uow->scheduleInsert($object); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertNotNull($object->id); |
|
119
|
|
|
$this->assertEquals(end($uuids), $object->id); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testScheduleInsertCancelsScheduleRemove() |
|
123
|
|
|
{ |
|
124
|
|
|
$user1 = $this->uow->createDocument($this->type, array('_id' => '1', '_rev' => 1, 'username' => 'foo', 'type' => $this->type)); |
|
125
|
|
|
$this->uow->scheduleRemove($user1); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals(UnitOfWork::STATE_REMOVED, $this->uow->getDocumentState($user1)); |
|
128
|
|
|
|
|
129
|
|
|
$this->uow->scheduleInsert($user1); |
|
130
|
|
|
|
|
131
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $this->uow->getDocumentState($user1)); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
class UoWUser |
|
136
|
|
|
{ |
|
137
|
|
|
public $id; |
|
138
|
|
|
public $username; |
|
139
|
|
|
} |
|
140
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: