forked from cocur/chain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move SimpleMerge into Trait (Issue cocur#54)
- Loading branch information
1 parent
418890e
commit 94935c5
Showing
4 changed files
with
74 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Cocur\Chain\Link; | ||
|
||
use Cocur\Chain\Chain; | ||
|
||
/** | ||
* Simple, therefore quicker merge. | ||
* @author Andrey Tsarev | ||
* @copyright 2015-2018 Florian Eckerstorfer | ||
*/ | ||
trait SimpleMerge | ||
{ | ||
/** | ||
* Merge arrays. | ||
* Merge the elements of one array with the elements of the array in the Chain. | ||
* @param Chain|array $array Array to merge with | ||
* @return self | ||
*/ | ||
public function simpleMerge( $array ) : self | ||
{ | ||
$array = $array instanceof Chain ? $array->array : $array; | ||
foreach ($array as $item) { | ||
$this->array[] = $item; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace Cocur\Chain\Link; | ||
|
||
use Cocur\Chain\Chain; | ||
|
||
/** | ||
* SimpleMergeTest. | ||
* | ||
* @author Andrey Tsarev | ||
* @copyright 2015-2018 Florian Eckerstorfer | ||
*/ | ||
class SimpleMergeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @test | ||
* @covers \Cocur\Chain\Link\SimpleMerge::simpleMerge() | ||
*/ | ||
public function mergeMergesArray(): void | ||
{ | ||
/** @var SimpleMerge $mock */ | ||
$mock = $this->getMockForTrait(SimpleMerge::class); | ||
$mock->array = [0, 1, 2]; | ||
$mock->simpleMerge([3, 4]); | ||
|
||
$this->assertEquals([0, 1, 2, 3, 4], $mock->array); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers \Cocur\Chain\Link\SimpleMerge::simpleMerge() | ||
*/ | ||
public function mergeMergesChain(): void | ||
{ | ||
/** @var SimpleMerge $mock */ | ||
$mock = $this->getMockForTrait(SimpleMerge::class); | ||
$mock->array = [0, 1, 2]; | ||
$mock->simpleMerge(Chain::create([3, 4])); | ||
|
||
$this->assertEquals([0, 1, 2, 3, 4], $mock->array); | ||
} | ||
|
||
} |