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
2010-08-20 22:09:55 +01:00
use Symfony\Component\CssSelector\Tokenizer ;
2010-03-31 07:42:18 +01:00
class TokenizerTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
protected $tokenizer ;
2010-04-01 14:19:48 +01:00
2010-12-06 14:45:37 +00:00
protected function setUp ()
2010-05-06 12:25:53 +01:00
{
$this -> tokenizer = new Tokenizer ();
}
2010-04-01 14:19:48 +01:00
2010-05-06 12:25:53 +01:00
/**
* @ dataProvider getCssSelectors
*/
public function testTokenize ( $css )
{
$this -> assertEquals ( $css , $this -> tokensToString ( $this -> tokenizer -> tokenize ( $css )), '->tokenize() lexes an input string and returns an array of tokens' );
}
2010-03-31 07:42:18 +01:00
2010-05-06 12:25:53 +01:00
public function testTokenizeWithQuotedStrings ()
{
$this -> assertEquals ( 'foo[class=foo bar ]' , $this -> tokensToString ( $this -> tokenizer -> tokenize ( 'foo[class="foo bar"]' )), '->tokenize() lexes an input string and returns an array of tokens' );
$this -> assertEquals ( " foo[class=foo Abar ] " , $this -> tokensToString ( $this -> tokenizer -> tokenize ( 'foo[class="foo \\65 bar"]' )), '->tokenize() lexes an input string and returns an array of tokens' );
2012-12-11 18:25:03 +00:00
$this -> assertEquals ( " img[alt= ] " , $this -> tokensToString ( $this -> tokenizer -> tokenize ( 'img[alt=""]' )), '->tokenize() lexes an input string and returns an array of tokens' );
2010-05-06 12:25:53 +01:00
}
2010-03-31 07:42:18 +01:00
2011-06-29 17:22:32 +01:00
/**
* @ expectedException Symfony\Component\CssSelector\Exception\ParseException
*/
public function testTokenizeInvalidString ()
{
$this -> tokensToString ( $this -> tokenizer -> tokenize ( '/invalid' ));
}
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' ),
array ( 'h1:nth-child(3n+1)' ),
array ( 'h1 > p' ),
array ( 'h1#foo' ),
array ( 'h1.foo' ),
array ( 'h1[class*=foo]' ),
array ( 'h1 .foo' ),
array ( 'h1 #foo' ),
array ( 'h1 [class*=foo]' ),
);
2010-03-31 07:42:18 +01:00
}
2010-05-06 12:25:53 +01:00
protected function tokensToString ( $tokens )
{
$str = '' ;
2010-05-07 15:09:11 +01:00
foreach ( $tokens as $token ) {
2010-05-06 12:25:53 +01:00
$str .= str_repeat ( ' ' , $token -> getPosition () - strlen ( $str )) . $token ;
}
return $str ;
}
2010-03-31 07:42:18 +01:00
}