TokenCollectionTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php declare(strict_types = 1);
  2. namespace TheSeer\Tokenizer;
  3. use PHPUnit\Framework\TestCase;
  4. /**
  5. * @covers \TheSeer\Tokenizer\TokenCollection
  6. */
  7. class TokenCollectionTest extends TestCase {
  8. /** @var TokenCollection */
  9. private $collection;
  10. protected function setUp(): void {
  11. $this->collection = new TokenCollection();
  12. }
  13. public function testCollectionIsInitiallyEmpty(): void {
  14. $this->assertCount(0, $this->collection);
  15. }
  16. public function testTokenCanBeAddedToCollection(): void {
  17. $token = $this->createMock(Token::class);
  18. $this->collection->addToken($token);
  19. $this->assertCount(1, $this->collection);
  20. $this->assertSame($token, $this->collection[0]);
  21. }
  22. public function testCanIterateOverTokens(): void {
  23. $token = $this->createMock(Token::class);
  24. $this->collection->addToken($token);
  25. $this->collection->addToken($token);
  26. foreach ($this->collection as $position => $current) {
  27. $this->assertIsInt($position);
  28. $this->assertSame($token, $current);
  29. }
  30. }
  31. public function testOffsetCanBeUnset(): void {
  32. $token = $this->createMock(Token::class);
  33. $this->collection->addToken($token);
  34. $this->assertCount(1, $this->collection);
  35. unset($this->collection[0]);
  36. $this->assertCount(0, $this->collection);
  37. }
  38. public function testTokenCanBeSetViaOffsetPosition(): void {
  39. $token = $this->createMock(Token::class);
  40. $this->collection[0] = $token;
  41. $this->assertCount(1, $this->collection);
  42. $this->assertSame($token, $this->collection[0]);
  43. }
  44. public function testTryingToUseNonIntegerOffsetThrowsException(): void {
  45. $this->expectException(TokenCollectionException::class);
  46. $this->collection['foo'] = $this->createMock(Token::class);
  47. }
  48. public function testTryingToSetNonTokenAtOffsetThrowsException(): void {
  49. $this->expectException(TokenCollectionException::class);
  50. $this->collection[0] = 'abc';
  51. }
  52. public function testTryingToGetTokenAtNonExistingOffsetThrowsException(): void {
  53. $this->expectException(TokenCollectionException::class);
  54. $x = $this->collection[3];
  55. }
  56. }