2010-03-31 07:42:18 +01:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-03-31 07:42:18 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-03-31 07:42:18 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\CssSelector ;
2010-03-31 07:42:18 +01:00
2011-06-15 06:52:56 +01:00
use Symfony\Component\CssSelector\CssSelector ;
2010-03-31 07:42:18 +01:00
2011-06-15 06:52:56 +01:00
class CssSelectorTest extends \PHPUnit_Framework_TestCase
2010-03-31 07:42:18 +01:00
{
2011-06-15 06:52:56 +01:00
public function testCsstoXPath ()
2010-05-06 12:25:53 +01:00
{
2012-03-11 09:18:25 +00:00
$this -> assertEquals ( 'descendant-or-self::*' , CssSelector :: toXPath ( '' ));
2011-06-15 06:52:56 +01:00
$this -> assertEquals ( 'descendant-or-self::h1' , CssSelector :: toXPath ( 'h1' ));
$this -> assertEquals ( " descendant-or-self::h1[@id = 'foo'] " , CssSelector :: toXPath ( 'h1#foo' ));
$this -> assertEquals ( " descendant-or-self::h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')] " , CssSelector :: toXPath ( 'h1.foo' ));
2010-03-31 07:42:18 +01:00
2011-06-15 06:52:56 +01:00
$this -> assertEquals ( 'descendant-or-self::foo:h1' , CssSelector :: toXPath ( 'foo|h1' ));
2010-05-06 12:25:53 +01:00
}
2010-03-31 07:42:18 +01:00
2010-05-06 12:25:53 +01:00
/**
* @ dataProvider getCssSelectors
*/
public function testParse ( $css , $xpath )
{
2011-06-15 06:52:56 +01:00
$parser = new CssSelector ();
2010-03-31 07:42:18 +01:00
2011-06-15 06:52:56 +01:00
$this -> assertEquals ( $xpath , ( string ) $parser -> parse ( $css ) -> toXPath (), '->parse() parses an input string and returns a node' );
2010-05-06 12:25:53 +01:00
}
2010-03-31 07:42:18 +01:00
2010-05-06 12:25:53 +01:00
public function testParseExceptions ()
2010-03-31 07:42:18 +01:00
{
2011-06-15 06:52:56 +01:00
$parser = new CssSelector ();
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$parser -> parse ( 'h1:' );
$this -> fail ( '->parse() throws an Exception if the css selector is not valid' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2011-06-14 13:12:03 +01:00
$this -> assertInstanceOf ( '\Symfony\Component\CssSelector\Exception\ParseException' , $e , '->parse() throws an Exception if the css selector is not valid' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( " Expected symbol, got '' at h1: -> " , $e -> getMessage (), '->parse() throws an Exception if the css selector is not valid' );
}
2010-03-31 07:42:18 +01:00
}
2010-05-06 12:25:53 +01:00
public function getCssSelectors ()
2010-03-31 07:42:18 +01:00
{
2010-05-06 12:25:53 +01:00
return array (
array ( 'h1' , " h1 " ),
array ( 'foo|h1' , " foo:h1 " ),
array ( 'h1, h2, h3' , " h1 | h2 | h3 " ),
array ( 'h1:nth-child(3n+1)' , " */*[name() = 'h1' and ((position() -1) mod 3 = 0 and position() >= 1)] " ),
array ( 'h1 > p' , " h1/p " ),
array ( 'h1#foo' , " h1[@id = 'foo'] " ),
array ( 'h1.foo' , " h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')] " ),
array ( 'h1[class*="foo bar"]' , " h1[contains(@class, 'foo bar')] " ),
array ( 'h1[foo|class*="foo bar"]' , " h1[contains(@foo:class, 'foo bar')] " ),
array ( 'h1[class]' , " h1[@class] " ),
array ( 'h1 .foo' , " h1/descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')] " ),
array ( 'h1 #foo' , " h1/descendant::*[@id = 'foo'] " ),
array ( 'h1 [class*=foo]' , " h1/descendant::*[contains(@class, 'foo')] " ),
);
2010-03-31 07:42:18 +01:00
}
}