2
0

Css2XpathTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-2010 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 dirname(__FILE__) . '/../../../TestHelper.php';
  23. require_once 'Zend/Dom/Query/Css2Xpath.php';
  24. /**
  25. * Test class for Css2Xpath.
  26. *
  27. * @category Zend
  28. * @package Zend_Dom
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Dom
  33. */
  34. class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testTransformShouldBeCalledStatically()
  37. {
  38. Zend_Dom_Query_Css2Xpath::transform('');
  39. }
  40. public function testTransformShouldReturnStringByDefault()
  41. {
  42. $test = Zend_Dom_Query_Css2Xpath::transform('');
  43. $this->assertTrue(is_string($test));
  44. }
  45. /**
  46. * @group ZF-6281
  47. */
  48. public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
  49. {
  50. $test = Zend_Dom_Query_Css2Xpath::transform('#foo, #bar');
  51. $this->assertTrue(is_string($test));
  52. $this->assertContains('|', $test);
  53. $this->assertEquals(2, count(explode('|', $test)));
  54. }
  55. public function testTransformShouldRecognizeHashSymbolAsId()
  56. {
  57. $test = Zend_Dom_Query_Css2Xpath::transform('#foo');
  58. $this->assertEquals("//*[@id='foo']", $test);
  59. }
  60. public function testTransformShouldRecognizeDotSymbolAsClass()
  61. {
  62. $test = Zend_Dom_Query_Css2Xpath::transform('.foo');
  63. $this->assertEquals("//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $test);
  64. }
  65. public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
  66. {
  67. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo .bar');
  68. $this->assertContains('|', $test);
  69. $expected = array(
  70. "//div[@id='foo']//*[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  71. "//div[@id='foo'][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  72. );
  73. foreach ($expected as $path) {
  74. $this->assertContains($path, $test);
  75. }
  76. }
  77. public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
  78. {
  79. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo>span');
  80. $this->assertEquals("//div[@id='foo']/span", $test);
  81. }
  82. /**
  83. * @group ZF-6281
  84. */
  85. public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpath()
  86. {
  87. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo span.bar, #bar li.baz a');
  88. $this->assertTrue(is_string($test));
  89. $this->assertContains('|', $test);
  90. $actual = explode('|', $test);
  91. $expected = array(
  92. "//div[@id='foo']//span[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
  93. "//*[@id='bar']//li[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//a",
  94. );
  95. $this->assertEquals(count($expected), count($actual));
  96. foreach ($actual as $path) {
  97. $this->assertContains($path, $expected);
  98. }
  99. }
  100. public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
  101. {
  102. $test = Zend_Dom_Query_Css2Xpath::transform('div.foo .bar a .baz span');
  103. $this->assertContains('|', $test);
  104. $segments = array(
  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. "//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//a[contains(concat(' ', normalize-space(@class), ' '), ' baz ')]//span",
  109. );
  110. foreach ($segments as $xpath) {
  111. $this->assertContains($xpath, $test);
  112. }
  113. }
  114. public function testShouldAllowEqualitySelectionOfArbitraryAttributes()
  115. {
  116. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo="bar"]');
  117. $this->assertEquals("//div[@foo='bar']", $test);
  118. }
  119. public function testShouldCastAttributeNamesToLowerCase()
  120. {
  121. $test = Zend_Dom_Query_Css2Xpath::transform('div[dojoType="bar"]');
  122. $this->assertEquals("//div[@dojotype='bar']", $test);
  123. }
  124. public function testShouldAllowContentSubSelectionOfArbitraryAttributes()
  125. {
  126. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo~="bar"]');
  127. $this->assertEquals("//div[contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]", $test);
  128. }
  129. public function testShouldAllowContentMatchingOfArbitraryAttributes()
  130. {
  131. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
  132. $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
  133. }
  134. /**
  135. * @group ZF-4010
  136. */
  137. public function testShouldAllowMatchingOfAttributeValues()
  138. {
  139. $test = Zend_Dom_Query_Css2Xpath::transform('tag#id @attribute');
  140. $this->assertEquals("//tag[@id='id']//@attribute", $test);
  141. }
  142. /**
  143. * @group ZF-8006
  144. */
  145. public function testShouldAllowWhitespaceInDescendentSelectorExpressions()
  146. {
  147. $test = Zend_Dom_Query_Css2Xpath::transform('child > leaf');
  148. $this->assertEquals("//child/leaf", $test);
  149. }
  150. /**
  151. * @group ZF-9764
  152. */
  153. public function testIdSelectorWithAttribute()
  154. {
  155. $test = Zend_Dom_Query_Css2Xpath::transform('#id[attribute="value"]');
  156. $this->assertEquals("//*[@id='id'][@attribute='value']", $test);
  157. }
  158. /**
  159. * @group ZF-9764
  160. */
  161. public function testIdSelectorWithLeadingAsterix()
  162. {
  163. $test = Zend_Dom_Query_Css2Xpath::transform('*#id');
  164. $this->assertEquals("//*[@id='id']", $test);
  165. }
  166. }