BSONDocumentTest.php 3.95 KB
Newer Older
1 2
<?php

3
namespace MongoDB\Tests\Model;
4

5
use MongoDB\BSON\ObjectId;
6
use MongoDB\Model\BSONArray;
7
use MongoDB\Model\BSONDocument;
8
use MongoDB\Tests\TestCase;
9
use ArrayObject;
10
use stdClass;
11
use ReflectionClass;
12 13 14

class BSONDocumentTest extends TestCase
{
15 16 17 18 19 20 21
    public function testConstructorDefaultsToPropertyAccess()
    {
        $document = new BSONDocument(['foo' => 'bar']);
        $this->assertEquals(ArrayObject::ARRAY_AS_PROPS, $document->getFlags());
        $this->assertSame('bar', $document->foo);
    }

22 23 24 25 26 27 28 29
    public function testBsonSerializeCastsToObject()
    {
        $data = [0 => 'foo', 2 => 'bar'];

        $document = new BSONDocument($data);
        $this->assertSame($data, $document->getArrayCopy());
        $this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->bsonSerialize());
    }
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
    public function testClone()
    {
        $document = new BSONDocument([
            'a' => [
                'a' => 'foo',
                'b' => new stdClass,
                'c' => ['bar', new stdClass],
            ],
            'b' => new BSONDocument([
                'a' => 'foo',
                'b' => new stdClass,
                'c' => ['bar', new stdClass],
            ]),
        ]);
        $documentClone = clone $document;

        $this->assertSameDocument($document, $documentClone);
        $this->assertNotSame($document, $documentClone);
        $this->assertNotSame($document['a']['b'], $documentClone['a']['b']);
        $this->assertNotSame($document['a']['c'][1], $documentClone['a']['c'][1]);
        $this->assertNotSame($document['b'], $documentClone['b']);
        $this->assertNotSame($document['b']['b'], $documentClone['b']['b']);
        $this->assertNotSame($document['b']['c'][1], $documentClone['b']['c'][1]);
    }

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
    public function testCloneRespectsUncloneableObjects()
    {
        $this->assertFalse((new ReflectionClass(UncloneableObject::class))->isCloneable());

        $document = new BSONDocument([
            'a' => ['a' => new UncloneableObject],
            'b' => new BSONDocument(['a' => new UncloneableObject]),
        ]);
        $documentClone = clone $document;

        $this->assertNotSame($document, $documentClone);
        $this->assertSame($document['a']['a'], $documentClone['a']['a']);
        $this->assertNotSame($document['b'], $documentClone['b']);
        $this->assertSame($document['b']['a'], $documentClone['b']['a']);
    }

    public function testCloneSupportsBSONTypes()
    {
        /* Note: this test does not check that the BSON type itself is cloned,
         * as that is not yet supported in the driver (see: PHPC-1230). */
        $document = new BSONDocument([
            'a' => ['a' => new ObjectId],
            'b' => new BSONDocument(['a' => new ObjectId]),
        ]);
        $documentClone = clone $document;

        $this->assertNotSame($document, $documentClone);
        $this->assertNotSame($document['b'], $documentClone['b']);
    }

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    public function testJsonSerialize()
    {
        $document = new BSONDocument([
            'foo' => 'bar',
            'array' => new BSONArray([1, 2, 3]),
            'object' => new BSONDocument([1, 2, 3]),
            'nested' => new BSONDocument([new BSONDocument([new BSONDocument])]),
        ]);

        $expectedJson = '{"foo":"bar","array":[1,2,3],"object":{"0":1,"1":2,"2":3},"nested":{"0":{"0":{}}}}';

        $this->assertSame($expectedJson, json_encode($document));
    }

    public function testJsonSerializeCastsToObject()
    {
        $data = [0 => 'foo', 2 => 'bar'];

        $document = new BSONDocument($data);
        $this->assertSame($data, $document->getArrayCopy());
        $this->assertEquals((object) [0 => 'foo', 2 => 'bar'], $document->jsonSerialize());
    }

109 110 111 112 113 114 115 116
    public function testSetState()
    {
        $data = ['foo' => 'bar'];

        $document = BSONDocument::__set_state($data);
        $this->assertInstanceOf('MongoDB\Model\BSONDocument', $document);
        $this->assertSame($data, $document->getArrayCopy());
    }
117
}