Skip to content

Commit 6402803

Browse files
committed
Add failing tests
1 parent c04e4e8 commit 6402803

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

tests/ArrayHelper/FindTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Arrays\Tests\ArrayHelper;
6+
7+
use Closure;
8+
use PHPUnit\Framework\TestCase;
9+
use Yiisoft\Arrays\ArrayHelper;
10+
11+
final class FindTest extends TestCase
12+
{
13+
private array $array = [
14+
[
15+
"a" => 1,
16+
"b" => 2,
17+
"c" => 3,
18+
"d" => 4,
19+
"e" => 5,
20+
],
21+
[
22+
1, 2, 3, 4, 5
23+
],
24+
];
25+
26+
public function dataProviderFindFromArray(): array
27+
{
28+
return [
29+
[$this->array[0], fn ($value) => $value > 3, 4],
30+
[$this->array[1], fn ($value) => $value > 3, 4],
31+
[$this->array[1], fn ($value) => $value > 5, null],
32+
[$this->array[0], fn ($value, $key) => $key === "c", 3],
33+
[$this->array[0], fn () => false, null],
34+
[[], fn () => true, null],
35+
];
36+
}
37+
38+
/**
39+
* @dataProvider dataProviderFindFromArray
40+
*
41+
* @param Closure $predicate
42+
* @param $expected
43+
*/
44+
public function testFind($array, $predicate, $expected): void
45+
{
46+
$this->assertEquals($expected, ArrayHelper::find($array, $predicate));
47+
}
48+
49+
public function dataProviderFindKeyFromArray(): array
50+
{
51+
return [
52+
[$this->array[0], fn ($value) => $value > 3, "d"],
53+
[$this->array[1], fn ($value) => $value > 3, 3],
54+
[$this->array[1], fn ($value) => $value > 5, null],
55+
[$this->array[0], fn ($value, $key) => $key === "c", "c"],
56+
[$this->array[0], fn () => false, null],
57+
[[], fn () => true, null],
58+
];
59+
}
60+
61+
/**
62+
* @dataProvider dataProviderFindKeyFromArray
63+
*
64+
* @param Closure $predicate
65+
* @param $expected
66+
*/
67+
public function testFindKey($array, $predicate, $expected): void
68+
{
69+
$this->assertEquals($expected, ArrayHelper::findKey($array, $predicate));
70+
}
71+
72+
public function dataProviderAnyFromArray(): array
73+
{
74+
return [
75+
[$this->array[0], fn ($value) => $value > 3, true],
76+
[$this->array[1], fn ($value) => $value > 3, true],
77+
[$this->array[1], fn ($value) => $value > 5, false],
78+
[$this->array[0], fn ($value, $key) => $key === "c", true],
79+
[$this->array[0], fn () => false, false],
80+
[[], fn () => true, false],
81+
];
82+
}
83+
84+
/**
85+
* @dataProvider dataProviderAnyFromArray
86+
*
87+
* @param Closure $predicate
88+
* @param $expected
89+
*/
90+
public function testAny($array, $predicate, $expected): void
91+
{
92+
$this->assertEquals($expected, ArrayHelper::any($array, $predicate));
93+
}
94+
95+
public function dataProviderAllFromArray(): array
96+
{
97+
return [
98+
[$this->array[0], fn ($value) => $value > 0, true],
99+
[$this->array[1], fn ($value) => $value > 0, true],
100+
[$this->array[1], fn ($value) => $value > 1, false],
101+
[[], fn () => true, true],
102+
];
103+
}
104+
105+
/**
106+
* @dataProvider dataProviderAllFromArray
107+
*
108+
* @param Closure $predicate
109+
* @param $expected
110+
*/
111+
public function testAll($array, $predicate, $expected): void
112+
{
113+
$this->assertEquals($expected, ArrayHelper::all($array, $predicate));
114+
}
115+
}

0 commit comments

Comments
 (0)