Skip to content

Commit 0437bb8

Browse files
committed
Make expected document assertions more flexible
Normalizing top-level documents as arrays will allow single and multi-document checks to be more flexible. This will not extend to embedded documents, where we'll still need to be explicit. Additionally, assertSameDocuments can now operate on any array or Traversable without needing to set its own type map.
1 parent 82de6ec commit 0437bb8

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

tests/Collection/CrudSpec/AggregateFunctionalTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ public function testAggregateWithMultipleStages()
3232
);
3333

3434
$expected = array(
35-
(object) array('_id' => 2, 'x' => 22),
36-
(object) array('_id' => 3, 'x' => 33),
35+
array('_id' => 2, 'x' => 22),
36+
array('_id' => 3, 'x' => 33),
3737
);
3838

39-
// Use iterator_to_array() here since aggregate() may return an ArrayIterator
40-
$this->assertEquals($expected, iterator_to_array($cursor));
39+
$this->assertSameDocuments($expected, $cursor);
4140
}
4241

4342
public function testAggregateWithOut()

tests/Collection/CrudSpec/FindOneAndDeleteFunctionalTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testFindOneAndDeleteWhenManyDocumentsMatch()
2525
);
2626

2727
$document = $this->collection->findOneAndDelete($filter, $options);
28-
$this->assertEquals((object) array('x' => 22), $document);
28+
$this->assertSameDocument(array('x' => 22), $document);
2929

3030
$expected = array(
3131
array('_id' => 1, 'x' => 11),
@@ -44,7 +44,7 @@ public function testFindOneAndDeleteWhenOneDocumentMatches()
4444
);
4545

4646
$document = $this->collection->findOneAndDelete($filter, $options);
47-
$this->assertEquals((object) array('x' => 22), $document);
47+
$this->assertSameDocument(array('x' => 22), $document);
4848

4949
$expected = array(
5050
array('_id' => 1, 'x' => 11),

tests/Collection/CrudSpec/FindOneAndReplaceFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentBefo
2929
);
3030

3131
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
32-
$this->assertEquals((object) array('x' => 22), $document);
32+
$this->assertSameDocument(array('x' => 22), $document);
3333

3434
$expected = array(
3535
array('_id' => 1, 'x' => 11),
@@ -51,7 +51,7 @@ public function testFindOneAndReplaceWhenManyDocumentsMatchReturningDocumentAfte
5151
);
5252

5353
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
54-
$this->assertEquals((object) array('x' => 32), $document);
54+
$this->assertSameDocument(array('x' => 32), $document);
5555

5656
$expected = array(
5757
array('_id' => 1, 'x' => 11),
@@ -72,7 +72,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentBefo
7272
);
7373

7474
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
75-
$this->assertEquals((object) array('x' => 22), $document);
75+
$this->assertSameDocument(array('x' => 22), $document);
7676

7777
$expected = array(
7878
array('_id' => 1, 'x' => 11),
@@ -94,7 +94,7 @@ public function testFindOneAndReplaceWhenOneDocumentMatchesReturningDocumentAfte
9494
);
9595

9696
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
97-
$this->assertEquals((object) array('x' => 32), $document);
97+
$this->assertSameDocument(array('x' => 32), $document);
9898

9999
$expected = array(
100100
array('_id' => 1, 'x' => 11),
@@ -185,7 +185,7 @@ public function testFindOneAndReplaceWithUpsertWhenNoDocumentsMatchReturningDocu
185185
);
186186

187187
$document = $this->collection->findOneAndReplace($filter, $replacement, $options);
188-
$this->assertEquals((object) array('x' => 44), $document);
188+
$this->assertSameDocument(array('x' => 44), $document);
189189

190190
$expected = array(
191191
array('_id' => 1, 'x' => 11),

tests/Collection/CrudSpec/FindOneAndUpdateFunctionalTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentBefor
2929
);
3030

3131
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
32-
$this->assertEquals((object) array('x' => 22), $document);
32+
$this->assertSameDocument(array('x' => 22), $document);
3333

3434
$expected = array(
3535
array('_id' => 1, 'x' => 11),
@@ -51,7 +51,7 @@ public function testFindOneAndUpdateWhenManyDocumentsMatchReturningDocumentAfter
5151
);
5252

5353
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
54-
$this->assertEquals((object) array('x' => 23), $document);
54+
$this->assertSameDocument(array('x' => 23), $document);
5555

5656
$expected = array(
5757
array('_id' => 1, 'x' => 11),
@@ -72,7 +72,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentBefor
7272
);
7373

7474
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
75-
$this->assertEquals((object) array('x' => 22), $document);
75+
$this->assertSameDocument(array('x' => 22), $document);
7676

7777
$expected = array(
7878
array('_id' => 1, 'x' => 11),
@@ -94,7 +94,7 @@ public function testFindOneAndUpdateWhenOneDocumentMatchesReturningDocumentAfter
9494
);
9595

9696
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
97-
$this->assertEquals((object) array('x' => 23), $document);
97+
$this->assertSameDocument(array('x' => 23), $document);
9898

9999
$expected = array(
100100
array('_id' => 1, 'x' => 11),
@@ -183,7 +183,7 @@ public function testFindOneAndUpdateWithUpsertWhenNoDocumentsMatchReturningDocum
183183
);
184184

185185
$document = $this->collection->findOneAndUpdate($filter, $update, $options);
186-
$this->assertEquals((object) array('x' => 1), $document);
186+
$this->assertSameDocument(array('x' => 1), $document);
187187

188188
$expected = array(
189189
array('_id' => 1, 'x' => 11),

tests/FunctionalTestCase.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use MongoDB\Driver\Cursor;
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
9+
use stdClass;
10+
use Traversable;
911

1012
abstract class FunctionalTestCase extends TestCase
1113
{
@@ -38,10 +40,32 @@ protected function assertCommandSucceeded($document)
3840
$this->assertEquals(1, $document['ok']);
3941
}
4042

41-
protected function assertSameDocuments(array $expected, $cursor)
43+
protected function assertSameDocument($expectedDocument, $actualDocument)
4244
{
43-
$cursor->setTypeMap(array('document' => 'array'));
44-
$this->assertEquals($expected, iterator_to_array($cursor));
45+
$this->assertEquals(
46+
($expectedDocument instanceof stdClass) ? (array) $expectedDocument : $expectedDocument,
47+
($actualDocument instanceof stdClass) ? (array) $actualDocument : $actualDocument
48+
);
49+
}
50+
51+
protected function assertSameDocuments(array $expectedDocuments, $actualDocuments)
52+
{
53+
if ($actualDocuments instanceof Traversable) {
54+
$actualDocuments = iterator_to_array($actualDocuments);
55+
}
56+
57+
if ( ! is_array($actualDocuments)) {
58+
throw new InvalidArgumentException('$actualDocuments is not an array or Traversable');
59+
}
60+
61+
$normalizeRootDocuments = function($document) {
62+
return ($document instanceof stdClass) ? (array) $document : $document;
63+
};
64+
65+
$this->assertEquals(
66+
array_map($normalizeRootDocuments, $expectedDocuments),
67+
array_map($normalizeRootDocuments, $actualDocuments)
68+
);
4569
}
4670

4771
protected function getServerVersion(ReadPreference $readPreference = null)

0 commit comments

Comments
 (0)