Css2XpathTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dom_Query_Css2XpathTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. /** Zend_Dom_Query_Css2Xpath */
  28. require_once 'Zend/Dom/Query/Css2Xpath.php';
  29. /**
  30. * Test class for Zend_Dom_Query_Css2Xpath.
  31. *
  32. * @category Zend
  33. * @package Zend_Dom
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Dom
  38. */
  39. class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Dom_Query_Css2XpathTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. }
  60. /**
  61. * Tears down the fixture, for example, close a network connection.
  62. * This method is called after a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function tearDown()
  67. {
  68. }
  69. public function testTransformShouldBeCalledStatically()
  70. {
  71. Zend_Dom_Query_Css2Xpath::transform('');
  72. }
  73. public function testTransformShouldReturnStringByDefault()
  74. {
  75. $test = Zend_Dom_Query_Css2Xpath::transform('');
  76. $this->assertTrue(is_string($test));
  77. }
  78. /**
  79. * @group ZF-6281
  80. */
  81. public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
  82. {
  83. $test = Zend_Dom_Query_Css2Xpath::transform('#foo, #bar');
  84. $this->assertTrue(is_string($test));
  85. $this->assertContains('|', $test);
  86. $this->assertEquals(2, count(explode('|', $test)));
  87. }
  88. public function testTransformShouldRecognizeHashSymbolAsId()
  89. {
  90. $test = Zend_Dom_Query_Css2Xpath::transform('#foo');
  91. $this->assertEquals("//*[@id='foo']", $test);
  92. }
  93. public function testTransformShouldRecognizeDotSymbolAsClass()
  94. {
  95. $test = Zend_Dom_Query_Css2Xpath::transform('.foo');
  96. $this->assertEquals("//*[contains(@class, ' foo ')]", $test);
  97. }
  98. public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
  99. {
  100. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo .bar');
  101. $this->assertContains('|', $test);
  102. $expected = array(
  103. "//div[@id='foo']//*[contains(@class, ' bar ')]",
  104. "//div[@id='foo'][contains(@class, ' bar ')]",
  105. );
  106. foreach ($expected as $path) {
  107. $this->assertContains($path, $test);
  108. }
  109. }
  110. public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
  111. {
  112. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo>span');
  113. $this->assertEquals("//div[@id='foo']/span", $test);
  114. }
  115. /**
  116. * @group ZF-6281
  117. */
  118. public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpath()
  119. {
  120. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo span.bar, #bar li.baz a');
  121. $this->assertTrue(is_string($test));
  122. $this->assertContains('|', $test);
  123. $actual = explode('|', $test);
  124. $expected = array(
  125. "//div[@id='foo']//span[contains(@class, ' bar ')]",
  126. "//*[@id='bar']//li[contains(@class, ' baz ')]//a",
  127. );
  128. $this->assertEquals(count($expected), count($actual));
  129. foreach ($actual as $path) {
  130. $this->assertContains($path, $expected);
  131. }
  132. }
  133. public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
  134. {
  135. $test = Zend_Dom_Query_Css2Xpath::transform('div.foo .bar a .baz span');
  136. $this->assertContains('|', $test);
  137. $segments = array(
  138. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  139. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  140. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  141. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  142. );
  143. foreach ($segments as $xpath) {
  144. $this->assertContains($xpath, $test);
  145. }
  146. }
  147. public function testShouldAllowEqualitySelectionOfArbitraryAttributes()
  148. {
  149. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo="bar"]');
  150. $this->assertEquals("//div[@foo='bar']", $test);
  151. }
  152. public function testShouldCastAttributeNamesToLowerCase()
  153. {
  154. $test = Zend_Dom_Query_Css2Xpath::transform('div[dojoType="bar"]');
  155. $this->assertEquals("//div[@dojotype='bar']", $test);
  156. }
  157. public function testShouldAllowContentSubSelectionOfArbitraryAttributes()
  158. {
  159. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo~="bar"]');
  160. $this->assertEquals("//div[contains(@foo, ' bar ')]", $test);
  161. }
  162. public function testShouldAllowContentMatchingOfArbitraryAttributes()
  163. {
  164. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
  165. $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
  166. }
  167. /**
  168. * @group ZF-4010
  169. */
  170. public function testShouldAllowMatchingOfAttributeValues()
  171. {
  172. $test = Zend_Dom_Query_Css2Xpath::transform('tag#id @attribute');
  173. $this->assertEquals("//tag[@id='id']//@attribute", $test);
  174. }
  175. /**
  176. * @group ZF-8006
  177. */
  178. public function testShouldAllowWhitespaceInDescendentSelectorExpressions()
  179. {
  180. $test = Zend_Dom_Query_Css2Xpath::transform('child > leaf');
  181. $this->assertEquals("//child/leaf", $test);
  182. }
  183. }
  184. // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.
  185. if (PHPUnit_MAIN_METHOD == "Zend_Dom_Query_Css2XpathTest::main") {
  186. Zend_Dom_Query_Css2XpathTest::main();
  187. }