From abe10471a7b2978c299e2ca917ccfdee59da0484 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Tue, 30 Sep 2014 15:08:14 +0200 Subject: [PATCH] Added XmlMatcher --- README.md | 42 +++++ composer.json | 3 +- .../PHPMatcher/Factory/SimpleFactory.php | 3 +- src/Coduo/PHPMatcher/Matcher/XmlMatcher.php | 66 ++++++++ .../PHPMatcher/Matcher/XmlMatcherTest.php | 152 ++++++++++++++++++ tests/Coduo/PHPMatcher/MatcherTest.php | 39 ++++- 6 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 src/Coduo/PHPMatcher/Matcher/XmlMatcher.php create mode 100644 tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php diff --git a/README.md b/README.md index 87e710cb..787f0ca6 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,48 @@ match( ``` +### Xml matching + + +```php + + + + + + IBM + Any Value + + + + +XML + , + << + + + + + @string@ + @string@ + + + + +XML +) + +``` + Example scenario for api in behat using mongo. --- ``` cucumber diff --git a/composer.json b/composer.json index b736730c..a11b43ac 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "php": ">=5.3.0", "coduo/php-to-string": "1.0.*", "symfony/property-access": "~2.3", - "symfony/expression-language": "~2.4" + "symfony/expression-language": "~2.4", + "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 46cc3151..68395fe7 100644 --- a/src/Coduo/PHPMatcher/Factory/SimpleFactory.php +++ b/src/Coduo/PHPMatcher/Factory/SimpleFactory.php @@ -26,7 +26,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..021beddb --- /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..0b9bf384 --- /dev/null +++ b/tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php @@ -0,0 +1,152 @@ +matcher = new XmlMatcher(new ChainMatcher(array( + $scalarMatchers, + new ArrayMatcher($scalarMatchers) + ))); + } + + /** + * @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 38d26322..9e2202b5 100644 --- a/tests/Coduo/PHPMatcher/MatcherTest.php +++ b/tests/Coduo/PHPMatcher/MatcherTest.php @@ -11,6 +11,7 @@ use Coduo\PHPMatcher\Matcher\TypeMatcher; use Coduo\PHPMatcher\Matcher\WildcardMatcher; use Coduo\PHPMatcher\Matcher; +use Coduo\PHPMatcher\Matcher\XmlMatcher; class MatcherTest extends \PHPUnit_Framework_TestCase { @@ -42,7 +43,8 @@ public function setUp() $this->matcher = new Matcher(new ChainMatcher(array( $scalarMatchers, $arrayMatcher, - new JsonMatcher($arrayMatcher) + new JsonMatcher($arrayMatcher), + new XmlMatcher($arrayMatcher) ))); } @@ -158,6 +160,41 @@ 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; + + } + public function test_matcher_with_captures() { $this->assertTrue($this->matcher->match(