|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Laudis Neo4j package. |
| 5 | + * |
| 6 | + * (c) Laudis technologies <http://laudis.tech> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Laudis\Neo4j\Tests\Unit; |
| 13 | + |
| 14 | +use ArrayIterator; |
| 15 | +use BadMethodCallException; |
| 16 | +use function hexdec; |
| 17 | +use Laudis\Neo4j\Types\CypherList; |
| 18 | +use OutOfBoundsException; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use stdClass; |
| 21 | + |
| 22 | +final class CypherListTest extends TestCase |
| 23 | +{ |
| 24 | + private CypherList $list; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + |
| 30 | + $this->list = new CypherList(['A', 'B', 'C']); |
| 31 | + } |
| 32 | + |
| 33 | + public function testFromIterableEqual(): void |
| 34 | + { |
| 35 | + $fromIterable = CypherList::fromIterable($this->list); |
| 36 | + |
| 37 | + self::assertNotSame($this->list, $fromIterable); |
| 38 | + self::assertEquals($this->list, $fromIterable); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFromIterableArray(): void |
| 42 | + { |
| 43 | + $fromIterable = CypherList::fromIterable(['A', 'B', 'C']); |
| 44 | + |
| 45 | + self::assertNotSame($this->list, $fromIterable); |
| 46 | + self::assertEquals($this->list, $fromIterable); |
| 47 | + } |
| 48 | + |
| 49 | + public function testFromIterable(): void |
| 50 | + { |
| 51 | + $fromIterable = CypherList::fromIterable(new ArrayIterator(['A', 'B', 'C'])); |
| 52 | + |
| 53 | + self::assertNotSame($this->list, $fromIterable); |
| 54 | + self::assertEquals($this->list, $fromIterable); |
| 55 | + } |
| 56 | + |
| 57 | + public function testCount(): void |
| 58 | + { |
| 59 | + self::assertCount(3, $this->list); |
| 60 | + } |
| 61 | + |
| 62 | + public function testCountEmpty(): void |
| 63 | + { |
| 64 | + self::assertCount(0, new CypherList()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testCopy(): void |
| 68 | + { |
| 69 | + $copy = $this->list->copy(); |
| 70 | + |
| 71 | + self::assertNotSame($this->list, $copy); |
| 72 | + self::assertEquals($this->list, $copy); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCopyDepth(): void |
| 76 | + { |
| 77 | + $list = new CypherList([new stdClass()]); |
| 78 | + $copy = $list->copy(); |
| 79 | + |
| 80 | + self::assertNotSame($list, $copy); |
| 81 | + self::assertEquals($list, $copy); |
| 82 | + self::assertSame($list[0], $copy[0]); |
| 83 | + } |
| 84 | + |
| 85 | + public function testIsEmpty(): void |
| 86 | + { |
| 87 | + self::assertFalse($this->list->isEmpty()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testIsEmptyEmpty(): void |
| 91 | + { |
| 92 | + self::assertTrue((new CypherList())->isEmpty()); |
| 93 | + } |
| 94 | + |
| 95 | + public function testToArray(): void |
| 96 | + { |
| 97 | + self::assertEquals(['A', 'B', 'C'], $this->list->toArray()); |
| 98 | + } |
| 99 | + |
| 100 | + public function testMerge(): void |
| 101 | + { |
| 102 | + self::assertEquals(new CypherList(['A', 'B', 'C', 'A', 'B', 'C']), $this->list->merge($this->list)); |
| 103 | + } |
| 104 | + |
| 105 | + public function testHasKey(): void |
| 106 | + { |
| 107 | + self::assertFalse($this->list->hasKey(-1)); |
| 108 | + self::assertTrue($this->list->hasKey(0)); |
| 109 | + self::assertTrue($this->list->hasKey(1)); |
| 110 | + self::assertTrue($this->list->hasKey(2)); |
| 111 | + self::assertFalse($this->list->hasKey(3)); |
| 112 | + } |
| 113 | + |
| 114 | + public function testFilterPermissive(): void |
| 115 | + { |
| 116 | + $filter = $this->list->filter(static fn () => true); |
| 117 | + |
| 118 | + self::assertEquals($this->list, $filter); |
| 119 | + self::assertNotSame($this->list, $filter); |
| 120 | + } |
| 121 | + |
| 122 | + public function testFilterBlock(): void |
| 123 | + { |
| 124 | + $filter = $this->list->filter(static fn () => false); |
| 125 | + |
| 126 | + self::assertEquals(new CypherList(), $filter); |
| 127 | + } |
| 128 | + |
| 129 | + public function testFilterSelective(): void |
| 130 | + { |
| 131 | + $filter = $this->list->filter(static fn (int $i, string $x) => $x === 'B' || $i === 2); |
| 132 | + |
| 133 | + self::assertEquals(new CypherList(['B', 'C']), $filter); |
| 134 | + } |
| 135 | + |
| 136 | + public function testMap(): void |
| 137 | + { |
| 138 | + $filter = $this->list->map(static fn (int $i, string $x) => $i.':'.$x); |
| 139 | + |
| 140 | + self::assertEquals(new CypherList(['0:A', '1:B', '2:C']), $filter); |
| 141 | + } |
| 142 | + |
| 143 | + public function testReduce(): void |
| 144 | + { |
| 145 | + $count = $this->list->reduce(static function (?int $initial, int $key, string $value) { |
| 146 | + return ($initial ?? 0) + $key * hexdec($value); |
| 147 | + }, 5); |
| 148 | + |
| 149 | + self::assertEquals(5 + hexdec('B') + 2 * hexdec('C'), $count); |
| 150 | + } |
| 151 | + |
| 152 | + public function testFind(): void |
| 153 | + { |
| 154 | + self::assertFalse($this->list->find('X')); |
| 155 | + self::assertEquals(0, $this->list->find('A')); |
| 156 | + self::assertEquals(1, $this->list->find('B')); |
| 157 | + self::assertEquals(2, $this->list->find('C')); |
| 158 | + } |
| 159 | + |
| 160 | + public function testReversed(): void |
| 161 | + { |
| 162 | + self::assertEquals(new CypherList(['C', 'B', 'A']), $this->list->reversed()); |
| 163 | + self::assertEquals(new CypherList(['A', 'B', 'C']), $this->list); |
| 164 | + self::assertEquals(new CypherList(['A', 'B', 'C']), $this->list->reversed()->reversed()); |
| 165 | + } |
| 166 | + |
| 167 | + public function testSliceSingle(): void |
| 168 | + { |
| 169 | + $sliced = $this->list->slice(1, 1); |
| 170 | + self::assertEquals(new CypherList(['B']), $sliced); |
| 171 | + } |
| 172 | + |
| 173 | + public function testSliceDouble(): void |
| 174 | + { |
| 175 | + $sliced = $this->list->slice(1, 2); |
| 176 | + self::assertEquals(new CypherList(['B', 'C']), $sliced); |
| 177 | + } |
| 178 | + |
| 179 | + public function testSliceAll(): void |
| 180 | + { |
| 181 | + $sliced = $this->list->slice(0, 3); |
| 182 | + self::assertEquals(new CypherList(['A', 'B', 'C']), $sliced); |
| 183 | + } |
| 184 | + |
| 185 | + public function testSliceTooMuch(): void |
| 186 | + { |
| 187 | + $sliced = $this->list->slice(0, 5); |
| 188 | + self::assertEquals(new CypherList(['A', 'B', 'C']), $sliced); |
| 189 | + } |
| 190 | + |
| 191 | + public function testSliceEmpty(): void |
| 192 | + { |
| 193 | + $sliced = $this->list->slice(0, 0); |
| 194 | + self::assertEquals(new CypherList(), $sliced); |
| 195 | + } |
| 196 | + |
| 197 | + public function testGetValid(): void |
| 198 | + { |
| 199 | + self::assertEquals('A', $this->list->get(0)); |
| 200 | + self::assertEquals('B', $this->list->get(1)); |
| 201 | + self::assertEquals('C', $this->list->get(2)); |
| 202 | + } |
| 203 | + |
| 204 | + public function testFirst(): void |
| 205 | + { |
| 206 | + self::assertEquals('A', $this->list->first()); |
| 207 | + } |
| 208 | + |
| 209 | + public function testFirstInvalid(): void |
| 210 | + { |
| 211 | + $this->expectException(OutOfBoundsException::class); |
| 212 | + $this->expectExceptionMessage('Cannot grab first element of an empty list'); |
| 213 | + (new CypherList())->first(); |
| 214 | + } |
| 215 | + |
| 216 | + public function testLast(): void |
| 217 | + { |
| 218 | + self::assertEquals('C', $this->list->last()); |
| 219 | + } |
| 220 | + |
| 221 | + public function testLastInvalid(): void |
| 222 | + { |
| 223 | + $this->expectException(OutOfBoundsException::class); |
| 224 | + $this->expectExceptionMessage('Cannot grab last element of an empty list'); |
| 225 | + (new CypherList())->last(); |
| 226 | + } |
| 227 | + |
| 228 | + public function testGetInvalid(): void |
| 229 | + { |
| 230 | + $this->expectException(OutOfBoundsException::class); |
| 231 | + $this->expectExceptionMessage('Cannot get item in sequence at position: 3'); |
| 232 | + $this->list->get(3); |
| 233 | + } |
| 234 | + |
| 235 | + public function testGetNegative(): void |
| 236 | + { |
| 237 | + $this->expectException(OutOfBoundsException::class); |
| 238 | + $this->expectExceptionMessage('Cannot get item in sequence at position: -1'); |
| 239 | + $this->list->get(-1); |
| 240 | + } |
| 241 | + |
| 242 | + public function testIteration(): void |
| 243 | + { |
| 244 | + $counter = 0; |
| 245 | + foreach ($this->list as $key => $item) { |
| 246 | + ++$counter; |
| 247 | + self::assertEquals(['A', 'B', 'C'][$key], $item); |
| 248 | + } |
| 249 | + self::assertEquals(3, $counter); |
| 250 | + } |
| 251 | + |
| 252 | + public function testIterationEmpty(): void |
| 253 | + { |
| 254 | + $counter = 0; |
| 255 | + foreach ((new CypherList()) as $key => $item) { |
| 256 | + ++$counter; |
| 257 | + self::assertEquals(['A', 'B', 'C'][$key], $item); |
| 258 | + } |
| 259 | + self::assertEquals(0, $counter); |
| 260 | + } |
| 261 | + |
| 262 | + public function testOffsetSet(): void |
| 263 | + { |
| 264 | + $this->expectException(BadMethodCallException::class); |
| 265 | + $this->expectExceptionMessage('Laudis\Neo4j\Types\CypherList is immutable'); |
| 266 | + |
| 267 | + $this->list[0] = 'a'; |
| 268 | + } |
| 269 | + |
| 270 | + public function testOffsetUnset(): void |
| 271 | + { |
| 272 | + $this->expectException(BadMethodCallException::class); |
| 273 | + $this->expectExceptionMessage('Laudis\Neo4j\Types\CypherList is immutable'); |
| 274 | + |
| 275 | + unset($this->list[0]); |
| 276 | + } |
| 277 | + |
| 278 | + public function testOffsetGetValid(): void |
| 279 | + { |
| 280 | + self::assertEquals('A', $this->list[0]); |
| 281 | + self::assertEquals('B', $this->list[1]); |
| 282 | + self::assertEquals('C', $this->list[2]); |
| 283 | + } |
| 284 | + |
| 285 | + public function testOffsetGetInvalid(): void |
| 286 | + { |
| 287 | + $this->expectException(OutOfBoundsException::class); |
| 288 | + $this->expectExceptionMessage('Offset: "3" does not exists in object of instance: Laudis\Neo4j\Types\CypherList'); |
| 289 | + $this->list[3]; |
| 290 | + } |
| 291 | + |
| 292 | + public function testOffsetGetNegative(): void |
| 293 | + { |
| 294 | + $this->expectException(OutOfBoundsException::class); |
| 295 | + $this->expectExceptionMessage('Offset: "-1" does not exists in object of instance: Laudis\Neo4j\Types\CypherList'); |
| 296 | + $this->list[-1]; |
| 297 | + } |
| 298 | + |
| 299 | + public function testIssetValid(): void |
| 300 | + { |
| 301 | + self::assertTrue(isset($this->list[0])); |
| 302 | + self::assertTrue(isset($this->list[1])); |
| 303 | + self::assertTrue(isset($this->list[2])); |
| 304 | + } |
| 305 | + |
| 306 | + public function testIssetInValid(): void |
| 307 | + { |
| 308 | + self::assertFalse(isset($this->list[-1])); |
| 309 | + self::assertFalse(isset($this->list[3])); |
| 310 | + } |
| 311 | + |
| 312 | + public function testIssetValidNull(): void |
| 313 | + { |
| 314 | + self::assertTrue(isset((new CypherList([null]))[0])); |
| 315 | + } |
| 316 | +} |
0 commit comments