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

BaseCollectionTest.php$0 ➔ providesToArray()   A

Complexity

Conditions 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 15
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Arrayable\Tests;
6
7
use Inspirum\Arrayable\Arrayable;
8
use Inspirum\Arrayable\BaseCollection;
9
use PHPUnit\Framework\TestCase;
10
use Throwable;
11
use function array_map;
12
use function iterator_to_array;
13
use function json_encode;
14
15
final class BaseCollectionTest extends TestCase
16
{
17
    /**
18
     * @param array<array<mixed>> $data
19
     *
20
     * @dataProvider providesToArray
21
     */
22
    public function testJsonSerialize(array $data, string|Throwable $result): void
23
    {
24
        if ($result instanceof Throwable) {
25
            self::expectException($result::class);
26
            self::expectExceptionMessage($result->getMessage());
27
        }
28
29
        $items = array_map(fn(array $data): Arrayable => $this->createModel($data), $data);
30
31
        $collection = new class ($items) extends BaseCollection {
32
        };
33
34
        self::assertSame($data, $collection->__toArray());
35
        self::assertSame($data, $collection->toArray());
36
        self::assertSame($data, $collection->jsonSerialize());
37
        self::assertSame($result, (string) $collection);
38
        self::assertSame($result, json_encode($collection));
39
    }
40
41
    /**
42
     * @return iterable<array<string, mixed>>
43
     */
44
    public function providesToArray(): iterable
45
    {
46
        yield [
47
            'data'   => [
48
                [1, 3, 4],
49
            ],
50
            'result' => '[[1,3,4]]',
51
        ];
52
53
        yield [
54
            'data'   => [
55
                [1, 3, 4],
56
                ['a' => 1, 3, 'c' => true, 4],
57
            ],
58
            'result' => '[[1,3,4],{"a":1,"0":3,"c":true,"1":4}]',
59
        ];
60
    }
61
62
    public function testCollection(): void
63
    {
64
        $items = array_map(fn(array $data): Arrayable => $this->createModel($data), ['first' => [1, 2, 3], 'second' => [4, 5, 6], 'third' => [7, 8, 9]]);
65
66
        $collection = new class ($items) extends BaseCollection {
67
        };
68
69
        self::assertCount(3, $collection);
70
        self::assertSame($items, $collection->getItems());
71
72
        self::assertFalse(isset($collection[0]));
73
        self::assertTrue(isset($collection['first']));
74
        unset($collection['first']);
75
        self::assertFalse(isset($collection['first']));
76
        self::assertSame([4, 5, 6], $collection['second']->__toArray());
77
        $collection['second'] = $this->createModel(['foo' => 'bar']);
78
        $collection->offsetAdd($this->createModel(['bar1' => 'foo']));
79
        $collection->offsetAdd($this->createModel(['bar2' => 'foo']));
80
81
        self::assertSame(['foo' => 'bar'], $collection['second']->__toArray());
82
        self::assertSame(['bar2' => 'foo'], $collection[1]->__toArray());
83
84
        self::assertSame($collection->getItems(), iterator_to_array($collection));
85
    }
86
87
    /**
88
     * @param array<mixed, mixed> $data
89
     *
90
     * @return \Inspirum\Arrayable\Arrayable<int|string, mixed>
91
     */
92
    private function createModel(array $data): Arrayable
93
    {
94
        return new class ($data) implements Arrayable {
95
            /**
96
             * @param array<mixed> $data
97
             */
98
            public function __construct(private array $data)
99
            {
100
            }
101
102
            /**
103
             * @return array<mixed>
104
             */
105
            public function __toArray(): array
106
            {
107
                return $this->data;
108
            }
109
        };
110
    }
111
}
112