CssSelectorTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\CssSelector;
  13. /**
  14. * @group legacy
  15. */
  16. class CssSelectorTest extends TestCase
  17. {
  18. public function testCssToXPath()
  19. {
  20. $this->assertEquals('descendant-or-self::*', CssSelector::toXPath(''));
  21. $this->assertEquals('descendant-or-self::h1', CssSelector::toXPath('h1'));
  22. $this->assertEquals("descendant-or-self::h1[@id = 'foo']", CssSelector::toXPath('h1#foo'));
  23. $this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", CssSelector::toXPath('h1.foo'));
  24. $this->assertEquals('descendant-or-self::foo:h1', CssSelector::toXPath('foo|h1'));
  25. }
  26. /** @dataProvider getCssToXPathWithoutPrefixTestData */
  27. public function testCssToXPathWithoutPrefix($css, $xpath)
  28. {
  29. $this->assertEquals($xpath, CssSelector::toXPath($css, ''), '->parse() parses an input string and returns a node');
  30. }
  31. public function testParseExceptions()
  32. {
  33. try {
  34. CssSelector::toXPath('h1:');
  35. $this->fail('->parse() throws an Exception if the css selector is not valid');
  36. } catch (\Exception $e) {
  37. $this->assertInstanceOf('\Symfony\Component\CssSelector\Exception\ParseException', $e, '->parse() throws an Exception if the css selector is not valid');
  38. $this->assertEquals('Expected identifier, but <eof at 3> found.', $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
  39. }
  40. }
  41. public function getCssToXPathWithoutPrefixTestData()
  42. {
  43. return array(
  44. array('h1', 'h1'),
  45. array('foo|h1', 'foo:h1'),
  46. array('h1, h2, h3', 'h1 | h2 | h3'),
  47. array('h1:nth-child(3n+1)', "*/*[(name() = 'h1') and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
  48. array('h1 > p', 'h1/p'),
  49. array('h1#foo', "h1[@id = 'foo']"),
  50. array('h1.foo', "h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  51. array('h1[class*="foo bar"]', "h1[@class and contains(@class, 'foo bar')]"),
  52. array('h1[foo|class*="foo bar"]', "h1[@foo:class and contains(@foo:class, 'foo bar')]"),
  53. array('h1[class]', 'h1[@class]'),
  54. array('h1 .foo', "h1/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  55. array('h1 #foo', "h1/descendant-or-self::*/*[@id = 'foo']"),
  56. array('h1 [class*=foo]', "h1/descendant-or-self::*/*[@class and contains(@class, 'foo')]"),
  57. array('div>.foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  58. array('div > .foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  59. );
  60. }
  61. }