From 6f372a66534fcedf7e2c05db946dde888fdc9949 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Wed, 1 Oct 2014 08:16:41 +0200 Subject: [PATCH] Added XmlMatcher --- composer.json | 3 +- .../PHPMatcher/Factory/SimpleFactory.php | 3 +- src/Coduo/PHPMatcher/Matcher/XmlMatcher.php | 66 ++++++++ .../PHPMatcher/Matcher/XmlMatcherTest.php | 157 ++++++++++++++++++ tests/Coduo/PHPMatcher/MatcherTest.php | 41 ++++- 5 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 src/Coduo/PHPMatcher/Matcher/XmlMatcher.php create mode 100644 tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php diff --git a/composer.json b/composer.json index dd375f27..8c5159bb 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "coduo/php-to-string": "~1.0", "symfony/property-access": "~2.3", "symfony/expression-language": "~2.4", - "doctrine/lexer": "1.0.*" + "doctrine/lexer": "1.0.*", + "openlss/lib-array2xml": "0.0.9" }, "require-dev": { "phpunit/phpunit": "3.7.*" diff --git a/src/Coduo/PHPMatcher/Factory/SimpleFactory.php b/src/Coduo/PHPMatcher/Factory/SimpleFactory.php index 83186f85..96557150 100644 --- a/src/Coduo/PHPMatcher/Factory/SimpleFactory.php +++ b/src/Coduo/PHPMatcher/Factory/SimpleFactory.php @@ -28,7 +28,8 @@ protected function buildMatchers() return new Matcher\ChainMatcher(array( $scalarMatchers, $arrayMatcher, - new Matcher\JsonMatcher($arrayMatcher) + new Matcher\JsonMatcher($arrayMatcher), + new Matcher\XmlMatcher($arrayMatcher) )); } diff --git a/src/Coduo/PHPMatcher/Matcher/XmlMatcher.php b/src/Coduo/PHPMatcher/Matcher/XmlMatcher.php new file mode 100644 index 00000000..6b00120a --- /dev/null +++ b/src/Coduo/PHPMatcher/Matcher/XmlMatcher.php @@ -0,0 +1,66 @@ +matcher = $matcher; + } + + /** + * {@inheritDoc} + */ + public function match($value, $pattern) + { + if (!is_string($value) || !$this->isValidXml($value) || !$this->isValidXml($pattern)) { + return false; + } + + $arrayValue = XML2Array::createArray($value); + $arrayPattern = XML2Array::createArray($pattern); + + $match = $this->matcher->match($arrayValue, $arrayPattern); + if (!$match) { + $this->error = $this->matcher->getError(); + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function canMatch($pattern) + { + if (!is_string($pattern)) { + return false; + } + + return $this->isValidXml($pattern); + } + + private function isValidXml($string) + { + $xml = @simplexml_load_string($string); + + if (!$xml instanceof \SimpleXMLElement) { + + return false; + } + + return true; + } +} diff --git a/tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php b/tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php new file mode 100644 index 00000000..1d53257f --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php @@ -0,0 +1,157 @@ +matcher = new Matcher\XmlMatcher( + new Matcher\ChainMatcher(array( + $scalarMatchers, + new Matcher\ArrayMatcher($scalarMatchers, $parser) + ) + )); + } + + /** + * @dataProvider positivePatterns + */ + public function test_positive_can_match($pattern) + { + $this->assertTrue($this->matcher->canMatch($pattern)); + } + + /** + * @dataProvider negativePatterns + */ + public function test_negative_can_match($pattern) + { + $this->assertFalse($this->matcher->canMatch($pattern)); + } + + /** + * @dataProvider positiveMatches + */ + public function test_positive_matches($value, $pattern) + { + $this->assertTrue($this->matcher->match($value, $pattern), $this->matcher->getError()); + } + + /** + * @dataProvider negativeMatches + */ + public function test_negative_matches($value, $pattern) + { + $this->assertFalse($this->matcher->match($value, $pattern), $this->matcher->getError()); + + } + + public static function positivePatterns() + { + return array( + array(''), + array('@string@'), + ); + } + + public static function negativePatterns() + { + return array( + array('NorbertMichał', + '@string@@string@' + ), + array( + 'Norbert', + '@string@' + ), + array( + 'Norbert25', + 'Norbert@string@' + ), + array( + '', + '' + ), + array( + << + + + + + IBM + Any Value + + + + +XML + , + << + + + + + @string@ + @string@ + + + + +XML + ) + ); + } + + public static function negativeMatches() + { + return array( + array( + 'NorbertMichał', + '{"users":["Michał","@string@"]}' + ), + array( + 'NorbertMichał', + '@integer@@integer@' + ), + ); + } +} diff --git a/tests/Coduo/PHPMatcher/MatcherTest.php b/tests/Coduo/PHPMatcher/MatcherTest.php index 636f6327..2d57dedd 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -1,4 +1,5 @@ matcher = new Matcher(new Matcher\ChainMatcher(array( $scalarMatchers, $arrayMatcher, - new Matcher\JsonMatcher($arrayMatcher) + new Matcher\JsonMatcher($arrayMatcher), + new Matcher\XmlMatcher($arrayMatcher) ))); } @@ -155,6 +157,43 @@ public function test_matcher_with_json() $this->assertTrue(match($json, $jsonPattern)); } + public function test_matcher_with_xml() + { + $xml = << + + + + + IBM + Any Value + + + + +XML; + $xmlPattern = << + + + + + @string@ + @string@ + + + + +XML; + + $this->assertTrue($this->matcher->match($xml, $xmlPattern)); + $this->assertTrue(match($xml, $xmlPattern)); + } + public function test_matcher_with_captures() { $this->assertTrue($this->matcher->match(