genericMap = $this->getGenericMap(); foreach (range(0, 9) as $range) { $this->genericMap[$range] = $range; } } public function testHashMap(): void { $genericHashMap = new GenericMap(GenericMap::INT, GenericMap::INT); foreach (range(0, 9) as $range) { $genericHashMap[$range] = $range; } self::assertSame(GenericMap::INT, $this->genericMap->getKeyType()); self::assertSame(GenericMap::INT, $this->genericMap->getValueType()); self::assertCount(10, $this->genericMap); self::assertEquals(10, $this->genericMap->count()); $array = array_combine(range(0, 9), range(0, 9)); self::assertIsArray($this->genericMap->toArray()); self::assertSame($array, $this->genericMap->toArray()); self::assertSameSize($array, $this->genericMap); self::assertTrue(isset($this->genericMap[\random_int(0, 9)])); self::assertSame(0, $this->genericMap[0]); unset($this->genericMap[1]); self::assertNull($this->genericMap[1]); $this->expectException(\TypeError::class); $this->genericMap['H'] = 1; } public function testInvalidArgumentExceptionThrowsIsset(): void { $genericHashMap = new GenericMap(GenericMap::ANY, GenericMap::ANY); $this->expectException(InvalidArgumentException::class); $obj = new \stdClass(); /** @noinspection PhpExpressionResultUnusedInspection */ isset($genericHashMap[$obj]); } public function testTypeErrorThrowsIsset(): void { $genericMap = $this->getGenericMap(); $this->expectException(\TypeError::class); /** @noinspection PhpExpressionResultUnusedInspection */ isset($genericMap['C']); } public function testInvalidArgumentExceptionThrowsOnArrayOrObject(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid key type (array)'); $genericMap = new GenericMap(GenericMap::ARRAY, GenericMap::ARRAY); } public function testOffsetGetThrowsInvalidArgumentException(): void { $genericMap = $this->getGenericMap(); $this->expectException(InvalidArgumentException::class); $obj = new \stdClass(); $test = $genericMap[$obj]; } public function testOffsetSetThrowsInvalidArgumentException(): void { $genericMap = $this->getGenericMap(); $this->expectException(InvalidArgumentException::class); $obj = new \stdClass(); $genericMap[$obj] = 1; } public function testOffsetUnSetThrowsInvalidArgumentException(): void { $genericMap = $this->getGenericMap(); $this->expectException(InvalidArgumentException::class); $obj = new \stdClass(); unset($genericMap[$obj]); } public function testOffsetSetThrowsValueErrorException(): void { $genericMap = $this->getGenericMap(); $this->expectException(ValueError::class); $genericMap[0] = 'string'; } public function testOffSetUnSetThrowsTypeErrorException(): void { $this->resetGenericMap(); $this->expectException(\TypeError::class); unset($this->genericMap['C']); } public function testOffsetGetThrowsTypeErrorException(): void { $this->resetGenericMap(); $this->expectException(\TypeError::class); $test = $this->genericMap['C']; } public function testThrowInvalidArgumentExceptionOnConstructValue(): void { $this->expectException(InvalidArgumentException::class); $genericMap = new GenericMap(GenericMap::CHAR, 'test'); } public function testUsingClassAsValue(): void { $genericMap = new GenericMap(GenericMap::INT, \SplObjectStorage::class); $splObject = new \SplObjectStorage(); $stdClass = new \stdClass(); $splObject->attach($stdClass); $genericMap[0] = $splObject; self::assertSame($splObject, $genericMap[0]); self::assertTrue($genericMap[0]->contains($stdClass)); } public function testObjectCantBeUsedAsKeyWithAny(): void { $genericMap = new GenericMap(GenericMap::ANY, GenericMap::ANY); $object = new \SplObjectStorage(); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Map keys cannot be objects or arrays'); $genericMap[$object] = 1; } public function testEnumMap(): void { $enumMap = new class(TestEnum::cases()) extends GenericMap { public function __construct(array $values = []) { parent::__construct(self::INT, self::ENUM); $this->assertInitialValues($values); $this->map->putAll($values); } }; self::assertCount(8, $enumMap->toArray()); self::assertSame(TestEnum::a, $enumMap[0]); self::assertSame(TestEnum::h, $enumMap[7]); } private function resetGenericMap(): void { $this->genericMap = $this->getGenericMap(); } private function getGenericMap(): GenericMap { return new GenericMap(GenericMap::INT, GenericMap::INT); } }