TokenizerTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Components\CssSelector;
  11. use Symfony\Components\CssSelector\Tokenizer;
  12. class TokenizerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $tokenizer;
  15. public 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. }
  31. public function getCssSelectors()
  32. {
  33. return array(
  34. array('h1'),
  35. array('h1:nth-child(3n+1)'),
  36. array('h1 > p'),
  37. array('h1#foo'),
  38. array('h1.foo'),
  39. array('h1[class*=foo]'),
  40. array('h1 .foo'),
  41. array('h1 #foo'),
  42. array('h1 [class*=foo]'),
  43. );
  44. }
  45. protected function tokensToString($tokens)
  46. {
  47. $str = '';
  48. foreach ($tokens as $token) {
  49. $str .= str_repeat(' ', $token->getPosition() - strlen($str)).$token;
  50. }
  51. return $str;
  52. }
  53. }