AttribNodeTest.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Components\CssSelector\Node;
  11. use Symfony\Components\CssSelector\Node\AttribNode;
  12. use Symfony\Components\CssSelector\Node\ElementNode;
  13. class AttribNodeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testToXpath()
  16. {
  17. $element = new ElementNode('*', 'h1');
  18. $operators = array(
  19. '^=' => "h1[starts-with(@class, 'foo')]",
  20. '$=' => "h1[substring(@class, string-length(@class)-2) = 'foo']",
  21. '*=' => "h1[contains(@class, 'foo')]",
  22. '=' => "h1[@class = 'foo']",
  23. '~=' => "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
  24. '|=' => "h1[@class = 'foo' or starts-with(@class, 'foo-')]",
  25. '!=' => "h1[not(@class) or @class != 'foo']",
  26. );
  27. // h1[class??foo]
  28. foreach ($operators as $op => $xpath)
  29. {
  30. $attrib = new AttribNode($element, '*', 'class', $op, 'foo');
  31. $this->assertEquals($xpath, (string) $attrib->toXpath(), '->toXpath() returns the xpath representation of the node');
  32. }
  33. // h1[class]
  34. $attrib = new AttribNode($element, '*', 'class', 'exists', 'foo');
  35. $this->assertEquals('h1[@class]', (string) $attrib->toXpath(), '->toXpath() returns the xpath representation of the node');
  36. }
  37. }