Commit a3449c6a authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #254

parents 765ff28c b251eb26
......@@ -6,6 +6,8 @@ use MongoDB\Driver\Command;
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Model\BSONDocument;
use InvalidArgumentException;
use stdClass;
use Traversable;
......@@ -38,11 +40,18 @@ abstract class FunctionalTestCase extends TestCase
$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(
is_object($expectedDocument) ? (array) $expectedDocument : $expectedDocument,
is_object($actualDocument) ? (array) $actualDocument : $actualDocument
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($expectedDocument))),
\MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($actualDocument)))
);
}
......@@ -57,7 +66,7 @@ abstract class FunctionalTestCase extends TestCase
}
$normalizeRootDocuments = function($document) {
return is_object($document) ? (array) $document : $document;
return \MongoDB\BSON\toJSON(\MongoDB\BSON\fromPHP($this->normalizeBSON($document)));
};
$this->assertEquals(
......@@ -84,4 +93,48 @@ abstract class FunctionalTestCase extends TestCase
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;
}
}
......@@ -174,6 +174,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$destination = $this->createStream();
$this->bucket->downloadToStream($id, $destination);
rewind($destination);
$this->assertStreamContents($input, $destination);
}
......@@ -208,30 +209,37 @@ class BucketFunctionalTest extends FunctionalTestCase
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination);
rewind($destination);
$this->assertStreamContents('baz', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -3]);
rewind($destination);
$this->assertStreamContents('foo', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -2]);
rewind($destination);
$this->assertStreamContents('bar', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => -1]);
rewind($destination);
$this->assertStreamContents('baz', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 0]);
rewind($destination);
$this->assertStreamContents('foo', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 1]);
rewind($destination);
$this->assertStreamContents('bar', $destination);
$destination = $this->createStream();
$this->bucket->downloadToStreamByName('filename', $destination, ['revision' => 2]);
rewind($destination);
$this->assertStreamContents('baz', $destination);
}
......@@ -331,7 +339,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$fileDocument = $this->bucket->getFileDocumentForStream($stream);
$this->assertEquals($id, $fileDocument->_id);
$this->assertSameObjectID($id, $fileDocument->_id);
$this->assertSame('filename', $fileDocument->filename);
$this->assertSame(6, $fileDocument->length);
$this->assertSameDocument($metadata, $fileDocument->metadata);
......@@ -363,7 +371,7 @@ class BucketFunctionalTest extends FunctionalTestCase
$id = $this->bucket->uploadFromStream('filename', $this->createStream('foobar'));
$stream = $this->bucket->openDownloadStream($id);
$this->assertEquals($id, $this->bucket->getFileIdForStream($stream));
$this->assertSameObjectID($id, $this->bucket->getFileIdForStream($stream));
}
public function testGetFileIdForStreamWithWritableStream()
......
......@@ -38,7 +38,7 @@ abstract class FunctionalTestCase extends BaseFunctionalTestCase
{
$this->assertInternalType('resource', $stream);
$this->assertSame('stream', get_resource_type($stream));
$this->assertEquals($expectedContents, stream_get_contents($stream, -1,.0));
$this->assertEquals($expectedContents, stream_get_contents($stream));
}
/**
......
......@@ -97,21 +97,19 @@ class SpecFunctionalTest extends FunctionalTestCase
foreach ($mi as $documents) {
list($expectedDocument, $actualDocument) = $documents;
array_walk($expectedDocument, function(&$value) use ($actualResult) {
if ($value === '*result') {
$value = $actualResult;
foreach ($expectedDocument as $key => $value) {
if ( ! is_string($value)) {
continue;
}
});
array_walk($expectedDocument, function(&$value, $key) use ($actualDocument) {
if ( ! is_string($value)) {
return;
if ($value === '*result') {
$expectedDocument[$key] = $actualResult;
}
if ( ! strncmp($value, '*actual_', 8)) {
$value = $actualDocument[$key];
$expectedDocument[$key] = $actualDocument[$key];
}
}
});
$this->assertSameDocument($expectedDocument, $actualDocument);
}
......@@ -154,7 +152,7 @@ class SpecFunctionalTest extends FunctionalTestCase
if (isset($value['$date'])) {
// TODO: This is necessary until PHPC-536 is implemented
$milliseconds = floor((new DateTime($value['$date']))->format('U.u') * 1000);
$value = new UTCDateTime($milliseconds);
$value = new UTCDateTime((int) $milliseconds);
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment