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

3
namespace MongoDB\Tests\Model;
4

5
use MongoDB\Model\BSONArray;
6
use MongoDB\Model\BSONDocument;
7
use MongoDB\Tests\TestCase;
8
use ArrayObject;
9
use stdClass;
10 11 12

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

20 21 22 23 24 25 26 27
    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());
    }
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    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]);
    }

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    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());
    }

77 78 79 80 81 82 83 84
    public function testSetState()
    {
        $data = ['foo' => 'bar'];

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