CssSelectorConverterTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Component\CssSelector\Tests;
  11. use Symfony\Component\CssSelector\CssSelectorConverter;
  12. class CssSelectorConverterTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testCssToXPath()
  15. {
  16. $converter = new CssSelectorConverter();
  17. $this->assertEquals('descendant-or-self::*', $converter->toXPath(''));
  18. $this->assertEquals('descendant-or-self::h1', $converter->toXPath('h1'));
  19. $this->assertEquals("descendant-or-self::h1[@id = 'foo']", $converter->toXPath('h1#foo'));
  20. $this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
  21. $this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
  22. $this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
  23. }
  24. public function testCssToXPathXml()
  25. {
  26. $converter = new CssSelectorConverter(false);
  27. $this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
  28. }
  29. /**
  30. * @expectedException \Symfony\Component\CssSelector\Exception\ParseException
  31. * @expectedExceptionMessage Expected identifier, but <eof at 3> found.
  32. */
  33. public function testParseExceptions()
  34. {
  35. $converter = new CssSelectorConverter();
  36. $converter->toXPath('h1:');
  37. }
  38. /** @dataProvider getCssToXPathWithoutPrefixTestData */
  39. public function testCssToXPathWithoutPrefix($css, $xpath)
  40. {
  41. $converter = new CssSelectorConverter();
  42. $this->assertEquals($xpath, $converter->toXPath($css, ''), '->parse() parses an input string and returns a node');
  43. }
  44. public function getCssToXPathWithoutPrefixTestData()
  45. {
  46. return array(
  47. array('h1', 'h1'),
  48. array('foo|h1', 'foo:h1'),
  49. array('h1, h2, h3', 'h1 | h2 | h3'),
  50. array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
  51. array('h1 > p', 'h1/p'),
  52. array('h1#foo', "h1[@id = 'foo']"),
  53. array('h1.foo', "h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  54. array('h1[class*="foo bar"]', "h1[@class and contains(@class, 'foo bar')]"),
  55. array('h1[foo|class*="foo bar"]', "h1[@foo:class and contains(@foo:class, 'foo bar')]"),
  56. array('h1[class]', 'h1[@class]'),
  57. array('h1 .foo', "h1/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  58. array('h1 #foo', "h1/descendant-or-self::*/*[@id = 'foo']"),
  59. array('h1 [class*=foo]', "h1/descendant-or-self::*/*[@class and contains(@class, 'foo')]"),
  60. array('div>.foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  61. array('div > .foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  62. );
  63. }
  64. }