No Description

CssSelectorConverterTest.php 3.2KB

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