From 5881dd6f55a70eb6679020c2c0ad62609529e183 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Fri, 8 May 2020 15:17:06 +0200 Subject: [PATCH] feat(every): add supports --- README.md | 1 + src/Chain.php | 2 + src/Link/Every.php | 26 ++++++++++++ tests/ChainTest.php | 1 + tests/Link/EveryTest.php | 87 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 src/Link/Every.php create mode 100644 tests/Link/EveryTest.php diff --git a/README.md b/README.md index 6563ecc..751479c 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ $chain->reduce(function ($current, $value) { - `->count()` - `->countValues()` +- `->every(callable)` - `->first()` - `->join([$glue])` - `->last()` diff --git a/src/Chain.php b/src/Chain.php index 84860c2..80eae03 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -7,6 +7,7 @@ use Cocur\Chain\Link\Count; use Cocur\Chain\Link\CountValues; use Cocur\Chain\Link\Diff; +use Cocur\Chain\Link\Every; use Cocur\Chain\Link\Filter; use Cocur\Chain\Link\First; use Cocur\Chain\Link\Find; @@ -55,6 +56,7 @@ class Chain extends AbstractChain implements Countable use Count; use CountValues; use Diff; + use Every; use Filter; use Find; use First; diff --git a/src/Link/Every.php b/src/Link/Every.php new file mode 100644 index 0000000..06182c6 --- /dev/null +++ b/src/Link/Every.php @@ -0,0 +1,26 @@ +array as $index => $element) { + $pass = $pass && $callback($element, $index); + } + + return $pass; + } +} diff --git a/tests/ChainTest.php b/tests/ChainTest.php index acfecba..c8fcf55 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -81,6 +81,7 @@ public function chainHasTraits(): void $this->assertTrue(method_exists($c, 'count')); $this->assertTrue(method_exists($c, 'countValues')); $this->assertTrue(method_exists($c, 'diff')); + $this->assertTrue(method_exists($c, 'every')); $this->assertTrue(method_exists($c, 'filter')); $this->assertTrue(method_exists($c, 'find')); $this->assertTrue(method_exists($c, 'first')); diff --git a/tests/Link/EveryTest.php b/tests/Link/EveryTest.php new file mode 100644 index 0000000..7ffa0f7 --- /dev/null +++ b/tests/Link/EveryTest.php @@ -0,0 +1,87 @@ +getMockForTrait(Every::class); + $mock->array = [1, 2, 3, 4]; + + $this->assertTrue($mock->every(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Every::every() + */ + public function everyReturnFalseWhenConditionFail(): void + { + /** @var Every $mock */ + $mock = $this->getMockForTrait(Every::class); + $mock->array = [-1, -2, -3, -4]; + + $this->assertFalse($mock->every(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Every::every() + */ + public function everyReturnFalseWhenConditionFailOnSomeElements(): void + { + /** @var Every $mock */ + $mock = $this->getMockForTrait(Every::class); + $mock->array = [1, 2, -3, 4]; + + $this->assertFalse($mock->every(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Every::every() + */ + public function everyReturnTrueWithEmptyChain(): void + { + /** @var Every $mock */ + $mock = $this->getMockForTrait(Every::class); + $mock->array = []; + + $this->assertTrue($mock->every(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Every::every() + */ + public function everyCallbackReceivesAlsoArrayKeys(): void + { + /** @var Every $mock */ + $mock = $this->getMockForTrait(Every::class); + $mock->array = ['foo' => 'fizz', 'bar' => 'buzz']; + + $this->assertTrue($mock->every(function ($v, $k) { + return 'foo' === $k || 'bar' === $k; + })); + } +}