Completed
Push — master ( b68a08...d427bc )
by Jérémy
04:52
created

testCanTransformArrayOfArrayObjectToAssociativeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 26
rs 9.7333
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Th3Mouk\RethinkDB\Translator\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use r\Connection;
9
use r\Cursor;
10
use Th3Mouk\RethinkDB\Translator\Translate;
11
12
class TranslateTest extends TestCase
13
{
14
    public function testCanNormalizeArrayObjectValues(): void
15
    {
16
        $values = [new \ArrayObject(['foo' => 'bar']),
17
        ];
18
19
        $this->assertEquals(Translate::normalizeValues($values), [
20
            ['foo' => 'bar'],
21
        ]);
22
    }
23
24
    public function testCanNormalizeDateTimeValues(): void
25
    {
26
        $values = [new \DateTime('2000-05-26T13:30:20+0200')];
27
28
        $this->assertEquals(Translate::normalizeValues($values), ['2000-05-26T13:30:20+0200']);
29
    }
30
31
    public function testCanNormalizeNestedValues(): void
32
    {
33
        $values = [
34
            new \ArrayObject([
35
                'foo' => new \DateTime('2000-05-26T13:30:20+0200'),
36
            ]),
37
        ];
38
39
        $this->assertEquals(Translate::normalizeValues($values), [
40
            ['foo' => '2000-05-26T13:30:20+0200'],
41
        ]);
42
    }
43
44
    public function testCanTransformArrayObjectToAssociativeArray(): void
45
    {
46
        $ao = new \ArrayObject(['foo' => 'bar']);
47
48
        $this->assertEquals(Translate::arrayObjectToAssociativeArray($ao), ['foo' => 'bar']);
49
    }
50
51
    /**
52
     * @throws \ReflectionException
53
     */
54
    public function testCanTransformCursorToAssociativeArray(): void
55
    {
56
        $connection = $this->createMock(Connection::class);
57
58
        $data      = new \stdClass();
59
        $data->foo = 'bar';
60
        $data->bar = 'foo';
61
62
        /** @var Connection $connection */
63
        $cursor = new Cursor(
64
            $connection,
65
            [
66
                'r' => [$data],
67
                't' => 2,
68
            ],
69
            'token',
70
            [],
71
            []
72
        );
73
74
        $this->assertEquals(Translate::cursorToAssociativeArray($cursor), [
75
            [
76
                'foo' => 'bar',
77
                'bar' => 'foo',
78
            ],
79
        ]);
80
    }
81
82
    public function testCanTransformArrayOfArrayObjectToAssociativeArray(): void
83
    {
84
        $mockResult = [];
85
86
        $mockResult[] = new \ArrayObject([
87
            'domains' => ['cine.fr'],
88
            'id' => '46ba3de0-3210-4f7b-aac9-c5176daa0cca',
89
            'status_changed_at' => new \DateTime('2000-05-26T13:30:20+0200'),
90
        ]);
91
92
        $mockResult[] = new \ArrayObject([
93
            'domains' => ['nyxinteractive.eu', 'irond.info'],
94
            'id' => '4640ebcb-48f0-4948-8d5b-93cf08d4249c',
95
            'status_changed_at' => new \DateTime('2000-05-27T13:30:20+0200'),
96
        ]);
97
98
        $this->assertEquals(Translate::arrayOfArrayObjectToAssociativeArray($mockResult), [
99
            [
100
                'domains' => ['cine.fr'],
101
                'id' => '46ba3de0-3210-4f7b-aac9-c5176daa0cca',
102
                'status_changed_at' => '2000-05-26T13:30:20+0200',
103
            ],
104
            [
105
                'domains' => ['nyxinteractive.eu', 'irond.info'],
106
                'id' => '4640ebcb-48f0-4948-8d5b-93cf08d4249c',
107
                'status_changed_at' => '2000-05-27T13:30:20+0200',
108
            ],
109
        ]);
110
    }
111
}
112