-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
})); | ||
} | ||
} |