Skip to content

Commit 95d3788

Browse files
committed
Implemented tests for getAll(includeInternals) and revision check on replace
Signed-off-by: Frank Mayer <frank@frankmayer.net>
1 parent 8aa5461 commit 95d3788

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tests/DocumentExtendedTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,87 @@ public function testCreateUpdateGetAndDeleteDocumentThroughCreateFromArray()
9191
$this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
9292
$this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));
9393

94+
$response = $documentHandler->delete($document);
95+
$this->assertTrue(true === $response, 'Delete should return true!');
96+
}
97+
98+
/**
99+
* test for creation, update, get, and delete of a document given its settings through createFromArray()
100+
*/
101+
public function testCreateUpdateGetAndDeleteDocumentWithRevisionCheck()
102+
{
103+
$documentHandler = $this->documentHandler;
104+
105+
$document = Document::createFromArray(array('someAttribute' => 'someValue', 'someOtherAttribute' => 'someOtherValue'));
106+
$documentId = $documentHandler->add($this->collection->getId(), $document);
107+
108+
$this->assertTrue(is_numeric($documentId), 'Did not return an id!');
109+
110+
$resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
111+
112+
$this->assertObjectHasAttribute('_id', $resultingDocument, '_id field should exist, empty or with an id');
113+
114+
115+
// Set some new values on the attributes and include the revision in the _rev attribute
116+
// This should result in a successfull update
117+
$document->set('someAttribute','someValue2');
118+
$document->set('someOtherAttribute','someOtherValue2');
119+
$document->set('_rev',$resultingDocument->getRevision());
120+
121+
$result = $documentHandler->update($document, 'error');
122+
123+
$this->assertTrue($result);
124+
$resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
125+
126+
$this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
127+
$this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));
128+
129+
// Set some new values on the attributes and include a fake revision in the _rev attribute
130+
// This should result in a successfull update
131+
$document->set('someAttribute','someValue3');
132+
$document->set('someOtherAttribute','someOtherValue3');
133+
$document->set('_rev',$resultingDocument->getRevision()-1000);
134+
135+
try {
136+
$result = $documentHandler->update($document, 'error');
137+
} catch (\Exception $e) {
138+
// don't bother us... just give us the $e
139+
}
140+
141+
$this->assertInstanceOf('Exception', $e);
142+
$this->assertTrue($e->getMessage() == 'HTTP/1.1 412 Precondition Failed');
143+
$resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
144+
145+
$this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue2'));
146+
$this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue2'));
147+
unset ($e);
148+
149+
$document = Document::createFromArray(array('someAttribute' => 'someValue3', 'someOtherAttribute' => 'someOtherValue3'));
150+
$document->setInternalId($this->collection->getId().'/'.$documentId);
151+
// Set some new values on the attributes and _rev attribute to NULL
152+
// This should result in a successfull update
153+
try {
154+
$result = $documentHandler->update($document, 'error');
155+
} catch (\Exception $e) {
156+
// don't bother us... just give us the $e
157+
}
158+
$resultingDocument = $documentHandler->get($this->collection->getId(), $documentId);
159+
160+
$this->assertTrue(true === ($resultingDocument->someAttribute == 'someValue3'));
161+
$this->assertTrue(true === ($resultingDocument->someOtherAttribute == 'someOtherValue3'));
162+
163+
164+
165+
166+
94167
$response = $documentHandler->delete($document);
95168
$this->assertTrue(true === $response, 'Delete should return true!');
96169
}
97170

98171

99172
/**
100173
* test to set some attributes and get all attributes of the document through getAll()
174+
* Also testing to optionally get internal attributes _id and _rev
101175
*/
102176
public function testGetAll()
103177
{
@@ -110,6 +184,10 @@ public function testGetAll()
110184

111185
$this->assertTrue(true === ($result['someAttribute'] == 'someValue'));
112186
$this->assertTrue(true === ($result['someOtherAttribute'] == 'someOtherValue'));
187+
188+
$result = $document->getAll(true);
189+
$this->assertArrayHasKey('_id', $result);
190+
$this->assertArrayHasKey('_rev', $result);
113191
}
114192

115193
public function tearDown()

0 commit comments

Comments
 (0)