Completed
Pull Request — master (#3)
by
unknown
02:00
created

testCanTransformCursorToAssociativeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 23
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Th3Mouk\RethinkDB\Translator\Tests;
6
7
use ArrayObject;
8
use DateTime;
9
use PHPUnit\Framework\TestCase;
10
use r\Connection;
11
use r\Cursor;
12
use stdClass;
13
use Th3Mouk\RethinkDB\Translator\Translate;
14
15
class TranslateTest extends TestCase
16
{
17
    public function testCanNormalizeArrayObjectValues(): void
18
    {
19
        $values = [new ArrayObject(['foo' => 'bar']),
20
        ];
21
22
        $this->assertEquals(Translate::normalizeValues($values), [
23
            ['foo' => 'bar'],
24
        ]);
25
    }
26
27
    public function testCanNormalizeDateTimeValues(): void
28
    {
29
        $values = [new DateTime('2000-05-26T13:30:20')];
30
31
        $this->assertEquals(Translate::normalizeValues($values), ['2000-05-26T13:30:20+0200']);
32
    }
33
34
    public function testCanNormalizeNestedValues(): void
35
    {
36
        $values = [new ArrayObject([
37
            'foo' => new DateTime('2000-05-26T13:30:20'),
38
        ]),
39
        ];
40
41
        $this->assertEquals(Translate::normalizeValues($values), [
42
            ['foo' => '2000-05-26T13:30:20+0200'],
43
        ]);
44
    }
45
46
    public function testCanOverrideDateTimeToStringOption(): void
47
    {
48
        $values = [new ArrayObject([
49
            'foo' => new DateTime('2000-05-26T13:30:20'),
50
        ]),
51
        ];
52
53
        $normalized = Translate::normalizeValues($values, ['dateTimeToString' => false]);
54
55
        $this->assertInstanceOf(DateTime::class, $normalized[0]['foo']);
56
        $this->assertEquals($normalized[0]['foo']->format(DATE_ISO8601), '2000-05-26T13:30:20+0200');
57
    }
58
59
    public function testCanTransformArrayObjectToAssociativeArray(): void
60
    {
61
        $ao = new ArrayObject(['foo' => 'bar']);
62
63
        $this->assertEquals(Translate::arrayObjectToAssociativeArray($ao), ['foo' => 'bar']);
64
    }
65
66
    public function testCanTransformCursorToAssociativeArray(): void
67
    {
68
        $connection = $this->createMock(Connection::class);
69
70
        $data      = new stdClass();
71
        $data->foo = 'bar';
72
        $data->bar = 'foo';
73
74
        $cursor = new Cursor(
75
            $connection,
76
            [
77
                'r' => [$data],
78
                't' => 2,
79
            ],
80
            'token',
81
            [],
82
            []
83
        );
84
85
        $this->assertEquals(Translate::cursorToAssociativeArray($cursor), [
86
            [
87
                'foo' => 'bar',
88
                'bar' => 'foo',
89
            ],
90
        ]);
91
    }
92
}
93