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

3
namespace MongoDB\Tests\Model;
4

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

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

23 24 25 26 27 28 29 30
    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());
    }
31

32 33 34 35 36
    public function testClone()
    {
        $document = new BSONDocument([
            'a' => [
                'a' => 'foo',
37 38
                'b' => new stdClass(),
                'c' => ['bar', new stdClass()],
39 40 41
            ],
            'b' => new BSONDocument([
                'a' => 'foo',
42 43
                'b' => new stdClass(),
                'c' => ['bar', new stdClass()],
44 45 46 47 48 49 50 51 52 53 54 55 56
            ]),
        ]);
        $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]);
    }

57 58 59 60 61
    public function testCloneRespectsUncloneableObjects()
    {
        $this->assertFalse((new ReflectionClass(UncloneableObject::class))->isCloneable());

        $document = new BSONDocument([
62 63
            'a' => ['a' => new UncloneableObject()],
            'b' => new BSONDocument(['a' => new UncloneableObject()]),
64 65 66 67 68 69 70 71 72 73 74 75 76 77
        ]);
        $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([
78 79
            'a' => ['a' => new ObjectId()],
            'b' => new BSONDocument(['a' => new ObjectId()]),
80 81 82 83 84 85 86
        ]);
        $documentClone = clone $document;

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

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

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

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

        $document = BSONDocument::__set_state($data);
115
        $this->assertInstanceOf(BSONDocument::class, $document);
116 117
        $this->assertSame($data, $document->getArrayCopy());
    }
118
}