Skip to content

Commit

Permalink
feat(every): add supports
Browse files Browse the repository at this point in the history
  • Loading branch information
nreynis committed May 8, 2020
1 parent 17a0ce5 commit 5881dd6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ $chain->reduce(function ($current, $value) {

- `->count()`
- `->countValues()`
- `->every(callable)`
- `->first()`
- `->join([$glue])`
- `->last()`
Expand Down
2 changes: 2 additions & 0 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,6 +56,7 @@ class Chain extends AbstractChain implements Countable
use Count;
use CountValues;
use Diff;
use Every;
use Filter;
use Find;
use First;
Expand Down
26 changes: 26 additions & 0 deletions src/Link/Every.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Cocur\Chain\Link;

/**
* Every.
*
* @author Nicolas Reynis
*/
trait Every
{
/**
* @param callable $callback
*
* @return bool
*/
public function every(callable $callback): bool
{
$pass = true;
foreach ($this->array as $index => $element) {
$pass = $pass && $callback($element, $index);
}

return $pass;
}
}
1 change: 1 addition & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
87 changes: 87 additions & 0 deletions tests/Link/EveryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Cocur\Chain\Link;

/**
* EveryTest.
*
* @author Nicolas Reynis
* @group unit
*/
class EveryTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @covers \Cocur\Chain\Link\Every::every()
*/
public function everyReturnTrueWhenConditionPass(): void
{
/** @var Every $mock */
$mock = $this->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;
}));
}
}

0 comments on commit 5881dd6

Please sign in to comment.