Css2XpathTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dom
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once 'Zend/Dom/Query/Css2Xpath.php';
  23. /**
  24. * Test class for Css2Xpath.
  25. *
  26. * @category Zend
  27. * @package Zend_Dom
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Dom
  32. */
  33. class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
  34. {
  35. public function testTransformShouldBeCalledStatically()
  36. {
  37. Zend_Dom_Query_Css2Xpath::transform('');
  38. }
  39. public function testTransformShouldReturnStringByDefault()
  40. {
  41. $test = Zend_Dom_Query_Css2Xpath::transform('');
  42. $this->assertTrue(is_string($test));
  43. }
  44. /**
  45. * @group ZF-6281
  46. */
  47. public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
  48. {
  49. $test = Zend_Dom_Query_Css2Xpath::transform('#foo, #bar');
  50. $this->assertTrue(is_string($test));
  51. $this->assertContains('|', $test);
  52. $this->assertEquals(2, count(explode('|', $test)));
  53. }
  54. public function testTransformShouldRecognizeHashSymbolAsId()
  55. {
  56. $test = Zend_Dom_Query_Css2Xpath::transform('#foo');
  57. $this->assertEquals("//*[@id='foo']", $test);
  58. }
  59. public function testTransformShouldRecognizeDotSymbolAsClass()
  60. {
  61. $test = Zend_Dom_Query_Css2Xpath::transform('.foo');
  62. $this->assertEquals("//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $test);
  63. }
  64. public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
  65. {
  66. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo .bar');
  67. $this->assertContains('|', $test);
  68. $expected = array(
  69. "//div[@id='foo']//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  70. "//div[@id='foo'][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  71. );
  72. foreach ($expected as $path) {
  73. $this->assertContains($path, $test);
  74. }
  75. }
  76. public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
  77. {
  78. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo>span');
  79. $this->assertEquals("//div[@id='foo']/span", $test);
  80. }
  81. /**
  82. * @group ZF-6281
  83. */
  84. public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpath()
  85. {
  86. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo span.bar, #bar li.baz a');
  87. $this->assertTrue(is_string($test));
  88. $this->assertContains('|', $test);
  89. $actual = explode('|', $test);
  90. $expected = array(
  91. "//div[@id='foo']//span[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  92. "//*[@id='bar']//li[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//a",
  93. );
  94. $this->assertEquals(count($expected), count($actual));
  95. foreach ($actual as $path) {
  96. $this->assertContains($path, $expected);
  97. }
  98. }
  99. public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
  100. {
  101. $test = Zend_Dom_Query_Css2Xpath::transform('div.foo .bar a .baz span');
  102. $this->assertContains('|', $test);
  103. $segments = array(
  104. "//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a//*[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
  105. "//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
  106. "//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a//*[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
  107. "//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
  108. );
  109. foreach ($segments as $xpath) {
  110. $this->assertContains($xpath, $test);
  111. }
  112. }
  113. public function testShouldAllowEqualitySelectionOfArbitraryAttributes()
  114. {
  115. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo="bar"]');
  116. $this->assertEquals("//div[@foo='bar']", $test);
  117. }
  118. public function testShouldCastAttributeNamesToLowerCase()
  119. {
  120. $test = Zend_Dom_Query_Css2Xpath::transform('div[dojoType="bar"]');
  121. $this->assertEquals("//div[@dojotype='bar']", $test);
  122. }
  123. public function testShouldAllowContentSubSelectionOfArbitraryAttributes()
  124. {
  125. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo~="bar"]');
  126. $this->assertEquals("//div[contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]", $test);
  127. }
  128. public function testShouldAllowContentMatchingOfArbitraryAttributes()
  129. {
  130. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
  131. $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
  132. }
  133. /**
  134. * @group ZF-4010
  135. */
  136. public function testShouldAllowMatchingOfAttributeValues()
  137. {
  138. $test = Zend_Dom_Query_Css2Xpath::transform('tag#id @attribute');
  139. $this->assertEquals("//tag[@id='id']//@attribute", $test);
  140. }
  141. /**
  142. * @group ZF-8006
  143. */
  144. public function testShouldAllowWhitespaceInDescendentSelectorExpressions()
  145. {
  146. $test = Zend_Dom_Query_Css2Xpath::transform('child > leaf');
  147. $this->assertEquals("//child/leaf", $test);
  148. }
  149. /**
  150. * @group ZF-9764
  151. */
  152. public function testIdSelectorWithAttribute()
  153. {
  154. $test = Zend_Dom_Query_Css2Xpath::transform('#id[attribute="value"]');
  155. $this->assertEquals("//*[@id='id'][@attribute='value']", $test);
  156. }
  157. /**
  158. * @group ZF-9764
  159. */
  160. public function testIdSelectorWithLeadingAsterix()
  161. {
  162. $test = Zend_Dom_Query_Css2Xpath::transform('*#id');
  163. $this->assertEquals("//*[@id='id']", $test);
  164. }
  165. }