CssSelectorTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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;
  11. use Symfony\Component\CssSelector\CssSelector;
  12. class CssSelectorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testCsstoXPath()
  15. {
  16. $this->assertEquals('descendant-or-self::*', CssSelector::toXPath(''));
  17. $this->assertEquals('descendant-or-self::h1', CssSelector::toXPath('h1'));
  18. $this->assertEquals("descendant-or-self::h1[@id = 'foo']", CssSelector::toXPath('h1#foo'));
  19. $this->assertEquals("descendant-or-self::h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", CssSelector::toXPath('h1.foo'));
  20. $this->assertEquals('descendant-or-self::foo:h1', CssSelector::toXPath('foo|h1'));
  21. }
  22. /**
  23. * @dataProvider getCssSelectors
  24. */
  25. public function testParse($css, $xpath)
  26. {
  27. $parser = new CssSelector();
  28. $this->assertEquals($xpath, (string) $parser->parse($css)->toXPath(), '->parse() parses an input string and returns a node');
  29. }
  30. public function testParseExceptions()
  31. {
  32. $parser = new CssSelector();
  33. try {
  34. $parser->parse('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 symbol, got '' at h1: -> ", $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
  39. }
  40. }
  41. public function getCssSelectors()
  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) mod 3 = 0 and position() >= 1)]"),
  48. array('h1 > p', "h1/p"),
  49. array('h1#foo', "h1[@id = 'foo']"),
  50. array('h1.foo', "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  51. array('h1[class*="foo bar"]', "h1[contains(@class, 'foo bar')]"),
  52. array('h1[foo|class*="foo bar"]', "h1[contains(@foo:class, 'foo bar')]"),
  53. array('h1[class]', "h1[@class]"),
  54. array('h1 .foo', "h1/descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  55. array('h1 #foo', "h1/descendant::*[@id = 'foo']"),
  56. array('h1 [class*=foo]', "h1/descendant::*[contains(@class, 'foo')]"),
  57. array('div>.foo', "div/*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  58. array('div > .foo', "div/*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
  59. );
  60. }
  61. }