FunctionNodeTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\CssSelector\Node;
  11. use Symfony\Component\CssSelector\Node\FunctionNode;
  12. use Symfony\Component\CssSelector\Node\ElementNode;
  13. use Symfony\Component\CssSelector\Token;
  14. class FunctionNodeTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testToXpath()
  17. {
  18. $element = new ElementNode('*', 'h1');
  19. // h1:contains("foo")
  20. $function = new FunctionNode($element, ':', 'contains', 'foo');
  21. $this->assertEquals("h1[contains(string(.), 'foo')]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  22. // h1:nth-child(1)
  23. $function = new FunctionNode($element, ':', 'nth-child', 1);
  24. $this->assertEquals("*/*[name() = 'h1' and (position() = 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  25. // h1:nth-child()
  26. $function = new FunctionNode($element, ':', 'nth-child', '');
  27. $this->assertEquals("h1[false() and position() = 0]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  28. // h1:nth-child(odd)
  29. $element2 = new ElementNode('*', new Token('Symbol', 'odd', -1));
  30. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  31. $this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 2 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  32. // h1:nth-child(even)
  33. $element2 = new ElementNode('*', new Token('Symbol', 'even', -1));
  34. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  35. $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  36. // h1:nth-child(n)
  37. $element2 = new ElementNode('*', new Token('Symbol', 'n', -1));
  38. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  39. $this->assertEquals("*/*[name() = 'h1' and (position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  40. // h1:nth-child(3n+1)
  41. $element2 = new ElementNode('*', new Token('Symbol', '3n+1', -1));
  42. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  43. $this->assertEquals("*/*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  44. // h1:nth-child(n+1)
  45. $element2 = new ElementNode('*', new Token('Symbol', 'n+1', -1));
  46. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  47. $this->assertEquals("*/*[name() = 'h1' and (position() >= 1)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  48. // h1:nth-child(1)
  49. $element2 = new ElementNode('*', new Token('Symbol', '2', -1));
  50. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  51. $this->assertEquals("*/*[name() = 'h1' and (position() = 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  52. // h1:nth-child(2n)
  53. $element2 = new ElementNode('*', new Token('Symbol', '2n', -1));
  54. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  55. $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  56. // h1:nth-child(-n)
  57. $element2 = new ElementNode('*', new Token('Symbol', '-n', -1));
  58. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  59. $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod -1 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  60. // h1:nth-last-child(2)
  61. $function = new FunctionNode($element, ':', 'nth-last-child', 2);
  62. $this->assertEquals("*/*[name() = 'h1' and (position() = last() - 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  63. // h1:nth-of-type(2)
  64. $function = new FunctionNode($element, ':', 'nth-of-type', 2);
  65. $this->assertEquals("*/h1[position() = 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  66. // h1:nth-last-of-type(2)
  67. $function = new FunctionNode($element, ':', 'nth-last-of-type', 2);
  68. $this->assertEquals("*/h1[position() = last() - 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  69. /*
  70. // h1:not(p)
  71. $element2 = new ElementNode('*', 'p');
  72. $function = new FunctionNode($element, ':', 'not', $element2);
  73. $this->assertEquals("h1[not()]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  74. */
  75. }
  76. }