PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit/tests/Util/XMLTest.php

https://gitlab.com/techniconline/kmc
PHP | 318 lines | 245 code | 58 blank | 15 comment | 1 complexity | a50e9dbee248ba930d11b260a0d7317d MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @since Class available since Release 3.3.0
  12. * @covers PHPUnit_Util_XML
  13. */
  14. class Util_XMLTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function testAssertValidKeysValidKeys()
  17. {
  18. $options = array('testA' => 1, 'testB' => 2, 'testC' => 3);
  19. $valid = array('testA', 'testB', 'testC');
  20. $expected = array('testA' => 1, 'testB' => 2, 'testC' => 3);
  21. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  22. $this->assertEquals($expected, $validated);
  23. }
  24. public function testAssertValidKeysValidKeysEmpty()
  25. {
  26. $options = array('testA' => 1, 'testB' => 2);
  27. $valid = array('testA', 'testB', 'testC');
  28. $expected = array('testA' => 1, 'testB' => 2, 'testC' => null);
  29. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  30. $this->assertEquals($expected, $validated);
  31. }
  32. public function testAssertValidKeysDefaultValuesA()
  33. {
  34. $options = array('testA' => 1, 'testB' => 2);
  35. $valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
  36. $expected = array('testA' => 1, 'testB' => 2, 'testC' => 25);
  37. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  38. $this->assertEquals($expected, $validated);
  39. }
  40. public function testAssertValidKeysDefaultValuesB()
  41. {
  42. $options = array();
  43. $valid = array('testA' => 23, 'testB' => 24, 'testC' => 25);
  44. $expected = array('testA' => 23, 'testB' => 24, 'testC' => 25);
  45. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  46. $this->assertEquals($expected, $validated);
  47. }
  48. public function testAssertValidKeysInvalidKey()
  49. {
  50. $options = array('testA' => 1, 'testB' => 2, 'testD' => 3);
  51. $valid = array('testA', 'testB', 'testC');
  52. try {
  53. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  54. $this->fail();
  55. } catch (PHPUnit_Framework_Exception $e) {
  56. $this->assertEquals('Unknown key(s): testD', $e->getMessage());
  57. }
  58. }
  59. public function testAssertValidKeysInvalidKeys()
  60. {
  61. $options = array('testA' => 1, 'testD' => 2, 'testE' => 3);
  62. $valid = array('testA', 'testB', 'testC');
  63. try {
  64. $validated = PHPUnit_Util_XML::assertValidKeys($options, $valid);
  65. $this->fail();
  66. } catch (PHPUnit_Framework_Exception $e) {
  67. $this->assertEquals('Unknown key(s): testD, testE', $e->getMessage());
  68. }
  69. }
  70. public function testConvertAssertSelect()
  71. {
  72. $selector = 'div#folder.open a[href="http://www.xerox.com"][title="xerox"].selected.big > span + h1';
  73. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  74. $tag = array('tag' => 'div',
  75. 'id' => 'folder',
  76. 'class' => 'open',
  77. 'descendant' => array('tag' => 'a',
  78. 'class' => 'selected big',
  79. 'attributes' => array('href' => 'http://www.xerox.com',
  80. 'title' => 'xerox'),
  81. 'child' => array('tag' => 'span',
  82. 'adjacent-sibling' => array('tag' => 'h1'))));
  83. $this->assertEquals($tag, $converted);
  84. }
  85. public function testConvertAssertSelectElt()
  86. {
  87. $selector = 'div';
  88. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  89. $tag = array('tag' => 'div');
  90. $this->assertEquals($tag, $converted);
  91. }
  92. public function testConvertAssertClass()
  93. {
  94. $selector = '.foo';
  95. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  96. $tag = array('class' => 'foo');
  97. $this->assertEquals($tag, $converted);
  98. }
  99. public function testConvertAssertId()
  100. {
  101. $selector = '#foo';
  102. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  103. $tag = array('id' => 'foo');
  104. $this->assertEquals($tag, $converted);
  105. }
  106. public function testConvertAssertAttribute()
  107. {
  108. $selector = '[foo="bar"]';
  109. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  110. $tag = array('attributes' => array('foo' => 'bar'));
  111. $this->assertEquals($tag, $converted);
  112. }
  113. public function testConvertAssertAttributeSpaces()
  114. {
  115. $selector = '[foo="bar baz"] div[value="foo bar"]';
  116. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  117. $tag = array('attributes' => array('foo' => 'bar baz'),
  118. 'descendant' => array('tag' => 'div',
  119. 'attributes' => array('value' => 'foo bar')));
  120. $this->assertEquals($tag, $converted);
  121. }
  122. public function testConvertAssertAttributeMultipleSpaces()
  123. {
  124. $selector = '[foo="bar baz"] div[value="foo bar baz"]';
  125. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  126. $tag = array('attributes' => array('foo' => 'bar baz'),
  127. 'descendant' => array('tag' => 'div',
  128. 'attributes' => array('value' => 'foo bar baz')));
  129. $this->assertEquals($tag, $converted);
  130. }
  131. public function testConvertAssertSelectEltClass()
  132. {
  133. $selector = 'div.foo';
  134. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  135. $tag = array('tag' => 'div', 'class' => 'foo');
  136. $this->assertEquals($tag, $converted);
  137. }
  138. public function testConvertAssertSelectEltId()
  139. {
  140. $selector = 'div#foo';
  141. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  142. $tag = array('tag' => 'div', 'id' => 'foo');
  143. $this->assertEquals($tag, $converted);
  144. }
  145. public function testConvertAssertSelectEltAttrEqual()
  146. {
  147. $selector = 'div[foo="bar"]';
  148. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  149. $tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar'));
  150. $this->assertEquals($tag, $converted);
  151. }
  152. public function testConvertAssertSelectEltMultiAttrEqual()
  153. {
  154. $selector = 'div[foo="bar"][baz="fob"]';
  155. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  156. $tag = array('tag' => 'div', 'attributes' => array('foo' => 'bar', 'baz' => 'fob'));
  157. $this->assertEquals($tag, $converted);
  158. }
  159. public function testConvertAssertSelectEltAttrHasOne()
  160. {
  161. $selector = 'div[foo~="bar"]';
  162. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  163. $tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*\bbar\b.*/'));
  164. $this->assertEquals($tag, $converted);
  165. }
  166. public function testConvertAssertSelectEltAttrContains()
  167. {
  168. $selector = 'div[foo*="bar"]';
  169. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  170. $tag = array('tag' => 'div', 'attributes' => array('foo' => 'regexp:/.*bar.*/'));
  171. $this->assertEquals($tag, $converted);
  172. }
  173. public function testConvertAssertSelectEltChild()
  174. {
  175. $selector = 'div > a';
  176. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  177. $tag = array('tag' => 'div', 'child' => array('tag' => 'a'));
  178. $this->assertEquals($tag, $converted);
  179. }
  180. public function testConvertAssertSelectEltAdjacentSibling()
  181. {
  182. $selector = 'div + a';
  183. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  184. $tag = array('tag' => 'div', 'adjacent-sibling' => array('tag' => 'a'));
  185. $this->assertEquals($tag, $converted);
  186. }
  187. public function testConvertAssertSelectEltDescendant()
  188. {
  189. $selector = 'div a';
  190. $converted = PHPUnit_Util_XML::convertSelectToTag($selector);
  191. $tag = array('tag' => 'div', 'descendant' => array('tag' => 'a'));
  192. $this->assertEquals($tag, $converted);
  193. }
  194. public function testConvertAssertSelectContent()
  195. {
  196. $selector = '#foo';
  197. $content = 'div contents';
  198. $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
  199. $tag = array('id' => 'foo', 'content' => 'div contents');
  200. $this->assertEquals($tag, $converted);
  201. }
  202. public function testConvertAssertSelectTrue()
  203. {
  204. $selector = '#foo';
  205. $content = true;
  206. $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
  207. $tag = array('id' => 'foo');
  208. $this->assertEquals($tag, $converted);
  209. }
  210. public function testConvertAssertSelectFalse()
  211. {
  212. $selector = '#foo';
  213. $content = false;
  214. $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
  215. $tag = array('id' => 'foo');
  216. $this->assertEquals($tag, $converted);
  217. }
  218. public function testConvertAssertNumber()
  219. {
  220. $selector = '.foo';
  221. $content = 3;
  222. $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
  223. $tag = array('class' => 'foo');
  224. $this->assertEquals($tag, $converted);
  225. }
  226. public function testConvertAssertRange()
  227. {
  228. $selector = '#foo';
  229. $content = array('greater_than' => 5, 'less_than' => 10);
  230. $converted = PHPUnit_Util_XML::convertSelectToTag($selector, $content);
  231. $tag = array('id' => 'foo');
  232. $this->assertEquals($tag, $converted);
  233. }
  234. /**
  235. * @dataProvider charProvider
  236. */
  237. public function testPrepareString($char)
  238. {
  239. $e = null;
  240. $escapedString = PHPUnit_Util_XML::prepareString($char);
  241. $xml = "<?xml version='1.0' encoding='UTF-8' ?><tag>$escapedString</tag>";
  242. $dom = new DomDocument('1.0', 'UTF-8');
  243. try {
  244. $dom->loadXML($xml);
  245. } catch (Exception $e) {
  246. }
  247. $this->assertNull($e, sprintf(
  248. 'PHPUnit_Util_XML::prepareString("\x%02x") should not crash DomDocument',
  249. ord($char)
  250. ));
  251. }
  252. public function charProvider()
  253. {
  254. $data = array();
  255. for ($i = 0; $i < 256; $i++) {
  256. $data[] = array(chr($i));
  257. }
  258. return $data;
  259. }
  260. }