Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/FilterChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public function attachByName($name, $options = array(), $priority = self::DEFAUL
*/
public function merge(FilterChain $filterChain)
{
foreach ($filterChain->filters as $filter) {
$this->attach($filter);
foreach ($filterChain->filters->toArray(PriorityQueue::EXTR_BOTH) as $item) {
$this->attach($item['data'], $item['priority']);
}

return $this;
Expand Down
32 changes: 32 additions & 0 deletions test/FilterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,38 @@ public function testCanSerializeFilterChain()
$valueExpected = 'abc';
$this->assertEquals($valueExpected, $unserialized->filter($value));
}

public function testMergingTwoFilterChainsKeepFiltersPriority()
{
$value = 'AbC';
$valueExpected = 'abc';

$chain = new FilterChain();
$chain->attach(new StripUpperCase())
->attach(new LowerCase(), 1001);
$this->assertEquals($valueExpected, $chain->filter($value));

$chain = new FilterChain();
$chain->attach(new LowerCase(), 1001)
->attach(new StripUpperCase());
$this->assertEquals($valueExpected, $chain->filter($value));

$chain = new FilterChain();
$chain->attach(new LowerCase(), 1001);
$chainToMerge = new FilterChain();
$chainToMerge->attach(new StripUpperCase());
$chain->merge($chainToMerge);
$this->assertEquals(2, $chain->count());
$this->assertEquals($valueExpected, $chain->filter($value));

$chain = new FilterChain();
$chain->attach(new StripUpperCase());
$chainToMerge = new FilterChain();
$chainToMerge->attach(new LowerCase(), 1001);
$chain->merge($chainToMerge);
$this->assertEquals(2, $chain->count());
$this->assertEquals($valueExpected, $chain->filter($value));
}
}


Expand Down

0 comments on commit 61d3103

Please sign in to comment.