4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\CssSelector\CssSelector;
20 class CssSelectorTest extends TestCase
22 public function testCssToXPath()
24 $this->assertEquals('descendant-or-self::*', CssSelector::toXPath(''));
25 $this->assertEquals('descendant-or-self::h1', CssSelector::toXPath('h1'));
26 $this->assertEquals("descendant-or-self::h1[@id = 'foo']", CssSelector::toXPath('h1#foo'));
27 $this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", CssSelector::toXPath('h1.foo'));
28 $this->assertEquals('descendant-or-self::foo:h1', CssSelector::toXPath('foo|h1'));
31 /** @dataProvider getCssToXPathWithoutPrefixTestData */
32 public function testCssToXPathWithoutPrefix($css, $xpath)
34 $this->assertEquals($xpath, CssSelector::toXPath($css, ''), '->parse() parses an input string and returns a node');
37 public function testParseExceptions()
40 CssSelector::toXPath('h1:');
41 $this->fail('->parse() throws an Exception if the css selector is not valid');
42 } catch (\Exception $e) {
43 $this->assertInstanceOf('\Symfony\Component\CssSelector\Exception\ParseException', $e, '->parse() throws an Exception if the css selector is not valid');
44 $this->assertEquals('Expected identifier, but <eof at 3> found.', $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
48 public function getCssToXPathWithoutPrefixTestData()
52 array('foo|h1', 'foo:h1'),
53 array('h1, h2, h3', 'h1 | h2 | h3'),
54 array('h1:nth-child(3n+1)', "*/*[(name() = 'h1') and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
55 array('h1 > p', 'h1/p'),
56 array('h1#foo', "h1[@id = 'foo']"),
57 array('h1.foo', "h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
58 array('h1[class*="foo bar"]', "h1[@class and contains(@class, 'foo bar')]"),
59 array('h1[foo|class*="foo bar"]', "h1[@foo:class and contains(@foo:class, 'foo bar')]"),
60 array('h1[class]', 'h1[@class]'),
61 array('h1 .foo', "h1/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
62 array('h1 #foo', "h1/descendant-or-self::*/*[@id = 'foo']"),
63 array('h1 [class*=foo]', "h1/descendant-or-self::*/*[@class and contains(@class, 'foo')]"),
64 array('div>.foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
65 array('div > .foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),