codekandis / phpunit
`codekandis/phpunit` is a library providing an enhanced test case wrapper with additional constraint assertions for `PhpUnit`.
Requires
- php: >=8.5
- phpunit/phpunit: ^13.1.10
Requires (Dev)
- phpstan/phpstan: ^2.1.54
- rector/rector: ^2.4.3
- roave/security-advisories: dev-master
README
codekandis/phpunit is a library providing an enhanced test case wrapper with additional constraint assertions for
phpunit/phpunit.
Index
- Installation
- Usage
Installation
Install the latest version with
$ composer require --dev codekandis/phpunit
Usage
Using the test case wrapper
Create a test case that extends the TestCase wrapper.
The additional constraint assertions are available through this wrapper.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; class ApplicationTest extends TestCase { }
Using the data provider interface
Create a data provider that implements DataProviderInterface.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\DataProviderInterface; use Override; final class DifferentValuesDataProvider implements DataProviderInterface { #[Override] public static function provideData(): iterable { return [ 0 => [ 23, 42 ], 1 => [ 'foo', 'bar' ] ]; } }
Use the data provider in your test case.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; use CodeKandis\PhpUnit\DataProviderInterface; use PHPUnit\Framework\Attributes\DataProviderExternal; final class DifferentValuesTest extends TestCase { #[DataProviderExternal( DifferentValuesDataProvider::class, DataProviderInterface::PROVIDER_METHOD_NAME )] public function testValuesAreDifferent( mixed $value1, mixed $value2 ): void { static::assertNotSame( $value1, $value2 ); } }
Asserting array subsets
TestCase::assertArrayContainsKeyedSubset()
Use TestCase::assertArrayContainsKeyedSubset() to assert that $actualArray contains $expectedSubset with matching
keys and values. Set $strict to true to compare values strictly, or to false to compare values loosely.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; final class ArrayTest extends TestCase { public function testArrayContainsKeyedSubset(): void { static::assertArrayContainsKeyedSubset( [ 'foo' => [ 'bar' => 23 ] ], [ 'foo' => [ 'bar' => 23, 'baz' => 42 ] ], true ); } }
TestCase::assertArrayContainsUnkeyedSubset()
Use TestCase::assertArrayContainsUnkeyedSubset() to assert that $actualArray contains the values of
$expectedSubset without comparing keys. Set $strict to true to compare values strictly, or to false to compare
values loosely.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; final class ArrayTest extends TestCase { public function testArrayContainsUnkeyedSubset(): void { static::assertArrayContainsUnkeyedSubset( [ 23, ], [ 42, 23 ], true ); } }
TestCase::assertIsKeyedSubsetOfArray()
Use TestCase::assertIsKeyedSubsetOfArray() to assert that $actualSubset is contained in $expectedArray with
matching keys and values. Set $strict to true to compare values strictly, or to false to compare values loosely.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; final class ArrayTest extends TestCase { public function testKeyedSubsetIsSubsetOfArray(): void { static::assertIsKeyedSubsetOfArray( [ 'foo' => [ 'bar' => 23, 'baz' => 42 ] ], [ 'foo' => [ 'bar' => 23 ] ], true ); } }
TestCase::assertIsUnkeyedSubsetOfArray()
Use TestCase::assertIsUnkeyedSubsetOfArray() to assert that the values of $actualSubset are contained in
$expectedArray without comparing keys. Set $strict to true to compare values strictly, or to false to compare
values loosely.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; final class ArrayTest extends TestCase { public function testUnkeyedSubsetIsSubsetOfArray(): void { static::assertIsUnkeyedSubsetOfArray( [ 42, 23 ], [ 23 ], true ); } }
Asserting subclass relationships
TestCase::assertIsSubClassOf()
Use TestCase::assertIsSubClassOf() to assert that $actual is a subclass of or implements
$expectedInterfaceOrClassName. $actual can be an object or a class name.
<?php declare( strict_types = 1 ); namespace Vendor\Project\Tests; use CodeKandis\PhpUnit\TestCase; interface MessageHandlerInterface { } final class MessageHandler implements MessageHandlerInterface { } final class MessageHandlerTest extends TestCase { public function testMessageHandlerImplementsMessageHandlerInterface(): void { static::assertIsSubClassOf( MessageHandlerInterface::class, MessageHandler::class ); } }
The assertion throws an UnknownClassOrInterfaceException if the expected type does not exist.