Skip to content

Commit 98c617a

Browse files
committed
Rework unit tests
1 parent 3e3316f commit 98c617a

27 files changed

+227
-169
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": "^8.1"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "9.5.*",
20+
"phpunit/phpunit": "10.0.*",
2121
"steevanb/php-backtrace": "2.1.*",
2222
"symfony/config": "6.1.*",
2323
"symfony/dependency-injection": "6.1.*",

config/ci/phpunit.php-8.1.xml

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!-- add requireCoverageMetadata="true" -->
13
<phpunit
2-
cacheResultFile="../../var/ci/phpunit/php-8.1/.phpunit.result.cache"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
36
colors="true"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory="../../var/ci/phpunit/php-8.1"
410
>
511
<testsuites>
612
<testsuite name="default">
713
<directory>../../tests</directory>
814
</testsuite>
915
</testsuites>
10-
<coverage
11-
cacheDirectory="../../var/ci/phpunit/code-coverage"
12-
processUncoveredFiles="true"
13-
>
16+
<coverage cacheDirectory="../../var/ci/phpunit/code-coverage">
1417
<include>
18+
<directory suffix=".php">../../bridge</directory>
1519
<directory suffix=".php">../../src</directory>
1620
</include>
1721
</coverage>

config/ci/phpunit.php-8.2.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
<?xml version="1.0"?>
12
<phpunit
2-
cacheResultFile="../../var/ci/phpunit/php-8.2/.phpunit.result.cache"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
35
colors="true"
6+
requireCoverageMetadata="true"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory="../../var/ci/phpunit/php-8.2"
410
>
511
<testsuites>
612
<testsuite name="default">

src/AbstractCollection.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ public function hasKey(string|int $key): bool
3636

3737
public function remove(string|int $key): static
3838
{
39-
$this->assertIsNotReadOnly();
40-
41-
if ($this->hasKey($key) === false) {
42-
throw new KeyNotFoundException('Key "' . $key . '" not found.');
43-
}
39+
$this
40+
->assertIsNotReadOnly()
41+
->assertHasKey($key);
4442

4543
unset($this->values[$key]);
4644

@@ -155,12 +153,19 @@ protected function doReplace(iterable $values): static
155153
}
156154

157155
protected function doGet(string|int $key): mixed
156+
{
157+
$this->assertHasKey($key);
158+
159+
return $this->values[$key];
160+
}
161+
162+
protected function assertHasKey(string|int $key): static
158163
{
159164
if ($this->hasKey($key) === false) {
160-
throw new KeyNotFoundException('Key "' . $key . '" not found.');
165+
throw new KeyNotFoundException($key);
161166
}
162167

163-
return $this->values[$key];
168+
return $this;
164169
}
165170

166171
protected function doHas(mixed $value): bool

src/Exception/KeyNotFoundException.php

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
class KeyNotFoundException extends PhpCollectionException
88
{
9+
public function __construct(string|int $key)
10+
{
11+
parent::__construct('Key "' . $key . '" not found.');
12+
}
913
}

tests/Unit/AbstractCollection/ChangeKeyCaseTest.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Steevanb\PhpCollection\KeyCaseEnum;
99

10+
/** @covers \Steevanb\PhpCollection\AbstractCollection::changeKeyCase */
1011
final class ChangeKeyCaseTest extends TestCase
1112
{
1213
public function testDefaultParameters(): void
@@ -90,11 +91,4 @@ public function testEmpty(): void
9091

9192
static::assertCount(0, $collection);
9293
}
93-
94-
public function testReturnType(): void
95-
{
96-
$collection = new Collection();
97-
98-
static::assertSame($collection, $collection->changeKeyCase());
99-
}
10094
}

tests/Unit/AbstractCollection/ClearTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88

