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
<?php
namespace MongoDB\Tests;
use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use InvalidArgumentException;
use stdClass;
use Traversable;
abstract class FunctionalTestCase extends TestCase
{
protected $manager;
public function setUp()
{
$this->manager = new Manager($this->getUri());
}
protected function assertCollectionCount($namespace, $count)
{
list($databaseName, $collectionName) = explode('.', $namespace, 2);
$cursor = $this->manager->executeCommand($databaseName, new Command(['count' => $collectionName]));
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());
$this->assertArrayHasKey('n', $document);
$this->assertEquals($count, $document['n']);
}
protected function assertCommandSucceeded($document)
{
$document = is_object($document) ? (array) $document : $document;
$this->assertArrayHasKey('ok', $document);
$this->assertEquals(1, $document['ok']);
}
protected function assertSameObjectID($expectedObjectID, $actualObjectID)
{
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $expectedObjectID);
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $actualObjectID);
$this->assertEquals((string) $expectedObjectID, (string) $actualObjectID);
}
protected function assertSameDocument($expectedDocument, $actualDocument)
{
$this->assertEquals(
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($expectedDocument))),
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($actualDocument)))
);
}
protected function assertSameDocuments(array $expectedDocuments, $actualDocuments)
{
if ($actualDocuments instanceof Traversable) {
$actualDocuments = iterator_to_array($actualDocuments);
}
if ( ! is_array($actualDocuments)) {
throw new InvalidArgumentException('$actualDocuments is not an array or Traversable');
}
$normalizeRootDocuments = function($document) {
return \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($document)));
};
$this->assertEquals(
array_map($normalizeRootDocuments, $expectedDocuments),
array_map($normalizeRootDocuments, $actualDocuments)
);
}
protected function getPrimaryServer()
{
return $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
}
protected function getServerVersion(ReadPreference $readPreference = null)
{
$cursor = $this->manager->executeCommand(
$this->getDatabaseName(),
new Command(['buildInfo' => 1]),
$readPreference ?: new ReadPreference(ReadPreference::RP_PRIMARY)
);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
$document = current($cursor->toArray());
return $document['version'];
}
/**
* Normalizes a BSON document or array for use with assertEquals().
*
* The argument will be converted to a BSONArray or BSONDocument based on
* its type and keys. Document fields will be sorted alphabetically. Each
* value within the array or document will then be normalized recursively.
*
* @param array|object $bson
* @return BSONDocument|BSONArray
* @throws InvalidArgumentException if $bson is not an array or object
*/
private function normalizeBSON($bson)
{
if ( ! is_array($bson) && ! is_object($bson)) {
throw new InvalidArgumentException('$bson is not an array or object');
}
if ($bson instanceof BSONArray || (is_array($bson) && $bson === array_values($bson))) {
if ( ! $bson instanceof BSONArray) {
$bson = new BSONArray($bson);
}
} else {
if ( ! $bson instanceof BSONDocument) {
$bson = new BSONDocument((array) $bson);
}
$bson->ksort();
}
foreach ($bson as $key => $value) {
if ($value instanceof BSONArray || (is_array($value) && $value === array_values($value))) {
$bson[$key] = $this->normalizeBSON($value);
continue;
}
if ($value instanceof stdClass || $value instanceof BSONDocument || is_array($value)) {
$bson[$key] = $this->normalizeBSON($value);
continue;
}
}
return $bson;
}
}