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