endtoend-test-psl/tests/Psl/Collection/MutableMapTest.php

162 lines
3.9 KiB
PHP
Raw Normal View History

2020-02-21 02:03:40 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Collection;
use Psl\Collection\Map;
use Psl\Collection\MutableMap;
use Psl\Collection\MutableVector;
use Psl\Exception\InvariantViolationException;
/**
* @covers \Psl\Collection\MutableMap
*/
final class MutableMapTest extends AbstractMapTest
{
/**
* @psalm-var class-string<MutableMap>
*/
protected string $mapClass = MutableMap::class;
/**
* @psalm-var class-string<MutableVector>
*/
protected string $vectorClass = MutableVector::class;
public function testClear(): void
{
$map = $this->create(['foo' => 'bar']);
2020-02-21 02:03:40 +01:00
$cleared = $map->clear();
self::assertSame($cleared, $map);
self::assertCount(0, $map);
2020-02-21 02:03:40 +01:00
}
public function testSet(): void
{
$map = $this->create([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);
$modified = $map
->set('foo', 'foo')
->set('bar', 'bar')
->set('baz', 'baz');
self::assertSame($modified, $map);
2020-02-21 02:03:40 +01:00
self::assertSame('foo', $map->at('foo'));
self::assertSame('bar', $map->at('bar'));
self::assertSame('baz', $map->at('baz'));
2020-02-21 02:03:40 +01:00
$this->expectException(InvariantViolationException::class);
2020-07-09 00:28:29 +02:00
$this->expectExceptionMessage('Key (qux) is out-of-bounds.');
2020-02-21 02:03:40 +01:00
$map->set('qux', 'qux');
}
public function testSetAll(): void
{
$map = $this->create([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);
$modified = $map->setAll(new Map([
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
]));
self::assertSame($modified, $map);
2020-02-21 02:03:40 +01:00
self::assertSame('foo', $map->at('foo'));
self::assertSame('bar', $map->at('bar'));
self::assertSame('baz', $map->at('baz'));
2020-02-21 02:03:40 +01:00
$this->expectException(InvariantViolationException::class);
2020-07-09 00:28:29 +02:00
$this->expectExceptionMessage('Key (qux) is out-of-bounds.');
2020-02-21 02:03:40 +01:00
$map->setAll(['qux' => 'qux']);
}
public function testAdd(): void
{
$map = $this->create([
'foo' => 'bar',
'bar' => 'baz',
]);
$modified = $map
->add('foo', 'foo')
->add('bar', 'bar')
->add('baz', 'baz')
->add('qux', 'qux');
self::assertSame($modified, $map);
2020-02-21 02:03:40 +01:00
self::assertSame('foo', $map->at('foo'));
self::assertSame('bar', $map->at('bar'));
self::assertSame('baz', $map->at('baz'));
self::assertSame('qux', $map->at('qux'));
2020-02-21 02:03:40 +01:00
}
public function testAddAll(): void
{
$map = $this->create([
'foo' => 'bar',
'bar' => 'baz',
]);
$modified = $map->addAll([
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
'qux' => 'qux',
]);
self::assertSame($modified, $map);
2020-02-21 02:03:40 +01:00
self::assertSame('foo', $map->at('foo'));
self::assertSame('bar', $map->at('bar'));
self::assertSame('baz', $map->at('baz'));
self::assertSame('qux', $map->at('qux'));
2020-02-21 02:03:40 +01:00
}
public function testRemove(): void
{
$map = $this->create([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);
$modified = $map
->remove('foo')
->remove('bar');
self::assertSame($modified, $map);
self::assertCount(1, $map);
self::assertSame('qux', $map->get('baz'));
self::assertNull($map->get('foo'));
self::assertNull($map->get('bar'));
2020-02-21 02:03:40 +01:00
}
2020-03-01 21:52:25 +01:00
/**
* @template Tk of array-key
* @template Tv
*
* @psalm-param iterable<Tk, Tv> $items
*
* @psalm-return MutableMap<Tk, Tv>
*/
protected function create(iterable $items): MutableMap
{
return new MutableMap($items);
}
2020-02-21 02:03:40 +01:00
}