Css2XpathTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Dom_Query_Css2XpathTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /** Zend_Dom_Query_Css2Xpath */
  8. require_once 'Zend/Dom/Query/Css2Xpath.php';
  9. /**
  10. * Test class for Zend_Dom_Query_Css2Xpath.
  11. */
  12. class Zend_Dom_Query_Css2XpathTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. $suite = new PHPUnit_Framework_TestSuite("Zend_Dom_Query_Css2XpathTest");
  22. $result = PHPUnit_TextUI_TestRunner::run($suite);
  23. }
  24. /**
  25. * Sets up the fixture, for example, open a network connection.
  26. * This method is called before a test is executed.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. }
  33. /**
  34. * Tears down the fixture, for example, close a network connection.
  35. * This method is called after a test is executed.
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. }
  42. public function testTransformShouldBeCalledStatically()
  43. {
  44. Zend_Dom_Query_Css2Xpath::transform('');
  45. }
  46. public function testTransformShouldReturnStringByDefault()
  47. {
  48. $test = Zend_Dom_Query_Css2Xpath::transform('');
  49. $this->assertTrue(is_string($test));
  50. }
  51. public function testTransformShouldReturnMultiplePathsWhenExpressionContainsCommas()
  52. {
  53. $test = Zend_Dom_Query_Css2Xpath::transform('#foo, #bar');
  54. $this->assertTrue(is_array($test));
  55. $this->assertEquals(2, count($test));
  56. }
  57. public function testTransformShouldRecognizeHashSymbolAsId()
  58. {
  59. $test = Zend_Dom_Query_Css2Xpath::transform('#foo');
  60. $this->assertEquals("//*[@id='foo']", $test);
  61. }
  62. public function testTransformShouldRecognizeDotSymbolAsClass()
  63. {
  64. $test = Zend_Dom_Query_Css2Xpath::transform('.foo');
  65. $this->assertEquals("//*[contains(@class, ' foo ')]", $test);
  66. }
  67. public function testTransformShouldAssumeSpacesToIndicateRelativeXpathQueries()
  68. {
  69. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo .bar');
  70. $this->assertContains(' | ', $test);
  71. $expected = array(
  72. "//div[@id='foo']//*[contains(@class, ' bar ')]",
  73. "//div[@id='foo'][contains(@class, ' bar ')]",
  74. );
  75. foreach ($expected as $path) {
  76. $this->assertContains($path, $test);
  77. }
  78. }
  79. public function testTransformShouldWriteChildSelectorsAsAbsoluteXpathRelations()
  80. {
  81. $test = Zend_Dom_Query_Css2Xpath::transform('div#foo>span');
  82. $this->assertEquals("//div[@id='foo']/span", $test);
  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_array($test));
  88. $expected = array(
  89. "//div[@id='foo']//span[contains(@class, ' bar ')]",
  90. "//*[@id='bar']//li[contains(@class, ' baz ')]//a",
  91. );
  92. $this->assertEquals(count($expected), count($test));
  93. foreach ($test as $path) {
  94. $this->assertContains($path, $expected);
  95. }
  96. }
  97. public function testClassNotationWithoutSpecifiedTagShouldResultInMultipleQueries()
  98. {
  99. $test = Zend_Dom_Query_Css2Xpath::transform('div.foo .bar a .baz span');
  100. $this->assertContains(' | ', $test);
  101. $segments = array(
  102. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  103. "//div[contains(@class, ' foo ')]//*[contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  104. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a//*[contains(@class, ' baz ')]//span",
  105. "//div[contains(@class, ' foo ')][contains(@class, ' bar ')]//a[contains(@class, ' baz ')]//span",
  106. );
  107. foreach ($segments as $xpath) {
  108. $this->assertContains($xpath, $test);
  109. }
  110. }
  111. public function testShouldAllowEqualitySelectionOfArbitraryAttributes()
  112. {
  113. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo="bar"]');
  114. $this->assertEquals("//div[@foo='bar']", $test);
  115. }
  116. public function testShouldCastAttributeNamesToLowerCase()
  117. {
  118. $test = Zend_Dom_Query_Css2Xpath::transform('div[dojoType="bar"]');
  119. $this->assertEquals("//div[@dojotype='bar']", $test);
  120. }
  121. public function testShouldAllowContentSubSelectionOfArbitraryAttributes()
  122. {
  123. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo~="bar"]');
  124. $this->assertEquals("//div[contains(@foo, ' bar ')]", $test);
  125. }
  126. public function testShouldAllowContentMatchingOfArbitraryAttributes()
  127. {
  128. $test = Zend_Dom_Query_Css2Xpath::transform('div[foo*="bar"]');
  129. $this->assertEquals("//div[contains(@foo, 'bar')]", $test);
  130. }
  131. }
  132. // Call Zend_Dom_Query_Css2XpathTest::main() if this source file is executed directly.
  133. if (PHPUnit_MAIN_METHOD == "Zend_Dom_Query_Css2XpathTest::main") {
  134. Zend_Dom_Query_Css2XpathTest::main();
  135. }