Pull merge.
[yaffs-website] / vendor / symfony / css-selector / Tests / CssSelectorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\CssSelector\CssSelector;
16
17 /**
18  * @group legacy
19  */
20 class CssSelectorTest extends TestCase
21 {
22     public function testCssToXPath()
23     {
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'));
29     }
30
31     /** @dataProvider getCssToXPathWithoutPrefixTestData */
32     public function testCssToXPathWithoutPrefix($css, $xpath)
33     {
34         $this->assertEquals($xpath, CssSelector::toXPath($css, ''), '->parse() parses an input string and returns a node');
35     }
36
37     public function testParseExceptions()
38     {
39         try {
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');
45         }
46     }
47
48     public function getCssToXPathWithoutPrefixTestData()
49     {
50         return array(
51             array('h1', 'h1'),
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 ')]"),
66         );
67     }
68 }