AbstractHandlerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Component\CssSelector\Tests\Parser\Handler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\CssSelector\Parser\Reader;
  13. use Symfony\Component\CssSelector\Parser\Token;
  14. use Symfony\Component\CssSelector\Parser\TokenStream;
  15. /**
  16. * @author Jean-François Simon <contact@jfsimon.fr>
  17. */
  18. abstract class AbstractHandlerTest extends TestCase
  19. {
  20. /** @dataProvider getHandleValueTestData */
  21. public function testHandleValue($value, Token $expectedToken, $remainingContent)
  22. {
  23. $reader = new Reader($value);
  24. $stream = new TokenStream();
  25. $this->assertTrue($this->generateHandler()->handle($reader, $stream));
  26. $this->assertEquals($expectedToken, $stream->getNext());
  27. $this->assertRemainingContent($reader, $remainingContent);
  28. }
  29. /** @dataProvider getDontHandleValueTestData */
  30. public function testDontHandleValue($value)
  31. {
  32. $reader = new Reader($value);
  33. $stream = new TokenStream();
  34. $this->assertFalse($this->generateHandler()->handle($reader, $stream));
  35. $this->assertStreamEmpty($stream);
  36. $this->assertRemainingContent($reader, $value);
  37. }
  38. abstract public function getHandleValueTestData();
  39. abstract public function getDontHandleValueTestData();
  40. abstract protected function generateHandler();
  41. protected function assertStreamEmpty(TokenStream $stream)
  42. {
  43. $property = new \ReflectionProperty($stream, 'tokens');
  44. $property->setAccessible(true);
  45. $this->assertEquals([], $property->getValue($stream));
  46. }
  47. protected function assertRemainingContent(Reader $reader, $remainingContent)
  48. {
  49. if ('' === $remainingContent) {
  50. $this->assertEquals(0, $reader->getRemainingLength());
  51. $this->assertTrue($reader->isEOF());
  52. } else {
  53. $this->assertEquals(\strlen($remainingContent), $reader->getRemainingLength());
  54. $this->assertEquals(0, $reader->getOffset($remainingContent));
  55. }
  56. }
  57. }