ArrayCollectionTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Unit\Util;
  12. use League\CommonMark\Util\ArrayCollection;
  13. use PHPUnit\Framework\TestCase;
  14. final class ArrayCollectionTest extends TestCase
  15. {
  16. public function testConstructorAndToArray(): void
  17. {
  18. $collection = new ArrayCollection();
  19. $this->assertEquals([], $collection->toArray());
  20. $array = [];
  21. $collection = new ArrayCollection($array);
  22. $this->assertEquals($array, $collection->toArray());
  23. $array = ['foo', 'bar'];
  24. $collection = new ArrayCollection($array);
  25. $this->assertEquals($array, $collection->toArray());
  26. }
  27. public function testFirst(): void
  28. {
  29. $collection = new ArrayCollection(['foo', 'bar']);
  30. $this->assertEquals('foo', $collection->first());
  31. }
  32. public function testLast(): void
  33. {
  34. $collection = new ArrayCollection(['foo', 'bar']);
  35. $this->assertEquals('bar', $collection->last());
  36. }
  37. public function testGetIterator(): void
  38. {
  39. $array = ['foo', 'bar'];
  40. $collection = new ArrayCollection($array);
  41. $iterator = $collection->getIterator();
  42. $this->assertTrue($iterator instanceof \ArrayIterator);
  43. $this->assertEquals($array, $iterator->getArrayCopy());
  44. }
  45. public function testOffsetExists(): void
  46. {
  47. $collection = new ArrayCollection(['foo', 2 => 'bar', 3, null]);
  48. $this->assertTrue($collection->offsetExists(0));
  49. $this->assertTrue($collection->offsetExists(2));
  50. $this->assertTrue($collection->offsetExists(3));
  51. $this->assertTrue($collection->offsetExists(4));
  52. $this->assertTrue(isset($collection[0]));
  53. $this->assertTrue(isset($collection[2]));
  54. $this->assertTrue(isset($collection[3]));
  55. $this->assertTrue(isset($collection[4]));
  56. $this->assertFalse($collection->offsetExists(1));
  57. $this->assertFalse(isset($collection[1]));
  58. }
  59. public function testOffsetGet(): void
  60. {
  61. $collection = new ArrayCollection(['foo', 2 => 'bar']);
  62. $this->assertEquals('foo', $collection[0]);
  63. $this->assertEquals('foo', $collection->offsetGet(0));
  64. $this->assertEquals('bar', $collection[2]);
  65. $this->assertEquals('bar', $collection->offsetGet(2));
  66. }
  67. public function testOffsetSet(): void
  68. {
  69. $collection = new ArrayCollection();
  70. $collection[] = 'foo';
  71. $this->assertEquals(['foo'], $collection->toArray());
  72. $collection[] = 'bar';
  73. $this->assertEquals(['foo', 'bar'], $collection->toArray());
  74. $collection = new ArrayCollection(['foo']);
  75. $collection[42] = true;
  76. $this->assertEquals(['foo', 42 => true], $collection->toArray());
  77. $collection[42] = false;
  78. $this->assertEquals(['foo', 42 => false], $collection->toArray());
  79. }
  80. public function testOffsetUnset(): void
  81. {
  82. $collection = new ArrayCollection(['foo', 'bar', 'baz']);
  83. unset($collection[0]);
  84. $this->assertEquals([1 => 'bar', 2 => 'baz'], $collection->toArray());
  85. unset($collection[9999]);
  86. $this->assertEquals([1 => 'bar', 2 => 'baz'], $collection->toArray());
  87. unset($collection[1]);
  88. $this->assertEquals([2 => 'baz'], $collection->toArray());
  89. unset($collection[2]);
  90. $this->assertEquals([], $collection->toArray());
  91. }
  92. public function testOffsetUnsetWithNulls(): void
  93. {
  94. $collection = new ArrayCollection([2 => null]);
  95. unset($collection[99999]);
  96. $this->assertEquals([2 => null], $collection->toArray());
  97. unset($collection[2]);
  98. $this->assertEquals([], $collection->toArray());
  99. }
  100. public function testSlice(): void
  101. {
  102. $collection = new ArrayCollection(['foo', 'bar', 'baz', 42 => 'ftw']);
  103. $this->assertEquals(['foo', 'bar', 'baz', 42 => 'ftw'], $collection->slice(0));
  104. $this->assertEquals(['foo', 'bar', 'baz', 42 => 'ftw'], $collection->slice(0, null));
  105. $this->assertEquals(['foo', 'bar', 'baz', 42 => 'ftw'], $collection->slice(0, 99));
  106. $this->assertEquals(['foo', 'bar', 'baz', 42 => 'ftw'], $collection->slice(0, 4));
  107. $this->assertEquals(['foo', 'bar', 'baz'], $collection->slice(0, 3));
  108. $this->assertEquals(['foo', 'bar'], $collection->slice(0, 2));
  109. $this->assertEquals(['foo'], $collection->slice(0, 1));
  110. $this->assertEquals([], $collection->slice(0, 0));
  111. $this->assertEquals([1 => 'bar', 2 => 'baz', 42 => 'ftw'], $collection->slice(1));
  112. $this->assertEquals([1 => 'bar', 2 => 'baz', 42 => 'ftw'], $collection->slice(1, null));
  113. $this->assertEquals([1 => 'bar', 2 => 'baz', 42 => 'ftw'], $collection->slice(1, 99));
  114. $this->assertEquals([1 => 'bar', 2 => 'baz', 42 => 'ftw'], $collection->slice(1, 3));
  115. $this->assertEquals([1 => 'bar', 2 => 'baz'], $collection->slice(1, 2));
  116. $this->assertEquals([1 => 'bar'], $collection->slice(1, 1));
  117. $this->assertEquals([], $collection->slice(1, 0));
  118. $this->assertEquals([2 => 'baz', 42 => 'ftw'], $collection->slice(2));
  119. $this->assertEquals([2 => 'baz', 42 => 'ftw'], $collection->slice(2, null));
  120. $this->assertEquals([2 => 'baz', 42 => 'ftw'], $collection->slice(2, 99));
  121. $this->assertEquals([2 => 'baz', 42 => 'ftw'], $collection->slice(2, 2));
  122. $this->assertEquals([2 => 'baz'], $collection->slice(2, 1));
  123. $this->assertEquals([], $collection->slice(2, 0));
  124. $this->assertEquals([42 => 'ftw'], $collection->slice(3));
  125. $this->assertEquals([42 => 'ftw'], $collection->slice(3, null));
  126. $this->assertEquals([42 => 'ftw'], $collection->slice(3, 99));
  127. $this->assertEquals([42 => 'ftw'], $collection->slice(3, 1));
  128. $this->assertEquals([], $collection->slice(3, 0));
  129. $this->assertEquals([], $collection->slice(4));
  130. $this->assertEquals([], $collection->slice(99));
  131. $this->assertEquals([], $collection->slice(99, 99));
  132. }
  133. public function testToArray(): void
  134. {
  135. $collection = new ArrayCollection();
  136. $this->assertEquals([], $collection->toArray());
  137. $collection = new ArrayCollection([]);
  138. $this->assertEquals([], $collection->toArray());
  139. $collection = new ArrayCollection([1]);
  140. $this->assertEquals([1], $collection->toArray());
  141. $collection = new ArrayCollection([2 => 1, 'foo']);
  142. $this->assertEquals([2 => 1, 'foo'], $collection->toArray());
  143. }
  144. }