BSONArrayTest.php 3.3 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 ReflectionClass;
10 11
use stdClass;
use function json_encode;
12 13 14 15 16 17 18 19 20 21 22

class BSONArrayTest extends TestCase
{
    public function testBsonSerializeReindexesKeys()
    {
        $data = [0 => 'foo', 2 => 'bar'];

        $array = new BSONArray($data);
        $this->assertSame($data, $array->getArrayCopy());
        $this->assertSame(['foo', 'bar'], $array->bsonSerialize());
    }
23

24 25 26 27 28
    public function testClone()
    {
        $array = new BSONArray([
            [
                'foo',
29 30
                new stdClass(),
                ['bar', new stdClass()],
31 32 33
            ],
            new BSONArray([
                'foo',
34 35
                new stdClass(),
                ['bar', new stdClass()],
36 37 38 39 40 41 42 43 44 45 46 47 48
            ]),
        ]);
        $arrayClone = clone $array;

        $this->assertSameDocument($array, $arrayClone);
        $this->assertNotSame($array, $arrayClone);
        $this->assertNotSame($array[0][1], $arrayClone[0][1]);
        $this->assertNotSame($array[0][2][1], $arrayClone[0][2][1]);
        $this->assertNotSame($array[1], $arrayClone[1]);
        $this->assertNotSame($array[1][1], $arrayClone[1][1]);
        $this->assertNotSame($array[1][2][1], $arrayClone[1][2][1]);
    }

49 50 51 52 53
    public function testCloneRespectsUncloneableObjects()
    {
        $this->assertFalse((new ReflectionClass(UncloneableObject::class))->isCloneable());

        $array = new BSONArray([
54 55
            [new UncloneableObject()],
            new BSONArray([new UncloneableObject()]),
56 57 58 59 60 61 62 63 64 65 66 67 68 69
        ]);
        $arrayClone = clone $array;

        $this->assertNotSame($array, $arrayClone);
        $this->assertSame($array[0][0], $arrayClone[0][0]);
        $this->assertNotSame($array[1], $arrayClone[1]);
        $this->assertSame($array[1][0], $arrayClone[1][0]);
    }

    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). */
        $array = new BSONArray([
70 71
            [new ObjectId()],
            new BSONArray([new ObjectId()]),
72 73 74 75 76 77 78
        ]);
        $arrayClone = clone $array;

        $this->assertNotSame($array, $arrayClone);
        $this->assertNotSame($array[1], $arrayClone[1]);
    }

79 80 81 82 83 84
    public function testJsonSerialize()
    {
        $document = new BSONArray([
            'foo',
            new BSONArray(['foo' => 1, 'bar' => 2, 'baz' => 3]),
            new BSONDocument(['foo' => 1, 'bar' => 2, 'baz' => 3]),
85
            new BSONArray([new BSONArray([new BSONArray()])]),
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        ]);

        $expectedJson = '["foo",[1,2,3],{"foo":1,"bar":2,"baz":3},[[[]]]]';

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

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

        $array = new BSONArray($data);
        $this->assertSame($data, $array->getArrayCopy());
        $this->assertSame(['foo', 'bar'], $array->jsonSerialize());
    }

102 103 104 105 106
    public function testSetState()
    {
        $data = ['foo', 'bar'];

        $array = BSONArray::__set_state($data);
107
        $this->assertInstanceOf(BSONArray::class, $array);
108 109
        $this->assertSame($data, $array->getArrayCopy());
    }
110
}