|
| 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 | +} |
0 commit comments