AbstractHandlerTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(array(), $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. }