2021-03-03 13:59:55 -08:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 https://rewiv.com sikofitt@gmail.com
|
|
|
|
*
|
|
|
|
* This file is a part of Olive BBS
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the
|
|
|
|
* terms of the Mozilla Public License, v. 2.0.
|
|
|
|
*
|
|
|
|
* If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* ___ ___ ___
|
|
|
|
* ( ).-. ( ) ( )
|
|
|
|
* .--. | |( __)___ ___ .--. | |.-. | |.-. .--.
|
|
|
|
* / \| |(''"( )( / \| / \| / \ / _ \
|
|
|
|
* | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ;
|
|
|
|
* | | | | | | | | | | | | | | | | | | | || ' | |
|
|
|
|
* | | | | | | | | | | | |/ | | | | | | |_\_`.(___)
|
|
|
|
* | | | | | | | | | | | ' _.| | | | | | ( ). '.
|
|
|
|
* | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ |
|
|
|
|
* ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' '
|
|
|
|
* `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.'
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Olivebbs\Tests\Map;
|
|
|
|
|
2022-05-11 16:28:23 -07:00
|
|
|
use Olivebbs\Map\Enum\KeyType;
|
|
|
|
use Olivebbs\Map\Enum\ValueType;
|
2021-03-03 13:59:55 -08:00
|
|
|
use Olivebbs\Map\Exception\InvalidArgumentException;
|
|
|
|
use Olivebbs\Map\GenericMap;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-05-11 16:28:23 -07:00
|
|
|
use SplObjectStorage;
|
|
|
|
use stdClass;
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
class GenericMapTest extends TestCase
|
|
|
|
{
|
|
|
|
private GenericMap $genericMap;
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->genericMap = $this->getGenericMap();
|
|
|
|
foreach (range(0, 9) as $range) {
|
|
|
|
$this->genericMap[$range] = $range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHashMap(): void
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
$genericHashMap = new GenericMap(KeyType::INT, ValueType::INT);
|
2021-03-03 13:59:55 -08:00
|
|
|
foreach (range(0, 9) as $range) {
|
|
|
|
$genericHashMap[$range] = $range;
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:28:23 -07:00
|
|
|
self::assertSame(KeyType::INT, $this->genericMap->getKeyType());
|
|
|
|
self::assertSame(ValueType::INT, $this->genericMap->getValueType());
|
2021-03-03 13:59:55 -08:00
|
|
|
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]);
|
|
|
|
|
2022-05-11 16:28:23 -07:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
$this->genericMap['H'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidArgumentExceptionThrowsIsset(): void
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
$genericHashMap = new GenericMap(KeyType::ANY, ValueType::ANY);
|
2021-03-03 13:59:55 -08:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-11 16:28:23 -07:00
|
|
|
$obj = new stdClass();
|
2021-03-03 13:59:55 -08:00
|
|
|
/** @noinspection PhpExpressionResultUnusedInspection */
|
|
|
|
isset($genericHashMap[$obj]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTypeErrorThrowsIsset(): void
|
|
|
|
{
|
|
|
|
$genericMap = $this->getGenericMap();
|
2022-05-11 16:28:23 -07:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
/** @noinspection PhpExpressionResultUnusedInspection */
|
|
|
|
isset($genericMap['C']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffsetGetThrowsInvalidArgumentException(): void
|
|
|
|
{
|
|
|
|
$genericMap = $this->getGenericMap();
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-11 16:28:23 -07:00
|
|
|
$obj = new stdClass();
|
2021-03-03 13:59:55 -08:00
|
|
|
$test = $genericMap[$obj];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffsetSetThrowsInvalidArgumentException(): void
|
|
|
|
{
|
|
|
|
$genericMap = $this->getGenericMap();
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-11 16:28:23 -07:00
|
|
|
$obj = new stdClass();
|
2021-03-03 13:59:55 -08:00
|
|
|
$genericMap[$obj] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffsetUnSetThrowsInvalidArgumentException(): void
|
|
|
|
{
|
|
|
|
$genericMap = $this->getGenericMap();
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-11 16:28:23 -07:00
|
|
|
$obj = new stdClass();
|
2021-03-03 13:59:55 -08:00
|
|
|
unset($genericMap[$obj]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffsetSetThrowsValueErrorException(): void
|
|
|
|
{
|
|
|
|
$genericMap = $this->getGenericMap();
|
2022-05-11 16:28:23 -07:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
$genericMap[0] = 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffSetUnSetThrowsTypeErrorException(): void
|
|
|
|
{
|
|
|
|
$this->resetGenericMap();
|
|
|
|
|
2022-05-11 16:28:23 -07:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
unset($this->genericMap['C']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOffsetGetThrowsTypeErrorException(): void
|
|
|
|
{
|
|
|
|
$this->resetGenericMap();
|
2022-05-11 16:28:23 -07:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
$test = $this->genericMap['C'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThrowInvalidArgumentExceptionOnConstructValue(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2022-05-11 16:28:23 -07:00
|
|
|
$genericMap = new GenericMap(KeyType::CHAR, 'test');
|
2021-03-03 13:59:55 -08:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:51:09 -08:00
|
|
|
public function testUsingClassAsValue(): void
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
$genericMap = new GenericMap(KeyType::INT, SplObjectStorage::class);
|
|
|
|
$splObject = new SplObjectStorage();
|
|
|
|
$stdClass = new stdClass();
|
2021-03-04 10:51:09 -08:00
|
|
|
$splObject->attach($stdClass);
|
|
|
|
$genericMap[0] = $splObject;
|
|
|
|
self::assertSame($splObject, $genericMap[0]);
|
|
|
|
self::assertTrue($genericMap[0]->contains($stdClass));
|
2021-03-04 11:16:57 -08:00
|
|
|
}
|
2021-03-04 10:51:09 -08:00
|
|
|
|
2021-03-04 11:16:57 -08:00
|
|
|
public function testObjectCantBeUsedAsKeyWithAny(): void
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
$genericMap = new GenericMap(KeyType::ANY, ValueType::ANY);
|
|
|
|
$object = new SplObjectStorage();
|
2021-03-04 11:16:57 -08:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('Map keys cannot be objects or arrays');
|
|
|
|
$genericMap[$object] = 1;
|
2021-03-04 10:51:09 -08:00
|
|
|
}
|
|
|
|
|
2022-05-11 13:17:06 -07:00
|
|
|
public function testEnumMap(): void
|
|
|
|
{
|
|
|
|
$enumMap = new class(TestEnum::cases()) extends GenericMap {
|
|
|
|
public function __construct(array $values = [])
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
parent::__construct(KeyType::INT, ValueType::ENUM);
|
2022-05-11 13:17:06 -07:00
|
|
|
$this->assertInitialValues($values);
|
|
|
|
$this->map->putAll($values);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self::assertCount(8, $enumMap->toArray());
|
|
|
|
self::assertSame(TestEnum::a, $enumMap[0]);
|
|
|
|
self::assertSame(TestEnum::h, $enumMap[7]);
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:59:55 -08:00
|
|
|
private function resetGenericMap(): void
|
|
|
|
{
|
|
|
|
$this->genericMap = $this->getGenericMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getGenericMap(): GenericMap
|
|
|
|
{
|
2022-05-11 16:28:23 -07:00
|
|
|
return new GenericMap(KeyType::INT, ValueType::INT);
|
2021-03-03 13:59:55 -08:00
|
|
|
}
|
|
|
|
}
|