TokenizerTest.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Tokenizer;
  12. class TokenizerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $tokenizer;
  15. protected function setUp()
  16. {
  17. $this->tokenizer = new Tokenizer();
  18. }
  19. /**
  20. * @dataProvider getCssSelectors
  21. */
  22. public function testTokenize($css)
  23. {
  24. $this->assertEquals($css, $this->tokensToString($this->tokenizer->tokenize($css)), '->tokenize() lexes an input string and returns an array of tokens');
  25. }
  26. public function testTokenizeWithQuotedStrings()
  27. {
  28. $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');
  29. $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');
  30. $this->assertEquals("img[alt= ]", $this->tokensToString($this->tokenizer->tokenize('img[alt=""]')), '->tokenize() lexes an input string and returns an array of tokens');
  31. }
  32. /**
  33. * @expectedException Symfony\Component\CssSelector\Exception\ParseException
  34. */
  35. public function testTokenizeInvalidString()
  36. {
  37. $this->tokensToString($this->tokenizer->tokenize('/invalid'));
  38. }
  39. public function getCssSelectors()
  40. {
  41. return array(
  42. array('h1'),
  43. array('h1:nth-child(3n+1)'),
  44. array('h1 > p'),
  45. array('h1#foo'),
  46. array('h1.foo'),
  47. array('h1[class*=foo]'),
  48. array('h1 .foo'),
  49. array('h1 #foo'),
  50. array('h1 [class*=foo]'),
  51. );
  52. }
  53. protected function tokensToString($tokens)
  54. {
  55. $str = '';
  56. foreach ($tokens as $token) {
  57. $str .= str_repeat(' ', $token->getPosition() - strlen($str)).$token;
  58. }
  59. return $str;
  60. }
  61. }