Skip to content

Commit

Permalink
Merge pull request #29 from norzechowicz/xml-matcher
Browse files Browse the repository at this point in the history
Added XmlMatcher
  • Loading branch information
defrag committed Sep 30, 2014
2 parents 64642ab + abe1047 commit 092b13e
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 3 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,48 @@ match(

```

### Xml matching


```php
<?php

match(
<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
<m:StockValue>Any Value</m:StockValue>
</m:GetStockPrice>
</soap:Body>

</soap:Envelope>
XML
,
<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="@string@"
soap:encodingStyle="@string@">

<soap:Body xmlns:m="@string@">
<m:GetStockPrice>
<m:StockName>@string@</m:StockName>
<m:StockValue>@string@</m:StockValue>
</m:GetStockPrice>
</soap:Body>

</soap:Envelope>
XML
)

```

Example scenario for api in behat using mongo.
---
``` cucumber
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
3 changes: 2 additions & 1 deletion src/Coduo/PHPMatcher/Factory/SimpleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

Expand Down
66 changes: 66 additions & 0 deletions src/Coduo/PHPMatcher/Matcher/XmlMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Coduo\PHPMatcher\Matcher;

use LSS\XML2Array;

class XmlMatcher extends Matcher
{
/**
* @var
*/
private $matcher;

/**
* @param PropertyMatcher $matcher
*/
public function __construct(PropertyMatcher $matcher)
{
$this->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;
}
}
152 changes: 152 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/XmlMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Matcher\ArrayMatcher;
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\JsonMatcher;
use Coduo\PHPMatcher\Matcher\NullMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
use Coduo\PHPMatcher\Matcher\XmlMatcher;

class XmlMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JsonMatcher
*/
private $matcher;

public function setUp()
{
$scalarMatchers = new ChainMatcher(array(
new TypeMatcher(),
new ScalarMatcher(),
new NullMatcher(),
new WildcardMatcher()
));
$this->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('<xml></xml>'),
array('<users><user>@string@</user></users>'),
);
}

public static function negativePatterns()
{
return array(
array('<xml '),
array('asdkasdasdqwrq'),
);
}

public static function positiveMatches()
{
return array(
array(
'<users><user>Norbert</user><user>Michał</user></users>',
'<users><user>@string@</user><user>@string@</user></users>'
),
array(
'<users><user id="1">Norbert</user></users>',
'<users><user id="@string@">@string@</user></users>'
),
array(
'<users><user><name>Norbert</name><age>25</age></user></users>',
'<users><user><name>Norbert</name><age>@string@</age></user></users>'
),
array(
'<string><![CDATA[Any kid of text here]]></string>',
'<string><![CDATA[@string@]]></string>'
),
array(
<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
<m:StockValue>Any Value</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML
,
<<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="@string@"
soap:encodingStyle="@string@">
<soap:Body xmlns:m="@string@">
<m:GetStockPrice>
<m:StockName>@string@</m:StockName>
<m:StockValue>@string@</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML
)
);
}

public static function negativeMatches()
{
return array(
array(
'<users><user>Norbert</user><user>Michał</user></users>',
'{"users":["Michał","@string@"]}'
),
array(
'<users><user>Norbert</user><user>Michał</user></users>',
'<users><user>@integer@</user><user>@integer@</user></users>'
),
);
}
}
39 changes: 38 additions & 1 deletion tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
)));
}

Expand Down Expand Up @@ -158,6 +160,41 @@ public function test_matcher_with_json()
$this->assertTrue(match($json, $jsonPattern));
}

public function test_matcher_with_xml()
{
$xml = <<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
<m:StockValue>Any Value</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML;
$xmlPattern = <<<XML
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="@string@"
soap:encodingStyle="@string@">
<soap:Body xmlns:m="@string@">
<m:GetStockPrice>
<m:StockName>@string@</m:StockName>
<m:StockValue>@string@</m:StockValue>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
XML;

}

public function test_matcher_with_captures()
{
$this->assertTrue($this->matcher->match(
Expand Down

0 comments on commit 092b13e

Please sign in to comment.