BSONArrayTest.php 2.2 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 stdClass;
9 10 11 12 13 14 15 16 17 18 19

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

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    public function testClone()
    {
        $array = new BSONArray([
            [
                'foo',
                new stdClass,
                ['bar', new stdClass],
            ],
            new BSONArray([
                'foo',
                new stdClass,
                ['bar', new stdClass],
            ]),
        ]);
        $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]);
    }

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    public function testJsonSerialize()
    {
        $document = new BSONArray([
            'foo',
            new BSONArray(['foo' => 1, 'bar' => 2, 'baz' => 3]),
            new BSONDocument(['foo' => 1, 'bar' => 2, 'baz' => 3]),
            new BSONArray([new BSONArray([new BSONArray])]),
        ]);

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

69 70 71 72 73 74 75 76
    public function testSetState()
    {
        $data = ['foo', 'bar'];

        $array = BSONArray::__set_state($data);
        $this->assertInstanceOf('MongoDB\Model\BSONArray', $array);
        $this->assertSame($data, $array->getArrayCopy());
    }
77
}