Skip to content

Commit a3449c6

Browse files
committed
Merge pull request #254
2 parents 765ff28 + b251eb2 commit a3449c6

File tree

4 files changed

+75
-16
lines changed

4 files changed

+75
-16
lines changed

tests/FunctionalTestCase.php

Lines changed: 56 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 MongoDB\Model\BSONDocument;
10+
use InvalidArgumentException;
911
use stdClass;
1012
use Traversable;
1113

@@ -38,11 +40,18 @@ protected function assertCommandSucceeded($document)
3840
$this->assertEquals(1, $document['ok']);
3941
}
4042

43+
protected function assertSameObjectID($expectedObjectID, $actualObjectID)
44+
{
45+
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $expectedObjectID);
46+
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $actualObjectID);
47+
$this->assertEquals((string) $expectedObjectID, (string) $actualObjectID);
48+
}
49+
4150
protected function assertSameDocument($expectedDocument, $actualDocument)
4251
{
4352
$this->assertEquals(
44-
is_object($expectedDocument) ? (array) $expectedDocument : $expectedDocument,
45-
is_object($actualDocument) ? (array) $actualDocument : $actualDocument
53+
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($expectedDocument))),
54+
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($actualDocument)))
4655
);
4756
}
4857

@@ -57,7 +66,7 @@ protected function assertSameDocuments(array $expectedDocuments, $actualDocument
5766
}
5867

5968
$normalizeRootDocuments = function($document) {
60-
return is_object($document) ? (array) $document : $document;
69+
return \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($document)));
6170
};
6271

6372
$this->assertEquals(
@@ -84,4 +93,48 @@ protected function getServerVersion(ReadPreference $readPreference = null)
8493

8594
return $document['version'];
8695
}
96+
97+
/**
98+
* Normalizes a BSON document or array for use with assertEquals().
99+
*
100+
* The argument will be converted to a BSONArray or BSONDocument based on
101+
* its type and keys. Document fields will be sorted alphabetically. Each
102+
* value within the array or document will then be normalized recursively.
103+
*
104+
* @param array|object $bson
105+
* @return BSONDocument|BSONArray
106+
* @throws InvalidArgumentException if $bson is not an array or object
107+
*/
108+
private function normalizeBSON($bson)
109+
{
110+
if ( ! is_array($bson) && ! is_object($bson)) {
111+
throw new InvalidArgumentException('$bson is not an array or object');
112+
}
113+
114+
if ($bson instanceof BSONArray || (is_array($bson) && $bson === array_values($bson))) {
115+
if ( ! $bson instanceof BSONArray) {
116+
$bson = new BSONArray($bson);
117+
}
118+
} else {
119+
if ( ! $bson instanceof BSONDocument) {
120+
$bson = new BSONDocument((array) $bson);
121+
}
122+
123+
$bson->ksort();
124+
}
125+
126+
foreach ($bson as $key => $value) {
127+
if ($value instanceof BSONArray || (is_array($value) && $value === array_values($value))) {
128+
$bson[$key] = $this->normalizeBSON($value);
129+
continue;
130+
}
131+
132+
if ($value instanceof stdClass || $value instanceof BSONDocument || is_array($value)) {
133+
$bson[$key] = $this->normalizeBSON($value);
134+
continue;
135+
}
136+
}
137+
138+
return $bson;
139+
}
87140
}

tests/GridFS/BucketFunctionalTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public function testDownloadToStream($input)
174174
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
175175
$destination = $this->createStream();
176176
$this->bucket->downloadToStream($id, $destination);
177+
rewind($destination);
177178

178179
$this->assertStreamContents($input, $destination);
179180
}
@@ -208,30 +209,37 @@ public function testDownloadToStreamByName()
208209

209210
$destination = $this->createStream();
210211
$this->bucket->downloadToStreamByName('filename', $destination);
212+
rewind($destination);
211213
$this->assertStreamContents('baz', $destination);
212214

