-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapToCollectionTest.php
97 lines (88 loc) · 3.54 KB
/
MapToCollectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
use Illuminate\Support\Collection;
use Pulli\CollectionMacros\Test\Data\ChildObject;
use Pulli\CollectionMacros\Test\Data\OtherObject;
use Pulli\CollectionMacros\Test\Data\ParentObject;
use Pulli\CollectionMacros\Test\Data\TestObject;
describe('mapToCollection macro', function () {
it('wraps all arrays into collection objects', function () {
$data = Collection::mapToCollection([
['test' => ['test' => '1.1']],
['test' => ['test' => '1.2']],
['test' => ['test' => ['test' => '1.3.1']]],
]);
expect($data[0])->toBeInstanceOf(Collection::class)
->and($data[1])->toBeInstanceOf(Collection::class)
->and($data[0]['test'])->toBeInstanceOf(Collection::class)
->and($data[1]['test'])->toBeInstanceOf(Collection::class)
->and($data->get(0)->get('test')->get('test'))->toBe('1.1')
->and($data->get(1)->get('test')->get('test'))->toBe('1.2')
->and($data->get(2)->get('test')->get('test')->get('test'))->toBe('1.3.1');
});
it('wraps data objects into collection objects', function () {
$data = Collection::mapToCollection([
new TestObject('John Doe', 'Programmer'),
new TestObject('Jane Doe', 'Designer'),
], true);
expect($data[0])->toBeInstanceOf(Collection::class)
->and($data[1])->toBeInstanceOf(Collection::class)
->and($data->get(0))->toEqual(new Collection([
'name' => 'John Doe',
'job' => 'Programmer',
]))
->and($data->get(1))->toEqual(new Collection([
'name' => 'Jane Doe',
'job' => 'Designer',
]));
});
it('wraps nested data objects into collection objects', function () {
$data = Collection::mapToCollection([
['first' => new TestObject('John Doe', 'Programmer')],
['second' => new TestObject('Jane Doe', 'Designer')],
], true);
expect($data[0])->toBeInstanceOf(Collection::class)
->and($data[1])->toBeInstanceOf(Collection::class)
->and($data->get(0)->get('first'))->toEqual(new Collection([
'name' => 'John Doe',
'job' => 'Programmer',
]))
->and($data->get(1)->get('second'))->toEqual(new Collection([
'name' => 'Jane Doe',
'job' => 'Designer',
]));
});
it('wraps nested arrays and objects into collection objects', function () {
$data = Collection::mapToCollection([
new ParentObject(
name: 'parent1',
children: Collection::make([new ChildObject(name: 'child1')]),
other: Collection::make([new OtherObject(value: 'other1')]),
),
new ParentObject(
name: 'parent2',
children: Collection::make([new ChildObject(name: 'child2')]),
other: Collection::make([new OtherObject(value: 'other2')]),
),
], true);
expect($data->toArray())->toEqual([
[
'name' => 'parent1',
'children' => [
['name' => 'child1'],
],
'other' => [
['value' => 'other1'],
],
],
[
'name' => 'parent2',
'children' => [
['name' => 'child2'],
],
'other' => [
['value' => 'other2'],
],
],
]);
});
});