2
0

Css2XpathTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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-2009 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-2009 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. public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
  79. {
  80. $test = Zend_Dom_Query_Css2Xpath::transform('#foo, #bar');
  81. $this->assertTrue(is_array($test));
  82. $this->assertEquals(2, count($test));
  83. }
  84. public function testTransformShouldRecognizeHashSymbolAsId()
  85. {
  86. $test = Zend_Dom_Query_Css2Xpath::transform('#foo');
  87. $this->assertEquals("//*[@id='foo']", $test);
  88. }
  89. public function testTransformShouldRecognizeDotSymbolAsClass()
  90. {
  91. $test = Zend_Dom_Query_Css2Xpath::transform('.foo');
  92. $this->assertEquals("//*[contains(@class, ' foo ')]", $test);
  93. }
  94. public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
  95. {
  96. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo .bar');
  97. $this->assertContains(' | ', $test);
  98. $expected = array(
  99. "//div[@id='foo']//*[contains(@class, ' bar ')]",
  100. "//div[@id='foo'][contains(@class, ' bar ')]",
  101. );
  102. foreach ($expected as $path) {
  103. $this->assertContains($path, $test);
  104. }
  105. }
  106. public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
  107. {
  108. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo>span');
  109. $this->assertEquals("//div[@id='foo']/span", $test);
  110. }
  111. public function testMultipleComplexCssSpecificationShouldTransformToExpectedXpath()
  112. {
  113. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo span.bar, #bar li.baz a');
  114. $this->assertTrue(is_array($test));
  115. $expected = array(
  116. "//div[@id='foo']//span[contains(@class, ' bar ')]",
  117. "//*[@id='bar']//li[contains(@class, ' baz ')]//a",
  118. );
  119. $this->assertEquals(count($expected), count($test));
  120. foreach ($test as $path) {
  121. $this->assertContains($path, $expected);
  122. }
  123. }
  124. public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
  125. {
  126. $test = Zend_Dom_Query_Css2Xpath::transform('div.foo .bar a .baz span');
  127. $this->assertContains(' | ', $test);
  128. $segments = array(
  129. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  130. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  131. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  132. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  133. );
  134. foreach ($segments as $xpath) {
  135. $this->assertContains($xpath, $test);
  136. }
  137. }
  138. public function testShouldAllowEqualitySelectionOfArbitraryAttributes()
  139. {
  140. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo="bar"]');
  141. $this->assertEquals("//div[@foo='bar']", $test);
  142. }
  143. public function testShouldCastAttributeNamesToLowerCase()
  144. {
  145. $test = Zend_Dom_Query_Css2Xpath::transform('div[dojoType="bar"]');
  146. $this->assertEquals("//div[@dojotype='bar']", $test);
  147. }
  148. public function testShouldAllowContentSubSelectionOfArbitraryAttributes()
  149. {
  150. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo~="bar"]');
  151. $this->assertEquals("//div[contains(@foo, ' bar ')]", $test);
  152. }
  153. public function testShouldAllowContentMatchingOfArbitraryAttributes()
  154. {
  155. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
  156. $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
  157. }
  158. /**
  159. * @group ZF-4010
  160. */
  161. public function testShouldAllowMatchingOfAttributeValues()
  162. {
  163. $test = Zend_Dom_Query_Css2Xpath::transform('tag#id @attribute');
  164. $this->assertEquals("//tag[@id='id']//@attribute", $test);
  165. }
  166. }
  167. // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.
  168. if (PHPUnit_MAIN_METHOD == "Zend_Dom_Query_Css2XpathTest::main") {
  169. Zend_Dom_Query_Css2XpathTest::main();
  170. }