diff --git a/composer.json b/composer.json index fec6cde..f0c4a7f 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "php": "^8.1" }, "require-dev": { - "phpunit/phpunit": "9.5.*", + "phpunit/phpunit": "10.0.*", "steevanb/php-backtrace": "2.1.*", "symfony/config": "6.1.*", "symfony/dependency-injection": "6.1.*", diff --git a/config/ci/phpunit.php-8.1.xml b/config/ci/phpunit.php-8.1.xml index d16f6a2..2f52dc0 100644 --- a/config/ci/phpunit.php-8.1.xml +++ b/config/ci/phpunit.php-8.1.xml @@ -1,17 +1,21 @@ + + ../../tests - + + ../../bridge ../../src diff --git a/config/ci/phpunit.php-8.2.xml b/config/ci/phpunit.php-8.2.xml index a5e6db9..6151a0b 100644 --- a/config/ci/phpunit.php-8.2.xml +++ b/config/ci/phpunit.php-8.2.xml @@ -1,6 +1,12 @@ + diff --git a/src/AbstractCollection.php b/src/AbstractCollection.php index 0fac23a..07f4aca 100644 --- a/src/AbstractCollection.php +++ b/src/AbstractCollection.php @@ -36,11 +36,9 @@ public function hasKey(string|int $key): bool public function remove(string|int $key): static { - $this->assertIsNotReadOnly(); - - if ($this->hasKey($key) === false) { - throw new KeyNotFoundException('Key "' . $key . '" not found.'); - } + $this + ->assertIsNotReadOnly() + ->assertHasKey($key); unset($this->values[$key]); @@ -155,12 +153,19 @@ protected function doReplace(iterable $values): static } protected function doGet(string|int $key): mixed + { + $this->assertHasKey($key); + + return $this->values[$key]; + } + + protected function assertHasKey(string|int $key): static { if ($this->hasKey($key) === false) { - throw new KeyNotFoundException('Key "' . $key . '" not found.'); + throw new KeyNotFoundException($key); } - return $this->values[$key]; + return $this; } protected function doHas(mixed $value): bool diff --git a/src/Exception/KeyNotFoundException.php b/src/Exception/KeyNotFoundException.php index f5f56d7..10547fc 100644 --- a/src/Exception/KeyNotFoundException.php +++ b/src/Exception/KeyNotFoundException.php @@ -6,4 +6,8 @@ class KeyNotFoundException extends PhpCollectionException { + public function __construct(string|int $key) + { + parent::__construct('Key "' . $key . '" not found.'); + } } diff --git a/tests/Unit/AbstractCollection/ChangeKeyCaseTest.php b/tests/Unit/AbstractCollection/ChangeKeyCaseTest.php index 9b4beae..dbe3b91 100644 --- a/tests/Unit/AbstractCollection/ChangeKeyCaseTest.php +++ b/tests/Unit/AbstractCollection/ChangeKeyCaseTest.php @@ -7,11 +7,12 @@ use PHPUnit\Framework\TestCase; use Steevanb\PhpCollection\KeyCaseEnum; +/** @covers \Steevanb\PhpCollection\AbstractCollection::changeKeyCase */ final class ChangeKeyCaseTest extends TestCase { public function testDefaultParameters(): void { - $collection = new Collection(['foo' => 1]); + $collection = new TestCollection(['foo' => 1]); $collection->changeKeyCase(); @@ -22,7 +23,7 @@ public function testDefaultParameters(): void public function testLowerCaseAssociativesKeys(): void { - $collection = new Collection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]); + $collection = new TestCollection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]); $collection->changeKeyCase(KeyCaseEnum::LOWER); @@ -37,7 +38,7 @@ public function testLowerCaseAssociativesKeys(): void public function testUpperCaseAssociativesKeys(): void { - $collection = new Collection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]); + $collection = new TestCollection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]); $collection->changeKeyCase(KeyCaseEnum::UPPER); @@ -52,7 +53,7 @@ public function testUpperCaseAssociativesKeys(): void public function testLowerCaseIndexedKeys(): void { - $collection = new Collection([0 => 1, 10 => 2, 20 => 3]); + $collection = new TestCollection([0 => 1, 10 => 2, 20 => 3]); $collection->changeKeyCase(KeyCaseEnum::LOWER); @@ -67,7 +68,7 @@ public function testLowerCaseIndexedKeys(): void public function testUpperCaseIndexedKeys(): void { - $collection = new Collection([0 => 1, 10 => 2, 20 => 3]); + $collection = new TestCollection([0 => 1, 10 => 2, 20 => 3]); $collection->changeKeyCase(KeyCaseEnum::UPPER); @@ -82,7 +83,7 @@ public function testUpperCaseIndexedKeys(): void public function testEmpty(): void { - $collection = new Collection(); + $collection = new TestCollection(); static::assertCount(0, $collection); @@ -90,11 +91,4 @@ public function testEmpty(): void static::assertCount(0, $collection); } - - public function testReturnType(): void - { - $collection = new Collection(); - - static::assertSame($collection, $collection->changeKeyCase()); - } } diff --git a/tests/Unit/AbstractCollection/ClearTest.php b/tests/Unit/AbstractCollection/ClearTest.php index 7f5efac..5a426ae 100644 --- a/tests/Unit/AbstractCollection/ClearTest.php +++ b/tests/Unit/AbstractCollection/ClearTest.php @@ -6,11 +6,12 @@ use PHPUnit\Framework\TestCase; +/** @covers \Steevanb\PhpCollection\AbstractCollection::clear */ final class ClearTest extends TestCase { public function testEmpty(): void { - $collection = new Collection(); + $collection = new TestCollection(); static::assertCount(0, $collection); @@ -25,7 +26,7 @@ public function testEmpty(): void public function testOneItem(): void { - $collection = new Collection([1]); + $collection = new TestCollection([1]); static::assertCount(1, $collection); @@ -40,7 +41,7 @@ public function testOneItem(): void public function testThreeItem(): void { - $collection = new Collection([1, 2, 3]); + $collection = new TestCollection([1, 2, 3]); static::assertCount(3, $collection); diff --git a/tests/Unit/AbstractCollection/CountableTest.php b/tests/Unit/AbstractCollection/CountableTest.php index 85c652d..225f379 100644 --- a/tests/Unit/AbstractCollection/CountableTest.php +++ b/tests/Unit/AbstractCollection/CountableTest.php @@ -6,10 +6,11 @@ use PHPUnit\Framework\TestCase; +/** @coversNothing */ final class CountableTest extends TestCase { public function testCount(): void { - static::assertCount(3, new Collection([1, '2', null])); + static::assertCount(3, new TestCollection([1, '2', null])); } } diff --git a/tests/Unit/AbstractCollection/DoAddTest.php b/tests/Unit/AbstractCollection/DoAddTest.php new file mode 100644 index 0000000..580f52a --- /dev/null +++ b/tests/Unit/AbstractCollection/DoAddTest.php @@ -0,0 +1,74 @@ +callDoAdd('foo'); + + static::assertSame('foo', $collection->callDoGet(0)); + } + + public function testTreeValue(): void + { + $collection = (new TestCollection()) + ->callDoAdd('foo') + ->callDoAdd('bar') + ->callDoAdd('baz'); + + static::assertSame('foo', $collection->callDoGet(0)); + static::assertSame('bar', $collection->callDoGet(1)); + static::assertSame('baz', $collection->callDoGet(2)); + } + + public function testReadOnly(): void + { + $collection = (new TestCollection([1, 2]))->setReadOnly(); + + $this->expectException(ReadOnlyException::class); + $this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.'); + $this->expectExceptionCode(0); + $collection->callDoAdd(3); + } + + public function testValueAlreadyExistsDoNotAdd(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::DO_NOT_ADD); + $collection + ->callDoAdd(10) + ->callDoAdd(11) + ->callDoAdd(11) + ->callDoAdd(13); + + static::assertCount(3, $collection); + static::assertSame(10, $collection->callDoGet(0)); + static::assertSame(11, $collection->callDoGet(1)); + static::assertSame(13, $collection->callDoGet(2)); + } + + public function testException(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::EXCEPTION); + $collection + ->callDoAdd(10) + ->callDoAdd(11); + + $this->expectException(ValueAlreadyExistsException::class); + $this->expectExceptionMessage('Value "11" already exists.'); + $this->expectExceptionCode(0); + $collection->callDoAdd(11); + } +} diff --git a/tests/Unit/AbstractCollection/DoGetTest.php b/tests/Unit/AbstractCollection/DoGetTest.php index 1c60b1f..c061323 100644 --- a/tests/Unit/AbstractCollection/DoGetTest.php +++ b/tests/Unit/AbstractCollection/DoGetTest.php @@ -7,11 +7,12 @@ use PHPUnit\Framework\TestCase; use Steevanb\PhpCollection\Exception\KeyNotFoundException; +/** @covers \Steevanb\PhpCollection\AbstractCollection::doGet */ final class DoGetTest extends TestCase { public function testGet(): void { - $collection = new Collection([1, '2', null]); + $collection = new TestCollection([1, '2', null]); static::assertSame(1, $collection->callDoGet(0)); static::assertSame('2', $collection->callDoGet(1)); @@ -20,9 +21,11 @@ public function testGet(): void public function testKeyNotFound(): void { - $collection = new Collection([1, '2', null]); + $collection = new TestCollection([1, '2', null]); - static::expectException(KeyNotFoundException::class); - static::assertFalse($collection->callDoGet(3)); + $this->expectException(KeyNotFoundException::class); + $this->expectExceptionMessage('Key "3" not found.'); + $this->expectExceptionCode(0); + $collection->callDoGet(3); } } diff --git a/tests/Unit/AbstractCollection/DoReplaceTest.php b/tests/Unit/AbstractCollection/DoReplaceTest.php index 7150865..4079f2d 100644 --- a/tests/Unit/AbstractCollection/DoReplaceTest.php +++ b/tests/Unit/AbstractCollection/DoReplaceTest.php @@ -5,12 +5,19 @@ namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection; use PHPUnit\Framework\TestCase; +use Steevanb\PhpCollection\{ + Exception\ReadOnlyException, + Exception\ValueAlreadyExistsException, + ValueAlreadyExistsModeEnum +}; +/** @covers \Steevanb\PhpCollection\AbstractCollection::doReplace */ final class DoReplaceTest extends TestCase { - public function testConstructorValues(): void + public function testDoReplace(): void { - $collection = new Collection([10, 11, 12, 13]); + $collection = (new TestCollection()) + ->callDoReplace([10, 11, 12, 13]); static::assertCount(4, $collection); static::assertSame(10, $collection->callDoGet(0)); @@ -19,10 +26,9 @@ public function testConstructorValues(): void static::assertSame(13, $collection->callDoGet(3)); } - public function testReplace(): void + public function testCalledFromConstructor(): void { - $collection = (new Collection()) - ->callDoReplace([10, 11, 12, 13]); + $collection = new TestCollection([10, 11, 12, 13]); static::assertCount(4, $collection); static::assertSame(10, $collection->callDoGet(0)); @@ -30,4 +36,35 @@ public function testReplace(): void static::assertSame(12, $collection->callDoGet(2)); static::assertSame(13, $collection->callDoGet(3)); } + + public function testReadOnly(): void + { + $collection = (new TestCollection([1, 2]))->setReadOnly(); + + $this->expectException(ReadOnlyException::class); + $this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.'); + $this->expectExceptionCode(0); + $collection->callDoReplace([3, 4]); + } + + public function testValueAlreadyExistsDoNotAdd(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::DO_NOT_ADD); + $collection->callDoReplace([10, 11, 11, 13]); + + static::assertCount(3, $collection); + static::assertSame(10, $collection->callDoGet(0)); + static::assertSame(11, $collection->callDoGet(1)); + static::assertSame(13, $collection->callDoGet(3)); + } + + public function testException(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::EXCEPTION); + + $this->expectException(ValueAlreadyExistsException::class); + $this->expectExceptionMessage('Value "11" already exists.'); + $this->expectExceptionCode(0); + $collection->callDoReplace([10, 11, 11]); + } } diff --git a/tests/Unit/AbstractCollection/DoSetTest.php b/tests/Unit/AbstractCollection/DoSetTest.php index cc862a4..85d5aae 100644 --- a/tests/Unit/AbstractCollection/DoSetTest.php +++ b/tests/Unit/AbstractCollection/DoSetTest.php @@ -5,12 +5,18 @@ namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection; use PHPUnit\Framework\TestCase; +use Steevanb\PhpCollection\{ + Exception\ReadOnlyException, + Exception\ValueAlreadyExistsException, + ValueAlreadyExistsModeEnum +}; +/** @covers \Steevanb\PhpCollection\AbstractCollection::doSet */ final class DoSetTest extends TestCase { - public function testSetIntegerKeys(): void + public function testIntegerKeys(): void { - $collection = new Collection(); + $collection = new TestCollection(); $collection->callDoSet(0, 1); $collection->callDoSet(1, '2'); $collection->callDoSet(2, null); @@ -20,9 +26,9 @@ public function testSetIntegerKeys(): void static::assertNull($collection->callDoGet(2)); } - public function testSetStringKeys(): void + public function testStringKeys(): void { - $collection = new Collection(); + $collection = new TestCollection(); $collection->callDoSet('foo', 1); $collection->callDoSet('bar', '2'); $collection->callDoSet('baz', null); @@ -31,4 +37,54 @@ public function testSetStringKeys(): void static::assertSame('2', $collection->callDoGet('bar')); static::assertNull($collection->callDoGet('baz')); } + + public function testStringAndIntKeys(): void + { + $collection = new TestCollection(); + $collection->callDoSet('foo', 1); + $collection->callDoSet(0, '2'); + $collection->callDoSet('baz', null); + + static::assertSame(1, $collection->callDoGet('foo')); + static::assertSame('2', $collection->callDoGet(0)); + static::assertNull($collection->callDoGet('baz')); + } + + public function testReadOnly(): void + { + $collection = (new TestCollection([1, 2]))->setReadOnly(); + + $this->expectException(ReadOnlyException::class); + $this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.'); + $this->expectExceptionCode(0); + $collection->callDoSet(0, 4); + } + + public function testValueAlreadyExistsDoNotAdd(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::DO_NOT_ADD); + $collection + ->callDoSet(0, 10) + ->callDoSet(1, 11) + ->callDoSet(2, 11) + ->callDoSet(3, 13); + + static::assertCount(3, $collection); + static::assertSame(10, $collection->callDoGet(0)); + static::assertSame(11, $collection->callDoGet(1)); + static::assertSame(13, $collection->callDoGet(3)); + } + + public function testException(): void + { + $collection = new TestCollection([], ValueAlreadyExistsModeEnum::EXCEPTION); + $collection + ->callDoSet(0, 10) + ->callDoSet(1, 11); + + $this->expectException(ValueAlreadyExistsException::class); + $this->expectExceptionMessage('Value "11" already exists.'); + $this->expectExceptionCode(0); + $collection->callDoSet(2, 11); + } } diff --git a/tests/Unit/AbstractCollection/HasKeyTest.php b/tests/Unit/AbstractCollection/HasKeyTest.php index 850aa0e..ff30eb6 100644 --- a/tests/Unit/AbstractCollection/HasKeyTest.php +++ b/tests/Unit/AbstractCollection/HasKeyTest.php @@ -6,31 +6,38 @@ use PHPUnit\Framework\TestCase; +/** @covers \Steevanb\PhpCollection\AbstractCollection::hasKey */ final class HasKeyTest extends TestCase { - public function testHasStringKey(): void + public function testStringKeys(): void { - $collection = new Collection(['foo' => 1, 'bar' => '2', 'baz' => null]); + $collection = new TestCollection(['foo' => 1, 'bar' => '2', 'baz' => null]); static::assertTrue($collection->hasKey('foo')); static::assertTrue($collection->hasKey('bar')); static::assertTrue($collection->hasKey('baz')); + static::assertFalse($collection->hasKey('qux')); } - public function testHasIntegerKey(): void + public function testIntegerKeys(): void { - $collection = new Collection([0 => 1, 1 => '2', 3 => null]); + $collection = new TestCollection([0 => 1, 1 => '2', 3 => null]); static::assertTrue($collection->hasKey(0)); + static::assertFalse($collection->hasKey(2)); static::assertTrue($collection->hasKey(1)); static::assertTrue($collection->hasKey(3)); + static::assertFalse($collection->hasKey(4)); } - public function testDoNotHaveKey(): void + public function testMixedKeys(): void { - $collection = new Collection([0 => 1, 1 => '2', 3 => null]); + $collection = new TestCollection([0 => 1, 'foo' => 'bar', 3 => null]); + static::assertTrue($collection->hasKey(0)); static::assertFalse($collection->hasKey(2)); + static::assertTrue($collection->hasKey('foo')); + static::assertTrue($collection->hasKey(3)); static::assertFalse($collection->hasKey(4)); } } diff --git a/tests/Unit/AbstractCollection/ReadOnlyTest.php b/tests/Unit/AbstractCollection/ReadOnlyTest.php deleted file mode 100644 index 1ebf42d..0000000 --- a/tests/Unit/AbstractCollection/ReadOnlyTest.php +++ /dev/null @@ -1,96 +0,0 @@ -isReadOnly()); - } - - public function testReplace(): void - { - $collection = new Collection([1, 2]); - $collection->callDoReplace([3, 4]); - - static::addToAssertionCount(1); - } - - public function testAddValue(): void - { - $collection = new Collection([1, 2]); - $collection->callDoAdd(3); - - static::addToAssertionCount(1); - } - - public function testSet(): void - { - $collection = new Collection([1, 2]); - $collection->callDoSet(0, 4); - - static::assertSame(4, $collection->callDoGet(0)); - } - - public function testRemove(): void - { - $collection = new Collection([1, 2]); - $collection->remove(0); - - static::assertFalse($collection->hasKey(0)); - } - - public function testResetKeys(): void - { - $collection = new Collection([1, 2]); - $collection->resetKeys(); - - static::addToAssertionCount(1); - } - - public function testReadOnlyReplace(): void - { - $collection = (new Collection([1, 2]))->setReadOnly(); - - $this->expectException(ReadOnlyException::class); - $collection->callDoReplace([3, 4]); - } - - public function testReadOnlyAddValue(): void - { - $collection = (new Collection([1, 2]))->setReadOnly(); - - $this->expectException(ReadOnlyException::class); - $collection->callDoAdd(3); - } - - public function testReadOnlySet(): void - { - $collection = (new Collection([1, 2]))->setReadOnly(); - - $this->expectException(ReadOnlyException::class); - $collection->callDoSet(0, 4); - } - - public function testReadOnlyRemove(): void - { - $collection = (new Collection([1, 2]))->setReadOnly(); - - $this->expectException(ReadOnlyException::class); - $collection->remove(0); - } - - public function testReadOnlyResetKeys(): void - { - $collection = (new Collection([1, 2]))->setReadOnly(); - - $this->expectException(ReadOnlyException::class); - $collection->resetKeys(); - } -} diff --git a/tests/Unit/AbstractCollection/RemoveTest.php b/tests/Unit/AbstractCollection/RemoveTest.php index 17d64aa..eaf179c 100644 --- a/tests/Unit/AbstractCollection/RemoveTest.php +++ b/tests/Unit/AbstractCollection/RemoveTest.php @@ -5,13 +5,17 @@ namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection; use PHPUnit\Framework\TestCase; -use Steevanb\PhpCollection\Exception\KeyNotFoundException; +use Steevanb\PhpCollection\{ + Exception\KeyNotFoundException, + Exception\ReadOnlyException +}; +/** @covers \Steevanb\PhpCollection\AbstractCollection::remove */ final class RemoveTest extends TestCase { public function testRemove(): void { - $collection = new Collection([1, '2', null]); + $collection = new TestCollection([1, '2', null]); static::assertCount(3, $collection); @@ -25,9 +29,33 @@ public function testRemove(): void public function testKeyNotFound(): void { - $collection = new Collection([1, '2', null]); + $collection = new TestCollection([1, '2', null]); $this->expectException(KeyNotFoundException::class); + $this->expectExceptionMessage('Key "foo" not found.'); + $this->expectExceptionCode(0); $collection->remove('foo'); } + + public function testDoAdd(): void + { + $collection = new TestCollection([1, '2', null]); + + $collection + ->remove(1) + ->callDoAdd(3); + + static::assertFalse($collection->hasKey(1)); + static::assertSame(3, $collection->callDoGet(3)); + } + + public function testReadOnly(): void + { + $collection = (new TestCollection([1, 2]))->setReadOnly(); + + $this->expectException(ReadOnlyException::class); + $this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.'); + $this->expectExceptionCode(0); + $collection->remove(0); + } } diff --git a/tests/Unit/AbstractCollection/AbstractCollectionTest.php b/tests/Unit/AbstractCollection/ResetKeysTest.php similarity index 59% rename from tests/Unit/AbstractCollection/AbstractCollectionTest.php rename to tests/Unit/AbstractCollection/ResetKeysTest.php index ae3ee32..cd579e0 100644 --- a/tests/Unit/AbstractCollection/AbstractCollectionTest.php +++ b/tests/Unit/AbstractCollection/ResetKeysTest.php @@ -5,12 +5,14 @@ namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection; use PHPUnit\Framework\TestCase; +use Steevanb\PhpCollection\Exception\ReadOnlyException; -final class AbstractCollectionTest extends TestCase +/** @covers \Steevanb\PhpCollection\AbstractCollection::resetKeys */ +final class ResetKeysTest extends TestCase { public function testResetKeys(): void { - $collection = new Collection([0 => 1, 1 => '2', 2 => null, 'foo' => 'foo']); + $collection = new TestCollection([0 => 1, 1 => '2', 2 => null, 'foo' => 'foo']); static::assertSame([0, 1, 2, 'foo'], array_keys($collection->toArray())); static::assertSame(1, $collection->callDoGet(0)); @@ -27,11 +29,13 @@ public function testResetKeys(): void static::assertSame('foo', $collection->callDoGet(3)); } - public function testToArray(): void + public function testReadOnly(): void { - $data = [0 => 1, 1 => '2', 2 => null, 'foo' => 'foo']; - $collection = new Collection($data); + $collection = (new TestCollection([1, 2]))->setReadOnly(); - static::assertSame($data, $collection->toArray()); + $this->expectException(ReadOnlyException::class); + $this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.'); + $this->expectExceptionCode(0); + $collection->resetKeys(); } } diff --git a/tests/Unit/AbstractCollection/Collection.php b/tests/Unit/AbstractCollection/TestCollection.php similarity index 92% rename from tests/Unit/AbstractCollection/Collection.php rename to tests/Unit/AbstractCollection/TestCollection.php index 44adac4..59ad23a 100644 --- a/tests/Unit/AbstractCollection/Collection.php +++ b/tests/Unit/AbstractCollection/TestCollection.php @@ -6,7 +6,7 @@ use Steevanb\PhpCollection\AbstractCollection; -final class Collection extends AbstractCollection +final class TestCollection extends AbstractCollection { public function callDoSet(string|int $key, mixed $value): static { diff --git a/tests/Unit/AbstractCollection/ToArrayTest.php b/tests/Unit/AbstractCollection/ToArrayTest.php new file mode 100644 index 0000000..d028524 --- /dev/null +++ b/tests/Unit/AbstractCollection/ToArrayTest.php @@ -0,0 +1,19 @@ + 1, 1 => '2', 2 => null, 'foo' => 'foo']; + $collection = new TestCollection($data); + + static::assertSame($data, $collection->toArray()); + } +} diff --git a/tests/Unit/AbstractCollection/ValudAlreadyExistsTest.php b/tests/Unit/AbstractCollection/ValudAlreadyExistsTest.php deleted file mode 100644 index 6e7a434..0000000 --- a/tests/Unit/AbstractCollection/ValudAlreadyExistsTest.php +++ /dev/null @@ -1,56 +0,0 @@ -callDoGet(0)); - static::assertSame(11, $collection->callDoGet(1)); - static::assertSame(11, $collection->callDoGet(2)); - static::assertSame(13, $collection->callDoGet(3)); - } - - public function testDoNotAdd(): void - { - $collection = new Collection([10, 11, 11, 13], ValueAlreadyExistsModeEnum::DO_NOT_ADD); - - static::assertCount(3, $collection); - static::assertSame(10, $collection->callDoGet(0)); - static::assertSame(11, $collection->callDoGet(1)); - static::assertSame(13, $collection->callDoGet(3)); - } - - public function testDoNotAdd2(): void - { - $collection = new Collection([], ValueAlreadyExistsModeEnum::DO_NOT_ADD); - $collection - ->callDoAdd(10) - ->callDoAdd(11) - ->callDoAdd(11) - ->callDoAdd(13); - - static::assertCount(3, $collection); - static::assertSame(10, $collection->callDoGet(0)); - static::assertSame(11, $collection->callDoGet(1)); - static::assertSame(13, $collection->callDoGet(2)); - } - - public function testException(): void - { - static::expectException(ValueAlreadyExistsException::class); - new Collection([10, 11, 11, 13], ValueAlreadyExistsModeEnum::EXCEPTION); - } -} diff --git a/tests/Unit/AbstractEnumCollection/CollectionTest.php b/tests/Unit/AbstractEnumCollection/CollectionTest.php deleted file mode 100644 index 76956af..0000000 --- a/tests/Unit/AbstractEnumCollection/CollectionTest.php +++ /dev/null @@ -1,44 +0,0 @@ -toArray(); - - static::assertCount(0, $array); - } - - public function testOneValue(): void - { - $array = (new TestAbstractEnumCollection(TestEnum::class, [TestEnum::VALUE1]))->toArray(); - - static::assertCount(1, $array); - static::assertArrayHasKey(0, $array); - static::assertSame($array[0], TestEnum::VALUE1); - } - - public function testThreeValue(): void - { - $array = ( - new TestAbstractEnumCollection( - TestEnum::class, - [TestEnum::VALUE1, TestEnum::VALUE2, TestEnum::VALUE3] - ) - )->toArray(); - - static::assertCount(3, $array); - static::assertArrayHasKey(0, $array); - static::assertSame($array[0], TestEnum::VALUE1); - static::assertArrayHasKey(1, $array); - static::assertSame($array[1], TestEnum::VALUE2); - static::assertArrayHasKey(2, $array); - static::assertSame($array[2], TestEnum::VALUE3); - } -} diff --git a/tests/Unit/ArrayTest.php b/tests/Unit/ArrayTest.php index ccb9676..fffffe9 100644 --- a/tests/Unit/ArrayTest.php +++ b/tests/Unit/ArrayTest.php @@ -6,7 +6,10 @@ use PHPUnit\Framework\TestCase; -/** Goal of this class is to be "sure" of the behavior of array keys */ +/** + * Goal of this class is to be "sure" of the behavior of array keys + * @coversNothing + */ final class ArrayTest extends TestCase { public function testString(): void diff --git a/tests/Unit/CreateMockForMehodCallTrait.php b/tests/Unit/CreateMockForMehodCallTrait.php new file mode 100644 index 0000000..aad95f3 --- /dev/null +++ b/tests/Unit/CreateMockForMehodCallTrait.php @@ -0,0 +1,34 @@ + $className + * @param mixed ...$parameters + * @return T + */ + protected function createMockForMethodCall(string $className, string $method, ...$parameters) + { + $return = $this + ->getMockBuilder($className) + ->onlyMethods([$method]) + ->disableOriginalConstructor() + ->getMock(); + + $return + ->expects(static::once()) + ->method($method) + ->with(...$parameters); + + return $return; + } +} diff --git a/tests/Unit/AbstractEnumCollection/ConstructTest.php b/tests/Unit/EnumCollection/AbstractEnumCollection/AssertClassNameTest.php similarity index 54% rename from tests/Unit/AbstractEnumCollection/ConstructTest.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/AssertClassNameTest.php index 31cfc1c..659a2aa 100644 --- a/tests/Unit/AbstractEnumCollection/ConstructTest.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/AssertClassNameTest.php @@ -2,50 +2,42 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; use PHPUnit\Framework\TestCase; -use Steevanb\PhpCollection\{ - Exception\InvalidTypeException, - ValueAlreadyExistsModeEnum -}; +use Steevanb\PhpCollection\Exception\InvalidTypeException; -final class ConstructTest extends TestCase +/** @covers \Steevanb\PhpCollection\EnumCollection\AbstractEnumCollection::assertClassName */ +final class AssertClassNameTest extends TestCase { - public function testDefaultValues(): void + public function testUnitEnum(): void { $collection = new TestAbstractEnumCollection(TestEnum::class); - static::assertSame( - 'Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection\TestEnum', - $collection->getClassName() - ); - static::assertSame(ValueAlreadyExistsModeEnum::ADD, $collection->getValueAlreadyExistsMode()); + $collection->callAssertClassName(TestEnum::class); + $this->addToAssertionCount(1); } - public function testEnum(): void + public function testIntEnum(): void { - $collection = new TestAbstractEnumCollection(TestEnum::class); + $collection = new TestAbstractEnumCollection(TestIntEnum::class); - static::assertSame(TestEnum::class, $collection->getClassName()); + $collection->callAssertClassName(TestIntEnum::class); + $this->addToAssertionCount(1); } public function testStringEnum(): void { $collection = new TestAbstractEnumCollection(TestStringEnum::class); - static::assertSame(TestStringEnum::class, $collection->getClassName()); - } - - public function testIntEnum(): void - { - $collection = new TestAbstractEnumCollection(TestIntEnum::class); - - static::assertSame(TestIntEnum::class, $collection->getClassName()); + $collection->callAssertClassName(TestStringEnum::class); + $this->addToAssertionCount(1); } public function testObject(): void { + $collection = new TestAbstractEnumCollection(TestEnum::class); + $this->expectException(InvalidTypeException::class); $this->expectExceptionCode(0); $this->expectExceptionMessage( @@ -53,6 +45,6 @@ public function testObject(): void . 'Use Steevanb\PhpCollection\ObjectCollection\AbstractObjectCollection or ' . 'Steevanb\PhpCollection\ObjectCollection\AbstractObjectNullableCollection if you want to store objects.' ); - new TestAbstractEnumCollection(\stdClass::class); + $collection->callAssertClassName(\stdClass::class); } } diff --git a/tests/Unit/AbstractEnumCollection/CastValueToStringTest.php b/tests/Unit/EnumCollection/AbstractEnumCollection/CastValueToStringTest.php similarity index 80% rename from tests/Unit/AbstractEnumCollection/CastValueToStringTest.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/CastValueToStringTest.php index c172669..7963f27 100644 --- a/tests/Unit/AbstractEnumCollection/CastValueToStringTest.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/CastValueToStringTest.php @@ -2,10 +2,11 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; use PHPUnit\Framework\TestCase; +/** @covers \Steevanb\PhpCollection\EnumCollection\AbstractEnumCollection::castValueToString */ final class CastValueToStringTest extends TestCase { public function testEnum(): void diff --git a/tests/Unit/EnumCollection/AbstractEnumCollection/ConstructTest.php b/tests/Unit/EnumCollection/AbstractEnumCollection/ConstructTest.php new file mode 100644 index 0000000..83a478c --- /dev/null +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/ConstructTest.php @@ -0,0 +1,35 @@ +getClassName() + ); + static::assertSame(ValueAlreadyExistsModeEnum::ADD, $collection->getValueAlreadyExistsMode()); + static::assertCount(0, $collection); + } + + public function testAssertClassNameIsCalled(): void + { + $mock = $this->createMockForMethodCall(TestAbstractEnumCollection::class, 'assertClassName', TestEnum::class); + + $mock->__construct(); + } +} diff --git a/tests/Unit/AbstractEnumCollection/TestAbstractEnumCollection.php b/tests/Unit/EnumCollection/AbstractEnumCollection/TestAbstractEnumCollection.php similarity index 63% rename from tests/Unit/AbstractEnumCollection/TestAbstractEnumCollection.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/TestAbstractEnumCollection.php index 745f6a2..5fef01d 100644 --- a/tests/Unit/AbstractEnumCollection/TestAbstractEnumCollection.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/TestAbstractEnumCollection.php @@ -2,12 +2,13 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; use Steevanb\PhpCollection\EnumCollection\AbstractEnumCollection; class TestAbstractEnumCollection extends AbstractEnumCollection { + /** @param class-string $className */ public function __construct(string $className = TestEnum::class, iterable $values = []) { parent::__construct($className, $values); @@ -17,4 +18,9 @@ public function callCastValueToString(\UnitEnum $enum): string { return $this->castValueToString($enum); } + + public function callAssertClassName(string $className): static + { + return $this->assertClassName($className); + } } diff --git a/tests/Unit/AbstractEnumCollection/TestEnum.php b/tests/Unit/EnumCollection/AbstractEnumCollection/TestEnum.php similarity index 55% rename from tests/Unit/AbstractEnumCollection/TestEnum.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/TestEnum.php index 67245c0..8f6508c 100644 --- a/tests/Unit/AbstractEnumCollection/TestEnum.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/TestEnum.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; enum TestEnum { diff --git a/tests/Unit/AbstractEnumCollection/TestIntEnum.php b/tests/Unit/EnumCollection/AbstractEnumCollection/TestIntEnum.php similarity index 59% rename from tests/Unit/AbstractEnumCollection/TestIntEnum.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/TestIntEnum.php index 438d856..0f931f9 100644 --- a/tests/Unit/AbstractEnumCollection/TestIntEnum.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/TestIntEnum.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; enum TestIntEnum: int { diff --git a/tests/Unit/AbstractEnumCollection/TestStringEnum.php b/tests/Unit/EnumCollection/AbstractEnumCollection/TestStringEnum.php similarity index 64% rename from tests/Unit/AbstractEnumCollection/TestStringEnum.php rename to tests/Unit/EnumCollection/AbstractEnumCollection/TestStringEnum.php index 232a3b8..838fd1b 100644 --- a/tests/Unit/AbstractEnumCollection/TestStringEnum.php +++ b/tests/Unit/EnumCollection/AbstractEnumCollection/TestStringEnum.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Steevanb\PhpCollection\Tests\Unit\AbstractEnumCollection; +namespace Steevanb\PhpCollection\Tests\Unit\EnumCollection\AbstractEnumCollection; enum TestStringEnum: string { diff --git a/tests/Unit/ObjectCollection/AbstractObjectCollectionTest.php b/tests/Unit/ObjectCollection/AbstractObjectCollectionTest.php index 8ce7cfd..8b9a8d1 100644 --- a/tests/Unit/ObjectCollection/AbstractObjectCollectionTest.php +++ b/tests/Unit/ObjectCollection/AbstractObjectCollectionTest.php @@ -38,13 +38,13 @@ public function testCanAddValue(): void public function testCanAddValueInvalidInstanceOf(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); new ObjectCollection([new \DateTime()]); } public function testCanAddValueInvalidValueNull(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new ObjectCollection([null]); } diff --git a/tests/Unit/ObjectCollection/AbstractObjectNullableCollectionTest.php b/tests/Unit/ObjectCollection/AbstractObjectNullableCollectionTest.php index 2ebf629..d13afd2 100644 --- a/tests/Unit/ObjectCollection/AbstractObjectNullableCollectionTest.php +++ b/tests/Unit/ObjectCollection/AbstractObjectNullableCollectionTest.php @@ -37,7 +37,7 @@ public function testCanAddValue(): void public function testCanAddValueInvalidInstanceOf(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); new ObjectNullableCollection([new \DateTime()]); } diff --git a/tests/Unit/ObjectCollection/SetClassNameTest.php b/tests/Unit/ObjectCollection/SetClassNameTest.php index d9b130a..bd43db4 100644 --- a/tests/Unit/ObjectCollection/SetClassNameTest.php +++ b/tests/Unit/ObjectCollection/SetClassNameTest.php @@ -24,7 +24,7 @@ public function testSetUnitEnum(): void { $this->expectException(InvalidTypeException::class); $this->expectExceptionCode(0); - $this->expectDeprecationMessage( + $this->expectExceptionMessage( 'Steevanb\PhpCollection\ObjectCollection\AbstractObjectCollection can not store UnitEnum or BackedEnum.' . ' Use Steevanb\PhpCollection\EnumCollection\AbstractEnumCollection instead.' ); @@ -35,7 +35,7 @@ public function testSetBackedEnum(): void { $this->expectException(InvalidTypeException::class); $this->expectExceptionCode(0); - $this->expectDeprecationMessage( + $this->expectExceptionMessage( 'Steevanb\PhpCollection\ObjectCollection\AbstractObjectCollection can not store UnitEnum or BackedEnum.' . ' Use Steevanb\PhpCollection\EnumCollection\AbstractEnumCollection instead.' ); diff --git a/tests/Unit/ScalarCollection/FloatCollectionTest.php b/tests/Unit/ScalarCollection/FloatCollectionTest.php index f6f8b25..b9c2367 100644 --- a/tests/Unit/ScalarCollection/FloatCollectionTest.php +++ b/tests/Unit/ScalarCollection/FloatCollectionTest.php @@ -24,27 +24,27 @@ public function testAllowFloat(): void public function testInvalidTypeString(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new FloatCollection(['4']); } public function testInvalidTypeInt(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); new FloatCollection([1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new FloatCollection([true]); } public function testInvalidTypeNull(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new FloatCollection([null]); } @@ -75,7 +75,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new FloatCollection([1.0, 2.0], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new FloatCollection([2.0, 3.0])); } diff --git a/tests/Unit/ScalarCollection/FloatNullableCollectionTest.php b/tests/Unit/ScalarCollection/FloatNullableCollectionTest.php index 67bdd17..3bea056 100644 --- a/tests/Unit/ScalarCollection/FloatNullableCollectionTest.php +++ b/tests/Unit/ScalarCollection/FloatNullableCollectionTest.php @@ -24,20 +24,20 @@ public function testAllowFloat(): void public function testInvalidTypeString(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new FloatNullableCollection(['4']); } public function testInvalidTypeInt(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); new FloatNullableCollection([1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new FloatNullableCollection([true]); } @@ -76,7 +76,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new FloatNullableCollection([1.0, 2.0], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new FloatNullableCollection([2.0, 3.0])); } diff --git a/tests/Unit/ScalarCollection/IntCollectionTest.php b/tests/Unit/ScalarCollection/IntCollectionTest.php index 409c74f..9bfb66e 100644 --- a/tests/Unit/ScalarCollection/IntCollectionTest.php +++ b/tests/Unit/ScalarCollection/IntCollectionTest.php @@ -24,28 +24,28 @@ public function testAllowInt(): void public function testInvalidTypeString(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerCollection(['4']); } public function testInvalidTypeFloat(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerCollection([3.1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerCollection([true]); } public function testInvalidTypeNull(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerCollection([null]); } @@ -76,7 +76,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new IntegerCollection([1, 2], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new IntegerCollection([2, 3])); } diff --git a/tests/Unit/ScalarCollection/IntNullableCollectionTest.php b/tests/Unit/ScalarCollection/IntNullableCollectionTest.php index 1df9570..1ae80e3 100644 --- a/tests/Unit/ScalarCollection/IntNullableCollectionTest.php +++ b/tests/Unit/ScalarCollection/IntNullableCollectionTest.php @@ -24,21 +24,21 @@ public function testAllowInt(): void public function testInvalidTypeString(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerNullableCollection(['4']); } public function testInvalidTypeFloat(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerNullableCollection([3.1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new IntegerNullableCollection([true]); } @@ -77,7 +77,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new IntegerNullableCollection([1, 2], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new IntegerNullableCollection([2, 3])); } diff --git a/tests/Unit/ScalarCollection/StringCollectionTest.php b/tests/Unit/ScalarCollection/StringCollectionTest.php index 49a11fe..f966f96 100644 --- a/tests/Unit/ScalarCollection/StringCollectionTest.php +++ b/tests/Unit/ScalarCollection/StringCollectionTest.php @@ -24,28 +24,28 @@ public function testAllowString(): void public function testInvalidTypeInt(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringCollection([1]); } public function testInvalidTypeFloat(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringCollection([3.1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringCollection([true]); } public function testInvalidTypeNull(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringCollection([null]); } @@ -76,7 +76,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new StringCollection(['foo', 'bar'], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new StringCollection(['bar', 'baz'])); } diff --git a/tests/Unit/ScalarCollection/StringNullableCollectionTest.php b/tests/Unit/ScalarCollection/StringNullableCollectionTest.php index a15e4d0..0259745 100644 --- a/tests/Unit/ScalarCollection/StringNullableCollectionTest.php +++ b/tests/Unit/ScalarCollection/StringNullableCollectionTest.php @@ -24,21 +24,21 @@ public function testAllowString(): void public function testInvalidTypeInt(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringNullableCollection([1]); } public function testInvalidTypeFloat(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringNullableCollection([3.1]); } public function testInvalidTypeBool(): void { - static::expectException(InvalidTypeException::class); + $this->expectException(InvalidTypeException::class); /** @phpstan-ignore-next-line */ new StringNullableCollection([true]); } @@ -77,7 +77,7 @@ public function testMergeValueAlreadyExistsDoNotAdd(): void public function testMergeValueAlreadyExistsException(): void { - static::expectException(ValueAlreadyExistsException::class); + $this->expectException(ValueAlreadyExistsException::class); (new StringNullableCollection(['foo', 'bar'], ValueAlreadyExistsModeEnum::EXCEPTION)) ->merge(new StringNullableCollection(['bar', 'baz'])); }