Passed
Push — master ( ff3bbf...a365cf )
by Tomáš
01:33
created

anonymous//tests/ConvertorTest.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 10
wmc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Arrayable\Tests;
6
7
use ArrayIterator;
8
use Arrayable as CoreArrayable;
9
use Inspirum\Arrayable\Arrayable;
10
use Inspirum\Arrayable\Convertor;
11
use PHPUnit\Framework\TestCase;
12
use RuntimeException;
13
use Throwable;
14
use UnexpectedValueException;
15
use stdClass;
16
17
final class ConvertorTest extends TestCase
18
{
19
    /**
20
     * @param array<mixed> $result
21
     *
22
     * @dataProvider providesToArray
23
     */
24
    public function testToArray(mixed $data, array|Throwable $result, ?int $limit = null): void
25
    {
26
        if ($result instanceof Throwable) {
27
            self::expectException($result::class);
28
            self::expectExceptionMessage($result->getMessage());
29
        }
30
31
        self::assertSame(!($result instanceof Throwable), Convertor::isArrayable($data));
32
        self::assertEquals($result, Convertor::toArray($data, $limit));
33
    }
34
35
    /**
36
     * @return iterable<array<string, mixed>>
37
     */
38
    public function providesToArray(): iterable
39
    {
40
        yield [
41
            'data'   => [1, 3, 4],
42
            'result' => [1, 3, 4],
43
        ];
44
45
        yield [
46
            'data'   => '1',
47
            'result' => new RuntimeException('Cannot cast to array'),
48
        ];
49
50
        yield [
51
            'data'   => [
52
                2,
53
                (static function () {
54
                    $o        = new stdClass();
55
                    $o->foo   = 'bar';
56
                    $o->debug = true;
57
58
                    return $o;
59
                })(),
60
            ],
61
            'result' => [
62
                2,
63
                [
64
                    'foo'   => 'bar',
65
                    'debug' => true,
66
                ],
67
            ],
68
        ];
69
70
        yield [
71
            'data'   => new class implements Arrayable {
72
                /**
73
                 * @return array<mixed>
74
                 */
75
                public function __toArray(): array
76
                {
77
                    return ['1', 2, 'test'];
78
                }
79
            },
80
            'result' => ['1', 2, 'test'],
81
        ];
82
83
        yield [
84
            'data'   => new class implements CoreArrayable {
85
                /**
86
                 * @return array<mixed>
87
                 */
88
                public function __toArray(): array
89
                {
90
                    return ['1' => 2, '3' => 5];
91
                }
92
            },
93
            'result' => ['1' => 2, '3' => 5],
94
        ];
95
96
        yield [
97
            'data'   => new ArrayIterator([3, 4, 5]),
98
            'result' => [3, 4, 5],
99
        ];
100
101
        yield [
102
            'data'   => (static function () {
103
                yield 2.3;
104
                yield 8.10;
105
            })(),
106
            'result' => [2.3, 8.10],
107
        ];
108
109
        yield [
110
            'data'   => [
111
                1,
112
                3,
113
                [
114
                    3,
115
                    new ArrayIterator([
116
                        3,
117
                        4,
118
                        (static function () {
119
                            yield 2.3;
120
                            yield new class implements Arrayable {
121
                                /**
122
                                 * @return array<mixed>
123
                                 */
124
                                public function __toArray(): array
125
                                {
126
                                    return ['1', 2, 'test'];
127
                                }
128
                            };
129
                        })(),
130
                    ]),
131
                ],
132
            ],
133
            'result' => [1, 3, [3, [3, 4, [2.3, ['1', 2, 'test']]]]],
134
        ];
135
136
        yield [
137
            'data'   => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
138
            'result' => [1, [3, [4, [3, 4, 5]]]],
139
        ];
140
141
        yield [
142
            'data'   => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
143
            'result' => [1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])],
144
            'limit'  => 1,
145
        ];
146
147
        yield [
148
            'data'   => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
149
            'result' => [1, [3, [4, new ArrayIterator([3, 4, 5])]]],
150
            'limit'  => 3,
151
        ];
152
153
        yield [
154
            'data'   => '1',
155
            'result' => new UnexpectedValueException('Limit value should be positive number'),
156
            'limit'  => -1,
157
        ];
158
159
        yield [
160
            'data'   => '1',
161
            'result' => new UnexpectedValueException('Limit value should be positive number'),
162
            'limit'  => 0,
163
        ];
164
    }
165
}
166