Skip to content

Added XmlMatcher #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
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 @@ -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)
));
}

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

namespace Coduo\PHPMatcher\Tests\Matcher;

use Coduo\PHPMatcher\Lexer;
use Coduo\PHPMatcher\Matcher;
use Coduo\PHPMatcher\Parser;

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

public function setUp()
{
$parser = new Parser(new Lexer());
$scalarMatchers = new Matcher\ChainMatcher(array(
new Matcher\CallbackMatcher(),
new Matcher\ExpressionMatcher(),
new Matcher\NullMatcher(),
new Matcher\StringMatcher($parser),
new Matcher\IntegerMatcher($parser),
new Matcher\BooleanMatcher(),
new Matcher\DoubleMatcher($parser),
new Matcher\NumberMatcher(),
new Matcher\ScalarMatcher(),
new Matcher\WildcardMatcher(),
));

$this->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('<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>'
),
);
}
}
41 changes: 40 additions & 1 deletion tests/Coduo/PHPMatcher/MatcherTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Coduo\PHPMatcher\Tests;

use Coduo\PHPMatcher\Lexer;
Expand Down Expand Up @@ -39,7 +40,8 @@ public function setUp()
$this->matcher = new Matcher(new Matcher\ChainMatcher(array(
$scalarMatchers,
$arrayMatcher,
new Matcher\JsonMatcher($arrayMatcher)
new Matcher\JsonMatcher($arrayMatcher),
new Matcher\XmlMatcher($arrayMatcher)
)));
}

Expand Down Expand Up @@ -155,6 +157,43 @@ 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;

$this->assertTrue($this->matcher->match($xml, $xmlPattern));
$this->assertTrue(match($xml, $xmlPattern));
}

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