1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<?php
declare(strict_types=1);
namespace Ramsey\Collection\Test\Map;
use ArrayIterator;
use Ramsey\Collection\ArrayInterface;
use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Map\AssociativeArrayMap;
use Ramsey\Collection\Test\Mock\Foo;
use Ramsey\Collection\Test\TestCase;
use function serialize;
use function unserialize;
/**
* Tests for AssociativeArrayMap, as well as coverage for AbstractMap
*/
class AssociativeArrayMapTest extends TestCase
{
public function testOffsetSetWithEmptyOffsetThrowsException(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Map elements are key/value pairs; a key must be provided for value 123');
/**
* @phpstan-ignore-next-line
* @psalm-suppress NullArgument
*/
$associativeArrayMapObject[] = 123;
}
public function testOffsetSetWithValidKey(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$associativeArrayMapObject['foo'] = 123;
$this->assertSame(123, $associativeArrayMapObject['foo']);
}
public function testContainsKey(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$associativeArrayMapObject['foo'] = null;
$associativeArrayMapObject['bar'] = 123;
$this->assertFalse(isset($associativeArrayMapObject['foo']));
$this->assertTrue($associativeArrayMapObject->containsKey('foo'));
$this->assertTrue($associativeArrayMapObject->containsKey('bar'));
$this->assertFalse($associativeArrayMapObject->containsKey('baz'));
}
public function testKeys(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
// empty map returns empty array
$this->assertSame([], $associativeArrayMapObject->keys());
$associativeArrayMapObject['foo'] = null;
$associativeArrayMapObject['bar'] = 321;
// array with key-value entries return array containing keys
$this->assertSame(['foo', 'bar'], $associativeArrayMapObject->keys());
}
public function testContainsValue(): void
{
$foo = new Foo();
$associativeArrayMapObject = new AssociativeArrayMap();
$associativeArrayMapObject['foo'] = null;
$associativeArrayMapObject['bar'] = 123;
$associativeArrayMapObject['baz'] = $foo;
$this->assertTrue($associativeArrayMapObject->containsValue(null));
$this->assertTrue($associativeArrayMapObject->containsValue(123));
$this->assertTrue($associativeArrayMapObject->containsValue($foo));
$this->assertFalse($associativeArrayMapObject->containsValue(new Foo()));
}
public function testGet(): void
{
$data = ['foo' => 123];
$associativeArrayMapObject = new AssociativeArrayMap($data);
$this->assertSame($data['foo'], $associativeArrayMapObject->get('foo'));
$this->assertNull($associativeArrayMapObject->get('bar', null));
}
public function testPut(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$previousValue = $associativeArrayMapObject->put('foo', 123);
$this->assertNull($previousValue);
/** @var int $previousValue */
$previousValue = $associativeArrayMapObject->put('foo', 456);
$this->assertSame(123, $previousValue);
// Ensure the value changed
$this->assertSame(456, $associativeArrayMapObject->get('foo'));
}
public function testPutIfAbsent(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$currentValue = $associativeArrayMapObject->putIfAbsent('foo', 123);
$this->assertNull($currentValue);
/** @var int $currentValue */
$currentValue = $associativeArrayMapObject->putIfAbsent('foo', 456);
$this->assertSame(123, $currentValue);
// Ensure the value does not change
$this->assertSame(123, $associativeArrayMapObject->get('foo'));
}
public function testRemove(): void
{
$data = ['foo' => 123];
$associativeArrayMapObject = new AssociativeArrayMap($data);
$previousValue = $associativeArrayMapObject->remove('foo');
$this->assertSame($data['foo'], $previousValue);
/** @var mixed $previousValue */
$previousValue = $associativeArrayMapObject->remove('foo');
$this->assertNull($previousValue);
$this->assertFalse($associativeArrayMapObject->containsKey('foo'));
}
public function testRemoveIf(): void
{
$data = ['foo' => 123];
$associativeArrayMapObject = new AssociativeArrayMap($data);
$this->assertFalse($associativeArrayMapObject->removeIf('foo', 456));
$this->assertSame($data['foo'], $associativeArrayMapObject->get('foo'));
$this->assertTrue($associativeArrayMapObject->removeIf('foo', 123));
$this->assertFalse($associativeArrayMapObject->containsKey('foo'));
}
public function testReplace(): void
{
$data = ['foo' => 123];
$associativeArrayMapObject = new AssociativeArrayMap($data);
$previousValue = $associativeArrayMapObject->replace('foo', 456);
$this->assertSame($data['foo'], $previousValue);
$this->assertSame(456, $associativeArrayMapObject->get('foo'));
/** @var mixed $previousValue */
$previousValue = $associativeArrayMapObject->replace('bar', 789);
$this->assertNull($previousValue);
$this->assertFalse($associativeArrayMapObject->containsKey('bar'));
}
public function testReplaceIf(): void
{
$data = ['foo' => 123];
$associativeArrayMapObject = new AssociativeArrayMap($data);
$this->assertFalse($associativeArrayMapObject->replaceIf('foo', 456, 789));
$this->assertSame($data['foo'], $associativeArrayMapObject->get('foo'));
$this->assertTrue($associativeArrayMapObject->replaceIf('foo', 123, 987));
$this->assertSame(987, $associativeArrayMapObject->get('foo'));
}
public function testGetIterator(): void
{
$associativeArrayMapObject = new AssociativeArrayMap();
$this->assertInstanceOf(ArrayIterator::class, $associativeArrayMapObject->getIterator());
}
public function testSerializable(): void
{
$phpArray = ['foo' => 123, 'bar' => 456];
$associativeArrayMapObject = new AssociativeArrayMap($phpArray);
$associativeArrayMapObjectSerialized = serialize($associativeArrayMapObject);
$associativeArrayMapObject2 = unserialize($associativeArrayMapObjectSerialized);
$this->assertInstanceOf(ArrayInterface::class, $associativeArrayMapObject2);
$this->assertEquals($associativeArrayMapObject, $associativeArrayMapObject2);
}
public function testToArray(): void
{
$phpArray = ['foo' => 123, 'bar' => 456];
$associativeArrayMapObject = new AssociativeArrayMap($phpArray);
$this->assertSame($phpArray, $associativeArrayMapObject->toArray());
}
}
|