|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LeonAero\TwigLambda\Tests; |
| 4 | + |
| 5 | + |
| 6 | +use ArrayIterator; |
| 7 | +use InvalidArgumentException; |
| 8 | +use LeonAero\TwigLambda\Dictionary; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use stdClass; |
| 11 | + |
| 12 | +class DictionaryTest extends TestCase |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * @dataProvider getOffsets |
| 17 | + */ |
| 18 | + public function testArrayAccess($offset): void |
| 19 | + { |
| 20 | + $d = new Dictionary(); |
| 21 | + self::assertFalse($d->offsetExists($offset)); |
| 22 | + |
| 23 | + $d->offsetSet($offset, 34); |
| 24 | + self::assertTrue($d->offsetExists($offset)); |
| 25 | + self::assertEquals(34, $d->offsetGet($offset)); |
| 26 | + |
| 27 | + $d->offsetUnset($offset); |
| 28 | + self::assertFalse($d->offsetExists($offset)); |
| 29 | + } |
| 30 | + |
| 31 | + public function getOffsets(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + 'int' => [12], |
| 35 | + 'object' => [new stdClass()], |
| 36 | + 'string' => ['foobar'], |
| 37 | + 'bool' => [true], |
| 38 | + 'null' => [null], |
| 39 | + 'float' => [1.4], |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Check if Dictionary recognize difference between |
| 45 | + * key types. |
| 46 | + */ |
| 47 | + public function testKeyTypeAwareness(): void |
| 48 | + { |
| 49 | + $keys = [ |
| 50 | + 1, |
| 51 | + 0, |
| 52 | + true, |
| 53 | + false, |
| 54 | + '1', |
| 55 | + '0', |
| 56 | + 1.0, |
| 57 | + 0.0, |
| 58 | + null, |
| 59 | + ]; |
| 60 | + |
| 61 | + $d = new Dictionary(); |
| 62 | + foreach ($keys as $value => $key) { |
| 63 | + $d[$key] = $value; |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($keys as $value => $key) { |
| 67 | + self::assertEquals($value, $d[$key]); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function testCount(): void |
| 72 | + { |
| 73 | + $d = new Dictionary(); |
| 74 | + |
| 75 | + self::assertEquals(0, $d->count()); |
| 76 | + |
| 77 | + $d[1] = 12; |
| 78 | + $d[true] = new stdClass(); |
| 79 | + $d[false] = new stdClass(); |
| 80 | + $d['false'] = 'abc'; |
| 81 | + $d['1'] = 'def'; |
| 82 | + $d[null] = 13; |
| 83 | + $d[new stdClass()] = 1; |
| 84 | + |
| 85 | + self::assertEquals(7, $d->count()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @dataProvider getInvalidOffsets |
| 90 | + */ |
| 91 | + public function testOffsetSet_InvalidOffset_ThrowException($offset): void |
| 92 | + { |
| 93 | + $this->expectException(InvalidArgumentException::class); |
| 94 | + $d = new Dictionary(); |
| 95 | + $d[$offset] = 1; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @dataProvider getInvalidOffsets |
| 100 | + */ |
| 101 | + public function testOffsetGet_InvalidOffset_ThrowException($offset): void |
| 102 | + { |
| 103 | + $this->expectException(InvalidArgumentException::class); |
| 104 | + $d = new Dictionary(); |
| 105 | + $d[$offset]; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @dataProvider getInvalidOffsets |
| 110 | + */ |
| 111 | + public function testOffsetExists_InvalidOffset_ThrowException($offset): void |
| 112 | + { |
| 113 | + $this->expectException(InvalidArgumentException::class); |
| 114 | + $d = new Dictionary(); |
| 115 | + isset($d[$offset]); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @dataProvider getInvalidOffsets |
| 120 | + */ |
| 121 | + public function testOffsetUnset_InvalidOffset_ThrowException($offset): void |
| 122 | + { |
| 123 | + $this->expectException(InvalidArgumentException::class); |
| 124 | + $d = new Dictionary(); |
| 125 | + unset($d[$offset]); |
| 126 | + } |
| 127 | + |
| 128 | + public function getInvalidOffsets(): array |
| 129 | + { |
| 130 | + return [ |
| 131 | + 'array' => [[1, 2, 3]], |
| 132 | + 'Closure' => [ |
| 133 | + static function () { |
| 134 | + } |
| 135 | + ], |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + public function testFromArray_CreateFromArray(): void |
| 140 | + { |
| 141 | + $array = [ |
| 142 | + 'ab' => new stdClass(), |
| 143 | + 12 => 'cd', |
| 144 | + 0 => 'ef', |
| 145 | + 'gh' => 10.2, |
| 146 | + ]; |
| 147 | + $d = Dictionary::fromArray($array); |
| 148 | + |
| 149 | + self::assertSame($array['ab'], $d['ab']); |
| 150 | + self::assertSame($array[12], $d[12]); |
| 151 | + self::assertSame($array[0], $d[0]); |
| 152 | + self::assertSame($array['gh'], $d['gh']); |
| 153 | + self::assertEquals(4, $d->count()); |
| 154 | + } |
| 155 | + |
| 156 | + public function testFromArray_CreateFromTraversable(): void |
| 157 | + { |
| 158 | + $array = [ |
| 159 | + 'ab' => new stdClass(), |
| 160 | + 12 => 'cd', |
| 161 | + 0 => 'ef', |
| 162 | + 'gh' => 10.2, |
| 163 | + ]; |
| 164 | + $d = Dictionary::fromArray(new ArrayIterator($array)); |
| 165 | + |
| 166 | + self::assertSame($array['ab'], $d['ab']); |
| 167 | + self::assertSame($array[12], $d[12]); |
| 168 | + self::assertSame($array[0], $d[0]); |
| 169 | + self::assertSame($array['gh'], $d['gh']); |
| 170 | + self::assertEquals(4, $d->count()); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @dataProvider getInvalidArrays |
| 175 | + */ |
| 176 | + public function testFromArray_InvalidArray_ThrowException($array): void |
| 177 | + { |
| 178 | + $this->expectException(InvalidArgumentException::class); |
| 179 | + Dictionary::fromArray($array); |
| 180 | + } |
| 181 | + |
| 182 | + public function getInvalidArrays(): array |
| 183 | + { |
| 184 | + return [ |
| 185 | + 'int' => [12], |
| 186 | + 'float' => [1.2], |
| 187 | + 'string' => ['abcdef'], |
| 188 | + 'object' => [new stdClass()], |
| 189 | + 'null' => [null], |
| 190 | + 'false' => [false], |
| 191 | + 'true' => [true], |
| 192 | + ]; |
| 193 | + } |
| 194 | + |
| 195 | + public function testUnserialize(): void |
| 196 | + { |
| 197 | + $serialized = 'C:30:"LeonAero\TwigLambda\Dictionary":203:{a:5:{i:0;a:2:{i:0;O:8:"stdClass":0:{}i:1;i:12;}i:1;a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}i:2;a:2:{i:0;O:8:"stdClass":0:{}i:1;s:3:"tet";}i:3;a:2:{i:0;i:12;i:1;O:8:"stdClass":0:{}}i:4;a:2:{i:0;i:15;i:1;r:14;}}}'; |
| 198 | + /* @var $d Dictionary */ |
| 199 | + $d = unserialize($serialized); |
| 200 | + |
| 201 | + self::assertEquals('bar', $d['foo']); |
| 202 | + self::assertSame($d[12], $d[15]); |
| 203 | + self::assertCount(5, $d); |
| 204 | + |
| 205 | + $it = $d->getIterator(); |
| 206 | + |
| 207 | + $it->rewind(); |
| 208 | + $obj1 = $it->key(); |
| 209 | + self::assertEquals(12, $it->current()); |
| 210 | + $it->next(); |
| 211 | + $it->next(); |
| 212 | + $obj2 = $it->key(); |
| 213 | + self::assertEquals('tet', $it->current()); |
| 214 | + self::assertNotSame($obj1, $obj2); |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments