Skip to content

Rework unit tests #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
14 changes: 9 additions & 5 deletions config/ci/phpunit.php-8.1.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0"?>
<!-- add requireCoverageMetadata="true" -->
<phpunit
cacheResultFile="../../var/ci/phpunit/php-8.1/.phpunit.result.cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
failOnRisky="true"
failOnWarning="true"
cacheDirectory="../../var/ci/phpunit/php-8.1"
>
<testsuites>
<testsuite name="default">
<directory>../../tests</directory>
</testsuite>
</testsuites>
<coverage
cacheDirectory="../../var/ci/phpunit/code-coverage"
processUncoveredFiles="true"
>
<coverage cacheDirectory="../../var/ci/phpunit/code-coverage">
<include>
<directory suffix=".php">../../bridge</directory>
<directory suffix=".php">../../src</directory>
</include>
</coverage>
Expand Down
8 changes: 7 additions & 1 deletion config/ci/phpunit.php-8.2.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0"?>
<phpunit
cacheResultFile="../../var/ci/phpunit/php-8.2/.phpunit.result.cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
requireCoverageMetadata="true"
failOnRisky="true"
failOnWarning="true"
cacheDirectory="../../var/ci/phpunit/php-8.2"
>
<testsuites>
<testsuite name="default">
Expand Down
19 changes: 12 additions & 7 deletions src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Exception/KeyNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

class KeyNotFoundException extends PhpCollectionException
{
public function __construct(string|int $key)
{
parent::__construct('Key "' . $key . '" not found.');
}
}
20 changes: 7 additions & 13 deletions tests/Unit/AbstractCollection/ChangeKeyCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -82,19 +83,12 @@ public function testUpperCaseIndexedKeys(): void

public function testEmpty(): void
{
$collection = new Collection();
$collection = new TestCollection();

static::assertCount(0, $collection);

$collection->changeKeyCase();

static::assertCount(0, $collection);
}

public function testReturnType(): void
{
$collection = new Collection();

static::assertSame($collection, $collection->changeKeyCase());
}
}
7 changes: 4 additions & 3 deletions tests/Unit/AbstractCollection/ClearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -25,7 +26,7 @@ public function testEmpty(): void

public function testOneItem(): void
{
$collection = new Collection([1]);
$collection = new TestCollection([1]);

static::assertCount(1, $collection);

Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/AbstractCollection/CountableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
74 changes: 74 additions & 0 deletions tests/Unit/AbstractCollection/DoAddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection;

use PHPUnit\Framework\TestCase;
use Steevanb\PhpCollection\{
Exception\ReadOnlyException,
Exception\ValueAlreadyExistsException,
ValueAlreadyExistsModeEnum
};

/** @covers \Steevanb\PhpCollection\AbstractCollection::doAdd */
final class DoAddTest extends TestCase
{
public function testOneValue(): void
{
$collection = (new TestCollection())
->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);
}
}
11 changes: 7 additions & 4 deletions tests/Unit/AbstractCollection/DoGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}
}
47 changes: 42 additions & 5 deletions tests/Unit/AbstractCollection/DoReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -19,15 +26,45 @@ 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));
static::assertSame(11, $collection->callDoGet(1));
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]);
}
}
Loading