Skip to content

Commit 092b13e

Browse files
committed
Merge pull request #29 from norzechowicz/xml-matcher
Added XmlMatcher
2 parents 64642ab + abe1047 commit 092b13e

File tree

6 files changed

+302
-3
lines changed

6 files changed

+302
-3
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,48 @@ match(
203203

204204
```
205205

206+
### Xml matching
207+
208+
209+
```php
210+
<?php
211+
212+
match(
213+
<<<XML
214+
<?xml version="1.0"?>
215+
<soap:Envelope
216+
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
217+
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
218+
219+
<soap:Body xmlns:m="http://www.example.org/stock">
220+
<m:GetStockPrice>
221+
<m:StockName>IBM</m:StockName>
222+
<m:StockValue>Any Value</m:StockValue>
223+
</m:GetStockPrice>
224+
</soap:Body>
225+
226+
</soap:Envelope>
227+
XML
228+
,
229+
<<<XML
230+
<?xml version="1.0"?>
231+
<soap:Envelope
232+
xmlns:soap="@string@"
233+
soap:encodingStyle="@string@">
234+
235+
<soap:Body xmlns:m="@string@">
236+
<m:GetStockPrice>
237+
<m:StockName>@string@</m:StockName>
238+
<m:StockValue>@string@</m:StockValue>
239+
</m:GetStockPrice>
240+
</soap:Body>
241+
242+
</soap:Envelope>
243+
XML
244+
)
245+
246+
```
247+
206248
Example scenario for api in behat using mongo.
207249
---
208250
``` cucumber

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": ">=5.3.0",
1818
"coduo/php-to-string": "1.0.*",
1919
"symfony/property-access": "~2.3",
20-
"symfony/expression-language": "~2.4"
20+
"symfony/expression-language": "~2.4",
21+
"openlss/lib-array2xml": "0.0.9"
2122
},
2223
"require-dev": {
2324
"phpunit/phpunit": "3.7.*"

src/Coduo/PHPMatcher/Factory/SimpleFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ protected function buildMatchers()
2626
return new Matcher\ChainMatcher(array(
2727
$scalarMatchers,
2828
$arrayMatcher,
29-
new Matcher\JsonMatcher($arrayMatcher)
29+
new Matcher\JsonMatcher($arrayMatcher),
30+
new Matcher\XmlMatcher($arrayMatcher)
3031
));
3132
}
3233

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use LSS\XML2Array;
6+
7+
class XmlMatcher extends Matcher
8+
{
9+
/**
10+
* @var
11+
*/
12+
private $matcher;
13+
14+
/**
15+
* @param PropertyMatcher $matcher
16+
*/
17+
public function __construct(PropertyMatcher $matcher)
18+
{
19+
$this->matcher = $matcher;
20+
}
21+
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function match($value, $pattern)
26+
{
27+
if (!is_string($value) || !$this->isValidXml($value) || !$this->isValidXml($pattern)) {
28+
return false;
29+
}
30+
31+
$arrayValue = XML2Array::createArray($value);
32+
$arrayPattern = XML2Array::createArray($pattern);
33+
34+
$match = $this->matcher->match($arrayValue, $arrayPattern);
35+
if (!$match) {
36+
$this->error = $this->matcher->getError();
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function canMatch($pattern)
47+
{
48+
if (!is_string($pattern)) {
49+
return false;
50+
}
51+
52+
return $this->isValidXml($pattern);
53+
}
54+
55+
private function isValidXml($string)
56+
{
57+
$xml = @simplexml_load_string($string);
58+
59+
if (!$xml instanceof \SimpleXMLElement) {
60+
61+
return false;
62+
}
63+
64+
return true;
65+
}
66+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher;
4+
5+
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
6+
use Coduo\PHPMatcher\Matcher\ChainMatcher;
7+
use Coduo\PHPMatcher\Matcher\JsonMatcher;
8+
use Coduo\PHPMatcher\Matcher\NullMatcher;
9+
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
10+
use Coduo\PHPMatcher\Matcher\TypeMatcher;
11+
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
12+
use Coduo\PHPMatcher\Matcher\XmlMatcher;
13+
14+
class XmlMatcherTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var JsonMatcher
18+
*/
19+
private $matcher;
20+
21+
public function setUp()
22+
{
23+
$scalarMatchers = new ChainMatcher(array(
24+
new TypeMatcher(),
25+
new ScalarMatcher(),
26+
new NullMatcher(),
27+
new WildcardMatcher()
28+
));
29+
$this->matcher = new XmlMatcher(new ChainMatcher(array(
30+
$scalarMatchers,
31+
new ArrayMatcher($scalarMatchers)
32+
)));
33+
}
34+
35+
/**
36+
* @dataProvider positivePatterns
37+
*/
38+
public function test_positive_can_match($pattern)
39+
{
40+
$this->assertTrue($this->matcher->canMatch($pattern));
41+
}
42+
43+
/**
44+
* @dataProvider negativePatterns
45+
*/
46+
public function test_negative_can_match($pattern)
47+
{
48+
$this->assertFalse($this->matcher->canMatch($pattern));
49+
}
50+
51+
/**
52+
* @dataProvider positiveMatches
53+
*/
54+
public function test_positive_matches($value, $pattern)
55+
{
56+
$this->assertTrue($this->matcher->match($value, $pattern), $this->matcher->getError());
57+
}
58+
59+
/**
60+
* @dataProvider negativeMatches
61+
*/
62+
public function test_negative_matches($value, $pattern)
63+
{
64+
$this->assertFalse($this->matcher->match($value, $pattern), $this->matcher->getError());
65+
66+
}
67+
68+
public static function positivePatterns()
69+
{
70+
return array(
71+
array('<xml></xml>'),
72+
array('<users><user>@string@</user></users>'),
73+
);
74+
}
75+
76+
public static function negativePatterns()
77+
{
78+
return array(
79+
array('<xml '),
80+
array('asdkasdasdqwrq'),
81+
);
82+
}
83+
84+
public static function positiveMatches()
85+
{
86+
return array(
87+
array(
88+
'<users><user>Norbert</user><user>Michał</user></users>',
89+
'<users><user>@string@</user><user>@string@</user></users>'
90+
),
91+
array(
92+
'<users><user id="1">Norbert</user></users>',
93+
'<users><user id="@string@">@string@</user></users>'
94+
),
95+
array(
96+
'<users><user><name>Norbert</name><age>25</age></user></users>',
97+
'<users><user><name>Norbert</name><age>@string@</age></user></users>'
98+
),
99+
array(
100+
'<string><![CDATA[Any kid of text here]]></string>',
101+
'<string><![CDATA[@string@]]></string>'
102+
),
103+
array(
104+
<<<XML
105+
<?xml version="1.0"?>
106+
<soap:Envelope
107+
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
108+
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
109+
110+
<soap:Body xmlns:m="http://www.example.org/stock">
111+
<m:GetStockPrice>
112+
<m:StockName>IBM</m:StockName>
113+
<m:StockValue>Any Value</m:StockValue>
114+
</m:GetStockPrice>
115+
</soap:Body>
116+
117+
</soap:Envelope>
118+
XML
119+
,
120+
<<<XML
121+
<?xml version="1.0"?>
122+
<soap:Envelope
123+
xmlns:soap="@string@"
124+
soap:encodingStyle="@string@">
125+
126+
<soap:Body xmlns:m="@string@">
127+
<m:GetStockPrice>
128+
<m:StockName>@string@</m:StockName>
129+
<m:StockValue>@string@</m:StockValue>
130+
</m:GetStockPrice>
131+
</soap:Body>
132+
133+
</soap:Envelope>
134+
XML
135+
)
136+
);
137+
}
138+
139+
public static function negativeMatches()
140+
{
141+
return array(
142+
array(
143+
'<users><user>Norbert</user><user>Michał</user></users>',
144+
'{"users":["Michał","@string@"]}'
145+
),
146+
array(
147+
'<users><user>Norbert</user><user>Michał</user></users>',
148+
'<users><user>@integer@</user><user>@integer@</user></users>'
149+
),
150+
);
151+
}
152+
}

tests/Coduo/PHPMatcher/MatcherTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Coduo\PHPMatcher\Matcher\TypeMatcher;
1212
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
1313
use Coduo\PHPMatcher\Matcher;
14+
use Coduo\PHPMatcher\Matcher\XmlMatcher;
1415

1516
class MatcherTest extends \PHPUnit_Framework_TestCase
1617
{
@@ -42,7 +43,8 @@ public function setUp()
4243
$this->matcher = new Matcher(new ChainMatcher(array(
4344
$scalarMatchers,
4445
$arrayMatcher,
45-
new JsonMatcher($arrayMatcher)
46+
new JsonMatcher($arrayMatcher),
47+
new XmlMatcher($arrayMatcher)
4648
)));
4749
}
4850

@@ -158,6 +160,41 @@ public function test_matcher_with_json()
158160
$this->assertTrue(match($json, $jsonPattern));
159161
}
160162

163+
public function test_matcher_with_xml()
164+
{
165+
$xml = <<<XML
166+
<?xml version="1.0"?>
167+
<soap:Envelope
168+
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
169+
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
170+
171+
<soap:Body xmlns:m="http://www.example.org/stock">
172+
<m:GetStockPrice>
173+
<m:StockName>IBM</m:StockName>
174+
<m:StockValue>Any Value</m:StockValue>
175+
</m:GetStockPrice>
176+
</soap:Body>
177+
178+
</soap:Envelope>
179+
XML;
180+
$xmlPattern = <<<XML
181+
<?xml version="1.0"?>
182+
<soap:Envelope
183+
xmlns:soap="@string@"
184+
soap:encodingStyle="@string@">
185+
186+
<soap:Body xmlns:m="@string@">
187+
<m:GetStockPrice>
188+
<m:StockName>@string@</m:StockName>
189+
<m:StockValue>@string@</m:StockValue>
190+
</m:GetStockPrice>
191+
</soap:Body>
192+
193+
</soap:Envelope>
194+
XML;
195+
196+
}
197+
161198
public function test_matcher_with_captures()
162199
{
163200
$this->assertTrue($this->matcher->match(

0 commit comments

Comments
 (0)