Skip to content

HHVM test suite compatibility #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Model\BSONDocument;
use InvalidArgumentException;
use stdClass;
use Traversable;

Expand Down Expand Up @@ -38,11 +40,18 @@ protected function assertCommandSucceeded($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(
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)))
);
}

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

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

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

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;
}
}
12 changes: 10 additions & 2 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public function testDownloadToStream($input)
$id = $this->bucket->uploadFromStream('filename', $this->createStream($input));
$destination = $this->createStream();
$this->bucket->downloadToStream($id, $destination);
rewind($destination);

$this->assertStreamContents($input, $destination);
}
Expand Down Expand Up @@ -208,30 +209,37 @@ public function testDownloadToStreamByName()

$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);
}

Expand Down Expand Up @@ -331,7 +339,7 @@ public function testGetFileDocumentForStreamWithReadableStream()

$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);
Expand Down Expand Up @@ -363,7 +371,7 @@ public function testGetFileIdForStreamWithReadableStream()
$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()
Expand Down
2 changes: 1 addition & 1 deletion tests/GridFS/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function assertStreamContents($expectedContents, $stream)
{
$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));
}

/**
Expand Down
18 changes: 8 additions & 10 deletions tests/GridFS/SpecFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,19 @@ private function assertEquivalentCollections($expectedCollection, $actualCollect
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);
}
Expand Down Expand Up @@ -154,7 +152,7 @@ private function convertTypes(array $data, $createBinary = true)
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;
}

Expand Down