FunctionNodeTest.php 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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(2n)
  49. $element2 = new ElementNode('*', new Token('Symbol', '2n', -1));
  50. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  51. $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod 2 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  52. // h1:nth-child(-n)
  53. $element2 = new ElementNode('*', new Token('Symbol', '-n', -1));
  54. $function = new FunctionNode($element, ':', 'nth-child', $element2);
  55. $this->assertEquals("*/*[name() = 'h1' and ((position() +0) mod -1 = 0 and position() >= 0)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  56. // h1:nth-last-child(2)
  57. $function = new FunctionNode($element, ':', 'nth-last-child', 2);
  58. $this->assertEquals("*/*[name() = 'h1' and (position() = last() - 2)]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  59. // h1:nth-of-type(2)
  60. $function = new FunctionNode($element, ':', 'nth-of-type', 2);
  61. $this->assertEquals("*/h1[position() = 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  62. // h1:nth-last-of-type(2)
  63. $function = new FunctionNode($element, ':', 'nth-last-of-type', 2);
  64. $this->assertEquals("*/h1[position() = last() - 2]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  65. /*
  66. // h1:not(p)
  67. $element2 = new ElementNode('*', 'p');
  68. $function = new FunctionNode($element, ':', 'not', $element2);
  69. $this->assertEquals("h1[not()]", (string) $function->toXpath(), '->toXpath() returns the xpath representation of the node');
  70. */
  71. }
  72. }