Skip to content

Commit 7270cce

Browse files
test none, some
1 parent 886f171 commit 7270cce

File tree

10 files changed

+243
-15
lines changed

10 files changed

+243
-15
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"files": [
3030
"src/CallbackArgs.php",
3131
"src/IndexOf.php",
32+
"src/None.php",
3233
"src/Contains.php",
3334
"src/All.php",
3435
"src/AtLeast.php",
@@ -43,6 +44,7 @@
4344
"src/Remove.php",
4445
"src/RemoveKey.php",
4546
"src/Pull.php",
47+
"src/Some.php",
4648
"src/array_functions.php"
4749
]
4850
},

src/AtLeast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace BrenoRoosevelt;
55

66
/**
7-
* Evaluates whether <b>at least n</b> elements match the specification
7+
* Evaluates whether <b>at least n</b> match the specification
88
*
99
* @param int $n The number to check
1010
* @param iterable $items The collection

src/AtMost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace BrenoRoosevelt;
55

66
/**
7-
* Evaluates whether <b>at most n</b> elements match the specification
7+
* Evaluates if <b>at most n</b> match the specification
88
*
99
* @param int $n The number to check
1010
* @param iterable $items The collection

src/Exactly.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace BrenoRoosevelt;
55

66
/**
7-
* Evaluates whether <b>exactly n</b> elements match the specification
7+
* Evaluates if <b>exactly n</b> match the specification
88
*
99
* @param int $n The number to check
1010
* @param iterable $items The collection

src/None.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Evaluates if none matches the specification
8+
*
9+
* @param iterable $items The collection
10+
* @param callable $callback Callable must return a boolean
11+
* @param int $mode [optional] <p>
12+
* Flag determining what arguments are sent to <i>callback</i>:
13+
* </p><ul>
14+
* <li>
15+
* <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
16+
* </li>
17+
* <li>
18+
* <b>CALLBACK_USE_KEY</b> - pass key as the only argument
19+
* to <i>callback</i> instead of the value</span>
20+
* </li>
21+
* <li>
22+
* <b>CALLBACK_USE_BOTH</b> - pass both value and key as
23+
* arguments to <i>callback</i></span>
24+
* </li>
25+
* </ul>
26+
* @return bool
27+
*/
28+
function none(iterable $items, callable $callback, int $mode = 0): bool
29+
{
30+
return ! some($items, $callback, $mode);
31+
}

src/Some.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt;
5+
6+
/**
7+
* Evaluates if some matches the specification
8+
*
9+
* @param iterable $items The collection
10+
* @param callable $callback Callable must return a boolean
11+
* @param int $mode [optional] <p>
12+
* Flag determining what arguments are sent to <i>callback</i>:
13+
* </p><ul>
14+
* <li>
15+
* <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
16+
* </li>
17+
* <li>
18+
* <b>CALLBACK_USE_KEY</b> - pass key as the only argument
19+
* to <i>callback</i> instead of the value</span>
20+
* </li>
21+
* <li>
22+
* <b>CALLBACK_USE_BOTH</b> - pass both value and key as
23+
* arguments to <i>callback</i></span>
24+
* </li>
25+
* </ul>
26+
* @return bool
27+
*/
28+
function some(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
29+
{
30+
return at_least(1, $items, $callback, $mode);
31+
}

src/array_functions.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33

44
namespace BrenoRoosevelt;
55

6-
function some(iterable $items, callable $callback, int $mode = CALLBACK_USE_VALUE): bool
7-
{
8-
return at_least(1, $items, $callback, $mode);
9-
}
10-
11-
function none(iterable $items, callable $callback, int $mode = 0): bool
12-
{
13-
return ! some($items, $callback, $mode);
14-
}
15-
166
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
177
{
188
foreach ($items as $key => $value) {

tests/NoneTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\none;
8+
use const BrenoRoosevelt\CALLBACK_USE_BOTH;
9+
use const BrenoRoosevelt\CALLBACK_USE_KEY;
10+
use const BrenoRoosevelt\CALLBACK_USE_VALUE;
11+
12+
class NoneTest extends TestCase
13+
{
14+
public function noneProvider(): array
15+
{
16+
return [
17+
'case_1' => [
18+
['a', 'b', 'c'], // collection
19+
fn($el) => false, // callback
20+
CALLBACK_USE_VALUE, // mode
21+
true, // expected
22+
],
23+
'case_2' => [
24+
['a', 'b', 'c'],
25+
fn($el) => true,
26+
CALLBACK_USE_VALUE,
27+
false,
28+
],
29+
'case_3' => [
30+
[],
31+
fn($el) => false,
32+
CALLBACK_USE_VALUE,
33+
true,
34+
],
35+
'case_3_1' => [
36+
[],
37+
fn($el) => true,
38+
CALLBACK_USE_VALUE,
39+
true,
40+
],
41+
'case_4' => [
42+
[1, 2, 3, 4],
43+
fn($el) => $el === 2,
44+
CALLBACK_USE_VALUE,
45+
false,
46+
],
47+
'case_5' => [
48+
[1, 2, 3, 4],
49+
fn($el) => $el === 0,
50+
CALLBACK_USE_VALUE,
51+
true,
52+
],
53+
'case_6' => [
54+
[1, 2, 3, 4],
55+
fn($key) => is_string($key),
56+
CALLBACK_USE_KEY,
57+
true,
58+
],
59+
'case_6_1' => [
60+
[1, 2, 3, 4, 'a' => 5],
61+
fn($key) => is_string($key),
62+
CALLBACK_USE_KEY,
63+
false,
64+
],
65+
'case_7' => [
66+
['a' => 1, 'b' => 2, 'c' => 3, 4],
67+
fn($v, $key) => is_integer($key) && is_integer($v),
68+
CALLBACK_USE_BOTH,
69+
false,
70+
]
71+
];
72+
}
73+
74+
/**
75+
* @param iterable $items
76+
* @param callable $callback
77+
* @param int $mode
78+
* @param bool $expected
79+
* @return void
80+
* @dataProvider noneProvider
81+
*/
82+
public function testNone(iterable $items, callable $callback, int $mode, bool $expected): void
83+
{
84+
$actual = none($items, $callback, $mode);
85+
$this->assertEquals($expected, $actual);
86+
}
87+
}

tests/SetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class SetTest extends TestCase
1010
{
11-
public function addProvider(): array
11+
public function setProvider(): array
1212
{
1313
return [
1414
'case_1' => [
@@ -32,7 +32,7 @@ public function addProvider(): array
3232
* @param $key
3333
* @param array $expected
3434
* @return void
35-
* @dataProvider addProvider
35+
* @dataProvider setProvider
3636
*/
3737
public function testAdd(array $items, $element, $key, array $expected): void
3838
{

tests/SomeTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace BrenoRoosevelt\Tests;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use function BrenoRoosevelt\some;
8+
use const BrenoRoosevelt\CALLBACK_USE_BOTH;
9+
use const BrenoRoosevelt\CALLBACK_USE_KEY;
10+
use const BrenoRoosevelt\CALLBACK_USE_VALUE;
11+
12+
class SomeTest extends TestCase
13+
{
14+
public function someProvider(): array
15+
{
16+
return [
17+
'case_1' => [
18+
['a', 'b', 'c'], // collection
19+
fn($el) => false, // callback
20+
CALLBACK_USE_VALUE, // mode
21+
false, // expected
22+
],
23+
'case_2' => [
24+
['a', 'b', 'c'],
25+
fn($el) => true,
26+
CALLBACK_USE_VALUE,
27+
true,
28+
],
29+
'case_3' => [
30+
[],
31+
fn($el) => false,
32+
CALLBACK_USE_VALUE,
33+
false,
34+
],
35+
'case_3_1' => [
36+
[],
37+
fn($el) => true,
38+
CALLBACK_USE_VALUE,
39+
false,
40+
],
41+
'case_4' => [
42+
[1, 2, 3, 4],
43+
fn($el) => $el === 2,
44+
CALLBACK_USE_VALUE,
45+
true,
46+
],
47+
'case_5' => [
48+
[1, 2, 3, 4],
49+
fn($el) => $el === 0,
50+
CALLBACK_USE_VALUE,
51+
false,
52+
],
53+
'case_6' => [
54+
[1, 2, 3, 4],
55+
fn($key) => is_string($key),
56+
CALLBACK_USE_KEY,
57+
false,
58+
],
59+
'case_6_1' => [
60+
[1, 2, 3, 4, 'a' => 5],
61+
fn($key) => is_string($key),
62+
CALLBACK_USE_KEY,
63+
true,
64+
],
65+
'case_7' => [
66+
['a' => 1, 'b' => 2, 'c' => 3, 4],
67+
fn($v, $key) => is_integer($key) && is_integer($v),
68+
CALLBACK_USE_BOTH,
69+
true,
70+
]
71+
];
72+
}
73+
74+
/**
75+
* @param iterable $items
76+
* @param callable $callback
77+
* @param int $mode
78+
* @param bool $expected
79+
* @return void
80+
* @dataProvider someProvider
81+
*/
82+
public function testSome(iterable $items, callable $callback, int $mode, bool $expected): void
83+
{
84+
$actual = some($items, $callback, $mode);
85+
$this->assertEquals($expected, $actual);
86+
}
87+
}

0 commit comments

Comments
 (0)