213215
$destination = $this->createStream();
214216
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -3]);
217+
rewind($destination);
215218
$this->assertStreamContents('foo', $destination);
216219

217220
$destination = $this->createStream();
218221
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -2]);
222+
rewind($destination);
219223
$this->assertStreamContents('bar', $destination);
220224

221225
$destination = $this->createStream();
222226
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -1]);
227+
rewind($destination);
223228
$this->assertStreamContents('baz', $destination);
224229

225230
$destination = $this->createStream();
226231
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 0]);
232+
rewind($destination);
227233
$this->assertStreamContents('foo', $destination);
228234

229235
$destination = $this->createStream();
230236
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 1]);
237+
rewind($destination);
231238
$this->assertStreamContents('bar', $destination);
232239

233240
$destination = $this->createStream();
234241
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 2]);
242+
rewind($destination);
235243
$this->assertStreamContents('baz', $destination);
236244
}
237245

@@ -331,7 +339,7 @@ public function testGetFileDocumentForStreamWithReadableStream()
331339

332340
$fileDocument = $this->bucket->getFileDocumentForStream($stream);
333341

334-
$this->assertEquals($id, $fileDocument->_id);
342+
$this->assertSameObjectID($id, $fileDocument->_id);
335343
$this->assertSame('filename', $fileDocument->filename);
336344
$this->assertSame(6, $fileDocument->length);
337345
$this->assertSameDocument($metadata, $fileDocument->metadata);
@@ -363,7 +371,7 @@ public function testGetFileIdForStreamWithReadableStream()
363371
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
364372
$stream = $this->bucket->openDownloadStream($id);
365373

366-
$this->assertEquals($id, $this->bucket->getFileIdForStream($stream));
374+
$this->assertSameObjectID($id, $this->bucket->getFileIdForStream($stream));
367375
}
368376

369377
public function testGetFileIdForStreamWithWritableStream()

tests/GridFS/FunctionalTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function assertStreamContents($expectedContents, $stream)
3838
{
3939
$this->assertInternalType('resource', $stream);
4040
$this->assertSame('stream', get_resource_type($stream));
41-
$this->assertEquals($expectedContents, stream_get_contents($stream, -1,.0));
41+
$this->assertEquals($expectedContents, stream_get_contents($stream));
4242
}
4343

4444
/**

tests/GridFS/SpecFunctionalTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,19 @@ private function assertEquivalentCollections($expectedCollection, $actualCollect
9797
foreach ($mi as $documents) {
9898
list($expectedDocument, $actualDocument) = $documents;
9999

100-
array_walk($expectedDocument, function(&$value) use ($actualResult) {
101-
if ($value === '*result') {
102-
$value = $actualResult;
100+
foreach ($expectedDocument as $key => $value) {
101+
if ( ! is_string($value)) {
102+
continue;
103103
}
104-
});
105104

106-
array_walk($expectedDocument, function(&$value, $key) use ($actualDocument) {
107-
if ( ! is_string($value)) {
108-
return;
105+
if ($value === '*result') {
106+
$expectedDocument[$key] = $actualResult;
109107
}
110108

111109
if ( ! strncmp($value, '*actual_', 8)) {
112-
$value = $actualDocument[$key];
110+
$expectedDocument[$key] = $actualDocument[$key];
113111
}
114-
});
112+
}
115113

116114
$this->assertSameDocument($expectedDocument, $actualDocument);
117115
}
@@ -154,7 +152,7 @@ private function convertTypes(array $data, $createBinary = true)
154152
if (isset($value['$date'])) {
155153
// TODO: This is necessary until PHPC-536 is implemented
156154
$milliseconds = floor((new DateTime($value['$date']))->format('U.u') * 1000);
157-
$value = new UTCDateTime($milliseconds);
155+
$value = new UTCDateTime((int) $milliseconds);
158156
return;
159157
}
160158

0 commit comments

Comments
 (0)