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;
|
|
|
|
|
|
|
|
use Olivebbs\Map\Exception\InvalidArgumentException;
|
|
|
|
use Olivebbs\Map\GenericMap;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-03-04 13:16:36 -08:00
|
|
|
use ValueError;
|
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
|
|
|
|
{
|
|
|
|
$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();
|
2021-03-04 13:16:36 -08:00
|
|
|
$this->expectException(ValueError::class);
|
2021-03-03 13:59:55 -08:00
|
|
|
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:16:36 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-03 13:59:55 -08:00
|
|
|
private function resetGenericMap(): void
|
|
|
|
{
|
|
|
|
$this->genericMap = $this->getGenericMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getGenericMap(): GenericMap
|
|
|
|
{
|
|
|
|
return new GenericMap(GenericMap::INT, GenericMap::INT);
|
|
|
|
}
|
|
|
|
}
|