TokenCollectionTest.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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() {
  11. $this->collection = new TokenCollection();
  12. }
  13. public function testCollectionIsInitiallyEmpty() {
  14. $this->assertCount(0, $this->collection);
  15. }
  16. public function testTokenCanBeAddedToCollection() {
  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() {
  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->assertInternalType('integer', $position);
  28. $this->assertSame($token, $current);
  29. }
  30. }
  31. public function testOffsetCanBeUnset() {
  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() {
  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() {
  45. $this->expectException(TokenCollectionException::class);
  46. $this->collection['foo'] = $this->createMock(Token::class);
  47. }
  48. public function testTryingToSetNonTokenAtOffsetThrowsException() {
  49. $this->expectException(TokenCollectionException::class);
  50. $this->collection[0] = 'abc';
  51. }
  52. public function testTryingToGetTokenAtNonExistingOffsetThrowsException() {
  53. $this->expectException(TokenCollectionException::class);
  54. $x = $this->collection[3];
  55. }
  56. }