Skip to content

Commit 6d26fd9

Browse files
steevanbSteevan BARBOYON
authored andcommitted
Rework unit tests
1 parent 3e3316f commit 6d26fd9

38 files changed

Lines changed: 436 additions & 318 deletions

composer.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 12 additions & 7 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
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
1314
{
14-
$collection = new Collection(['foo' => 1]);
15+
$collection = new TestCollection(['foo' => 1]);
1516

1617
$collection->changeKeyCase();
1718

@@ -22,7 +23,7 @@ public function testDefaultParameters(): void
2223

2324
public function testLowerCaseAssociativesKeys(): void
2425
{
25-
$collection = new Collection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]);
26+
$collection = new TestCollection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]);
2627

2728
$collection->changeKeyCase(KeyCaseEnum::LOWER);
2829

@@ -37,7 +38,7 @@ public function testLowerCaseAssociativesKeys(): void
3738

3839
public function testUpperCaseAssociativesKeys(): void
3940
{
40-
$collection = new Collection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]);
41+
$collection = new TestCollection(['foo' => 1, 'BaR' => 2, 'BAZ' => 3]);
4142

4243
$collection->changeKeyCase(KeyCaseEnum::UPPER);
4344

@@ -52,7 +53,7 @@ public function testUpperCaseAssociativesKeys(): void
5253

5354
public function testLowerCaseIndexedKeys(): void
5455
{
55-
$collection = new Collection([0 => 1, 10 => 2, 20 => 3]);
56+
$collection = new TestCollection([0 => 1, 10 => 2, 20 => 3]);
5657

5758
$collection->changeKeyCase(KeyCaseEnum::LOWER);
5859

@@ -67,7 +68,7 @@ public function testLowerCaseIndexedKeys(): void
6768

6869
public function testUpperCaseIndexedKeys(): void
6970
{
70-
$collection = new Collection([0 => 1, 10 => 2, 20 => 3]);
71+
$collection = new TestCollection([0 => 1, 10 => 2, 20 => 3]);
7172

7273
$collection->changeKeyCase(KeyCaseEnum::UPPER);
7374

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

8384
public function testEmpty(): void
8485
{
85-
$collection = new Collection();
86+
$collection = new TestCollection();
8687

8788
static::assertCount(0, $collection);
8889

8990
$collection->changeKeyCase();
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
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
1213
{
13-
$collection = new Collection();
14+
$collection = new TestCollection();
1415

1516
static::assertCount(0, $collection);
1617

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

2627
public function testOneItem(): void
2728
{
28-
$collection = new Collection([1]);
29+
$collection = new TestCollection([1]);
2930

3031
static::assertCount(1, $collection);
3132

@@ -40,7 +41,7 @@ public function testOneItem(): void
4041

4142
public function testThreeItem(): void
4243
{
43-
$collection = new Collection([1, 2, 3]);
44+
$collection = new TestCollection([1, 2, 3]);
4445

4546
static::assertCount(3, $collection);
4647

tests/Unit/AbstractCollection/CountableTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
use PHPUnit\Framework\TestCase;
88

9+
/** @coversNothing */
910
final class CountableTest extends TestCase
1011
{
1112
public function testCount(): void
1213
{
13-
static::assertCount(3, new Collection([1, '2', null]));
14+
static::assertCount(3, new TestCollection([1, '2', null]));
1415
}
1516
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\{
9+
Exception\ReadOnlyException,
10+
Exception\ValueAlreadyExistsException,
11+
ValueAlreadyExistsModeEnum
12+
};
13+
14+
/** @covers \Steevanb\PhpCollection\AbstractCollection::doAdd */
15+
final class DoAddTest extends TestCase
16+
{
17+
public function testOneValue(): void
18+
{
19+
$collection = (new TestCollection())
20+
->callDoAdd('foo');
21+
22+
static::assertSame('foo', $collection->callDoGet(0));
23+
}
24+
25+
public function testTreeValue(): void
26+
{
27+
$collection = (new TestCollection())
28+
->callDoAdd('foo')
29+
->callDoAdd('bar')
30+
->callDoAdd('baz');
31+
32+
static::assertSame('foo', $collection->callDoGet(0));
33+
static::assertSame('bar', $collection->callDoGet(1));
34+
static::assertSame('baz', $collection->callDoGet(2));
35+
}
36+
37+
public function testReadOnly(): void
38+
{
39+
$collection = (new TestCollection([1, 2]))->setReadOnly();
40+
41+
$this->expectException(ReadOnlyException::class);
42+
$this->expectExceptionMessage('This collection is read only, you cannot edit it\'s values.');
43+
$this->expectExceptionCode(0);
44+
$collection->callDoAdd(3);
45+
}
46+
47+
public function testValueAlreadyExistsDoNotAdd(): void
48+
{
49+
$collection = new TestCollection([], ValueAlreadyExistsModeEnum::DO_NOT_ADD);
50+
$collection
51+
->callDoAdd(10)
52+
->callDoAdd(11)
53+
->callDoAdd(11)
54+
->callDoAdd(13);
55+
56+
static::assertCount(3, $collection);
57+
static::assertSame(10, $collection->callDoGet(0));
58+
static::assertSame(11, $collection->callDoGet(1));
59+
static::assertSame(13, $collection->callDoGet(2));
60+
}
61+
62+
public function testException(): void
63+
{
64+
$collection = new TestCollection([], ValueAlreadyExistsModeEnum::EXCEPTION);
65+
$collection
66+
->callDoAdd(10)
67+
->callDoAdd(11);
68+
69+
$this->expectException(ValueAlreadyExistsException::class);
70+
$this->expectExceptionMessage('Value "11" already exists.');
71+
$this->expectExceptionCode(0);
72+
$collection->callDoAdd(11);
73+
}
74+
}

tests/Unit/AbstractCollection/DoGetTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
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
1314
{
14-
$collection = new Collection([1, '2', null]);
15+
$collection = new TestCollection([1, '2', null]);
1516

1617
static::assertSame(1, $collection->callDoGet(0));
1718
static::assertSame('2', $collection->callDoGet(1));
@@ -20,9 +21,11 @@ public function testGet(): void
2021

2122
public function testKeyNotFound(): void
2223
{
23-
$collection = new Collection([1, '2', null]);
24+
$collection = new TestCollection([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
}

0 commit comments

Comments
 (0)