9+
/** @covers \Steevanb\PhpCollection\AbstractCollection::clear */
910
final class ClearTest extends TestCase
1011
{
1112
public function testEmpty(): void

tests/Unit/AbstractCollection/CountableTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88

9+
/** @coversNothing */
910
final class CountableTest extends TestCase
1011
{
1112
public function testCount(): void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Steevanb\PhpCollection\Exception\ReadOnlyException;
9+
10+
/** @covers \Steevanb\PhpCollection\AbstractCollection::doAdd */
11+
final class DoAddTest extends TestCase
12+
{
13+
public function testOneValue(): void
14+
{
15+
$collection = (new Collection())
16+
->callDoAdd('foo');
17+
18+
static::assertSame('foo', $collection->callDoGet(0));
19+
}
20+
21+
public function testTreeValue(): void
22+
{
23+
$collection = (new Collection())
24+
->callDoAdd('foo')
25+
->callDoAdd('bar')
26+
->callDoAdd('baz');
27+
28+
static::assertSame('foo', $collection->callDoGet(0));
29+
static::assertSame('bar', $collection->callDoGet(1));
30+
static::assertSame('baz', $collection->callDoGet(2));
31+
}
32+
33+
public function testReadOnly(): void
34+
{
35+
$collection = (new Collection([1, 2]))->setReadOnly();
36+
37+
$this->expectException(ReadOnlyException::class);
38+
$this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.');
39+
$this->expectExceptionCode(0);
40+
$collection->callDoAdd(3);
41+
}
42+
}

tests/Unit/AbstractCollection/DoGetTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Steevanb\PhpCollection\Exception\KeyNotFoundException;
99

10+
/** @covers \Steevanb\PhpCollection\AbstractCollection::doGet */
1011
final class DoGetTest extends TestCase
1112
{
1213
public function testGet(): void
@@ -22,7 +23,9 @@ public function testKeyNotFound(): void
2223
{
2324
$collection = new Collection([1, '2', null]);
2425

25-
static::expectException(KeyNotFoundException::class);
26-
static::assertFalse($collection->callDoGet(3));
26+
$this->expectException(KeyNotFoundException::class);
27+
$this->expectExceptionMessage('Key "3" not found.');
28+
$this->expectExceptionCode(0);
29+
$collection->callDoGet(3);
2730
}
2831
}

tests/Unit/AbstractCollection/DoReplaceTest.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection;
66

77
use PHPUnit\Framework\TestCase;
8+
use Steevanb\PhpCollection\Exception\ReadOnlyException;
89

10+
/** @covers \Steevanb\PhpCollection\AbstractCollection::doReplace */
911
final class DoReplaceTest extends TestCase
1012
{
11-
public function testConstructorValues(): void
13+
public function testDoReplace(): void
1214
{
13-
$collection = new Collection([10, 11, 12, 13]);
15+
$collection = (new Collection())
16+
->callDoReplace([10, 11, 12, 13]);
1417

1518
static::assertCount(4, $collection);
1619
static::assertSame(10, $collection->callDoGet(0));
@@ -19,15 +22,24 @@ public function testConstructorValues(): void
1922
static::assertSame(13, $collection->callDoGet(3));
2023
}
2124

22-
public function testReplace(): void
25+
public function testCalledFromConstructor(): void
2326
{
24-
$collection = (new Collection())
25-
->callDoReplace([10, 11, 12, 13]);
27+
$collection = new Collection([10, 11, 12, 13]);
2628

2729
static::assertCount(4, $collection);
2830
static::assertSame(10, $collection->callDoGet(0));
2931
static::assertSame(11, $collection->callDoGet(1));
3032
static::assertSame(12, $collection->callDoGet(2));
3133
static::assertSame(13, $collection->callDoGet(3));
3234
}
35+
36+
public function testReadOnly(): void
37+
{
38+
$collection = (new Collection([1, 2]))->setReadOnly();
39+
40+
$this->expectException(ReadOnlyException::class);
41+
$this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.');
42+
$this->expectExceptionCode(0);
43+
$collection->callDoReplace([3, 4]);
44+
}
3345
}

tests/Unit/AbstractCollection/DoSetTest.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace Steevanb\PhpCollection\Tests\Unit\AbstractCollection;
66

77
use PHPUnit\Framework\TestCase;
8+
use Steevanb\PhpCollection\Exception\ReadOnlyException;
89

10+
/** @covers \Steevanb\PhpCollection\AbstractCollection::doSet */
911
final class DoSetTest extends TestCase
1012
{
11-
public function testSetIntegerKeys(): void
13+
public function testIntegerKeys(): void
1214
{
1315
$collection = new Collection();
1416
$collection->callDoSet(0, 1);
@@ -20,7 +22,7 @@ public function testSetIntegerKeys(): void
2022
static::assertNull($collection->callDoGet(2));
2123
}
2224

23-
public function testSetStringKeys(): void
25+
public function testStringKeys(): void
2426
{
2527
$collection = new Collection();
2628
$collection->callDoSet('foo', 1);
@@ -31,4 +33,26 @@ public function testSetStringKeys(): void
3133
static::assertSame('2', $collection->callDoGet('bar'));
3234
static::assertNull($collection->callDoGet('baz'));
3335
}
36+
37+
public function testStringAndIntKeys(): void
38+
{
39+
$collection = new Collection();
40+
$collection->callDoSet('foo', 1);
41+
$collection->callDoSet(0, '2');
42+
$collection->callDoSet('baz', null);
43+
44+
static::assertSame(1, $collection->callDoGet('foo'));
45+
static::assertSame('2', $collection->callDoGet(0));
46+
static::assertNull($collection->callDoGet('baz'));
47+
}
48+
49+
public function testReadOnly(): void
50+
{
51+
$collection = (new Collection([1, 2]))->setReadOnly();
52+
53+
$this->expectException(ReadOnlyException::class);
54+
$this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.');
55+
$this->expectExceptionCode(0);
56+
$collection->callDoSet(0, 4);
57+
}
3458
}

tests/Unit/AbstractCollection/HasKeyTest.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@
66

77
use PHPUnit\Framework\TestCase;
88

9+
/** @covers \Steevanb\PhpCollection\AbstractCollection::hasKey */
910
final class HasKeyTest extends TestCase
1011
{
11-
public function testHasStringKey(): void
12+
public function testStringKeys(): void
1213
{
1314
$collection = new Collection(['foo' => 1, 'bar' => '2', 'baz' => null]);
1415

1516
static::assertTrue($collection->hasKey('foo'));
1617
static::assertTrue($collection->hasKey('bar'));
1718
static::assertTrue($collection->hasKey('baz'));
19+
static::assertFalse($collection->hasKey('qux'));
1820
}
1921

20-
public function testHasIntegerKey(): void
22+
public function testIntegerKeys(): void
2123
{
2224
$collection = new Collection([0 => 1, 1 => '2', 3 => null]);
2325

2426
static::assertTrue($collection->hasKey(0));
27+
static::assertFalse($collection->hasKey(2));
2528
static::assertTrue($collection->hasKey(1));
2629
static::assertTrue($collection->hasKey(3));
30+
static::assertFalse($collection->hasKey(4));
2731
}
2832

29-
public function testDoNotHaveKey(): void
33+
public function testMixedKeys(): void
3034
{
31-
$collection = new Collection([0 => 1, 1 => '2', 3 => null]);
35+
$collection = new Collection([0 => 1, 'foo' => 'bar', 3 => null]);
3236

37+
static::assertTrue($collection->hasKey(0));
3338
static::assertFalse($collection->hasKey(2));
39+
static::assertTrue($collection->hasKey('foo'));
40+
static::assertTrue($collection->hasKey(3));
3441
static::assertFalse($collection->hasKey(4));
3542
}
3643
}

0 commit comments

Comments
 (0)