Skip to content

Commit 13df455

Browse files
committed
Added Collection test
Switched default value for preserved keys in collapse function in Collection to false
1 parent a3c610e commit 13df455

File tree

4 files changed

+241
-3
lines changed

4 files changed

+241
-3
lines changed

src/Support/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function map(callable $callable): self
6868
/**
6969
* @return ($preserveKeys is true ? Collection<array-key, mixed> : Collection<int, mixed>)
7070
*/
71-
public function collapse(bool $preserveKeys = true): self
71+
public function collapse(bool $preserveKeys = false): self
7272
{
7373
return $this->flatten(1, $preserveKeys);
7474
}

src/Validation/Logic/AndRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function validationMessageTree(): array
1818
return [
1919
'and' => $this->rules
2020
->map(self::resolveValidationMessages(...))
21-
->collapse()
21+
->collapse(preserveKeys: true)
2222
->all()
2323
];
2424
}

src/Validation/Logic/OrRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function validationMessageTree(): array
1818
return [
1919
'or' => $this->rules
2020
->map(self::resolveValidationMessages(...))
21-
->collapse()
21+
->collapse(preserveKeys: true)
2222
->all()
2323
];
2424
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
namespace Nuxtifyts\PhpDto\Tests\Unit\Support;
4+
5+
use Nuxtifyts\PhpDto\Support\Collection;
6+
use Nuxtifyts\PhpDto\Tests\Unit\UnitCase;
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Test;
10+
11+
#[CoversClass(Collection::class)]
12+
final class CollectionTest extends UnitCase
13+
{
14+
/**
15+
* @param Collection<array-key, mixed> $collection
16+
* @param array<string, mixed> $functionParams
17+
*/
18+
#[Test]
19+
#[DataProvider('push_function_provider')]
20+
#[DataProvider('put_function_provider')]
21+
#[DataProvider('first_function_provider')]
22+
#[DataProvider('map_function_provider')]
23+
#[DataProvider('collapse_function_provider')]
24+
#[DataProvider('flatten_function_provider')]
25+
#[DataProvider('all_function_provider')]
26+
#[DataProvider('validation_functions_provider')]
27+
public function will_be_able_to_perform_functions(
28+
Collection $collection,
29+
string $functionName,
30+
array $functionParams,
31+
mixed $expected
32+
): void {
33+
$result = $collection->{$functionName}(...$functionParams);
34+
35+
if ($expected instanceof Collection) {
36+
self::assertInstanceOf(Collection::class, $result);
37+
self::assertCollection($result, $expected);
38+
} else {
39+
self::assertEquals($expected, $result);
40+
}
41+
}
42+
43+
/**
44+
* @return array<string, mixed>
45+
*/
46+
public static function push_function_provider(): array
47+
{
48+
return [
49+
'push' => [
50+
'collection' => new Collection([1, 2, 3]),
51+
'functionName' => 'push',
52+
'functionParams' => [ 'item' => 4 ],
53+
'expected' => new Collection([1, 2, 3, 4])
54+
]
55+
];
56+
}
57+
58+
/**
59+
* @return array<string, mixed>
60+
*/
61+
public static function put_function_provider(): array
62+
{
63+
return [
64+
'put in new key' => [
65+
'collection' => new Collection([1, 2, 3]),
66+
'functionName' => 'put',
67+
'functionParams' => [ 'key' => 3, 'value' => 4 ],
68+
'expected' => new Collection([1, 2, 3, 4])
69+
],
70+
'put in existing key will override value' => [
71+
'collection' => new Collection([1, 2, 3]),
72+
'functionName' => 'put',
73+
'functionParams' => [ 'key' => 0, 'value' => 4 ],
74+
'expected' => new Collection([4, 2, 3])
75+
]
76+
];
77+
}
78+
79+
/**
80+
* @return array<string, mixed>
81+
*/
82+
public static function first_function_provider(): array
83+
{
84+
return [
85+
'first without callable and non empty collection' => [
86+
'collection' => new Collection([1, 2, 3]),
87+
'functionName' => 'first',
88+
'functionParams' => [],
89+
'expected' => 1
90+
],
91+
'first without callable and empty collection' => [
92+
'collection' => new Collection([]),
93+
'functionName' => 'first',
94+
'functionParams' => [],
95+
'expected' => null
96+
],
97+
'first with callable and existing item that will meet requirements' => [
98+
'collection' => new Collection([1, 2, 3]),
99+
'functionName' => 'first',
100+
'functionParams' => [ 'callable' => static fn (int $item) => $item === 2 ],
101+
'expected' => 2
102+
],
103+
'first with callable and no item that will meet requirements' => [
104+
'collection' => new Collection([1, 2, 3]),
105+
'functionName' => 'first',
106+
'functionParams' => [ 'callable' => static fn (int $item) => $item === 4 ],
107+
'expected' => null
108+
]
109+
];
110+
}
111+
112+
/**
113+
* @return array<string, mixed>
114+
*/
115+
public static function map_function_provider(): array
116+
{
117+
return [
118+
'map' => [
119+
'collection' => new Collection([1, 2, 3]),
120+
'functionName' => 'map',
121+
'functionParams' => [ 'callable' => static fn (int $item) => $item * 2 ],
122+
'expected' => new Collection([2, 4, 6])
123+
]
124+
];
125+
}
126+
127+
/**
128+
* @return array<string, mixed>
129+
*/
130+
public static function collapse_function_provider(): array
131+
{
132+
return [
133+
'collapse' => [
134+
'collection' => new Collection([
135+
new Collection([ 'a' => 1, 2, 3]),
136+
new Collection([4, 5, 6]),
137+
new Collection([7, 8, 9])
138+
]),
139+
'functionName' => 'collapse',
140+
'functionParams' => [],
141+
'expected' => new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9])
142+
],
143+
144+
];
145+
}
146+
147+
/**
148+
* @return array<string, mixed>
149+
*/
150+
public static function flatten_function_provider(): array
151+
{
152+
return [
153+
'flatten' => [
154+
'collection' => new Collection([
155+
'a1' => new Collection([
156+
'a1.1' => 1.1,
157+
'a1.2' => new Collection([
158+
'a1.2.1' => 1.21,
159+
'a1.2.2' => 1.22,
160+
'a1.2.3' => new Collection([
161+
'a1.2.3.1' => 1.231,
162+
'a1.2.3.2' => 1.232,
163+
'a1.2.3.3' => 1.233
164+
])
165+
])
166+
])
167+
]),
168+
'functionName' => 'flatten',
169+
'functionParams' => [],
170+
'expected' => new Collection([
171+
'a1.1' => 1.1,
172+
'a1.2.1' => 1.21,
173+
'a1.2.2' => 1.22,
174+
'a1.2.3.1' => 1.231,
175+
'a1.2.3.2' => 1.232,
176+
'a1.2.3.3' => 1.233
177+
])
178+
]
179+
];
180+
}
181+
182+
/**
183+
* @return array<string, mixed>
184+
*/
185+
public static function all_function_provider(): array
186+
{
187+
return [
188+
'all' => [
189+
'collection' => new Collection([1, 2, 3]),
190+
'functionName' => 'all',
191+
'functionParams' => [],
192+
'expected' => [1, 2, 3]
193+
]
194+
];
195+
}
196+
197+
/**
198+
* @return array<string, mixed>
199+
*/
200+
public static function validation_functions_provider(): array
201+
{
202+
return [
203+
'isEmpty' => [
204+
'collection' => new Collection([]),
205+
'functionName' => 'isEmpty',
206+
'functionParams' => [],
207+
'expected' => true
208+
],
209+
'isNotEmpty' => [
210+
'collection' => new Collection([1, 2, 3]),
211+
'functionName' => 'isNotEmpty',
212+
'functionParams' => [],
213+
'expected' => true
214+
],
215+
'every' => [
216+
'collection' => new Collection([1, 2, 3]),
217+
'functionName' => 'every',
218+
'functionParams' => [ 'callable' => static fn (int $item) => $item > 0 ],
219+
'expected' => true
220+
],
221+
'some' => [
222+
'collection' => new Collection([1, 2, 3]),
223+
'functionName' => 'some',
224+
'functionParams' => [ 'callable' => static fn (int $item) => $item === 2 ],
225+
'expected' => true
226+
],
227+
];
228+
}
229+
230+
/**
231+
* @param Collection<array-key, mixed> $collection
232+
* @param Collection<array-key, mixed> $expected
233+
*/
234+
private static function assertCollection(Collection $collection, Collection $expected): void
235+
{
236+
self::assertEquals($expected->all(), $collection->all());
237+
}
238+
}

0 commit comments

Comments
 (0)