diff --git a/src/FilterChain.php b/src/FilterChain.php index 15189e1a..25576d1a 100644 --- a/src/FilterChain.php +++ b/src/FilterChain.php @@ -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; diff --git a/test/FilterChainTest.php b/test/FilterChainTest.php index b1b3f20b..bcdfaf2c 100644 --- a/test/FilterChainTest.php +++ b/test/FilterChainTest.php @@ -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)); + } }