lastdragon-ru/phpunit-extensions
PHPUnit extensions for PHP projects, adding extra test utilities and helpers to streamline assertions, fixtures, and test setup. Designed to integrate cleanly with PHPUnit and improve developer productivity in automated testing workflows.
Installation Add the package via Composer:
composer require --dev lastdragon-ru/phpunit-extensions
Ensure phpunit.xml includes the autoloader (Composer handles this by default).
First Use Case Test a simple array comparison in a PHPUnit test:
use LastDragon\PHPUnitExtensions\Assert\ArrayAssert;
class ExampleTest extends \PHPUnit\Framework\TestCase
{
public function testArrayContains()
{
$expected = ['foo', 'bar'];
$actual = ['foo', 'baz', 'bar'];
ArrayAssert::assertArrayContains($expected, $actual);
}
}
Where to Look First
Assert directory for available extensions.Traits for reusable test logic.Exceptions for custom error handling.Array Assertions Useful for testing collections, API responses, or database results:
ArrayAssert::assertArrayContains(['key' => 'value'], $array);
ArrayAssert::assertArrayNotContains(['key' => 'wrong'], $array);
ArrayAssert::assertArrayEqualsCanonicalizing($expected, $actual); // Ignores array key order
Object Assertions Validate object properties or method outputs:
ObjectAssert::assertPropertyEquals('name', 'John', $user);
ObjectAssert::assertMethodReturns('getName', 'John', $user);
Exception Testing Simplify exception assertions:
ExceptionAssert::assertException(function() {
throw new \RuntimeException('Test');
}, \RuntimeException::class, 'Test');
Database Testing (if extended) If the package includes DB helpers (hypothetical), use:
DatabaseAssert::assertRecordExists('users', ['email' => 'test@example.com']);
Custom Assertions Extend existing assertions or create new ones:
class CustomAssert extends \PHPUnit\Framework\Assert
{
public static function assertJsonPathEquals($path, $expected, $actual) {
// Custom logic
}
}
Combine with Laravel’s Testing
Use in Laravel’s HttpTests or FeatureTests for cleaner assertions:
$response = $this->get('/api/users');
ArrayAssert::assertArrayContains(['id' => 1], $response->json());
Data Providers
Pair with PHPUnit’s @dataProvider for bulk testing:
public function testMultipleArrays()
{
$arrays = [
[['a'], ['a', 'b']],
[['x'], ['x', 'y']],
];
foreach ($arrays as $expected) {
ArrayAssert::assertArrayContains($expected[0], $expected[1]);
}
}
Traits for Reusable Logic Use traits in test classes to avoid repetition:
use LastDragon\PHPUnitExtensions\Traits\AssertTrait;
class UserTest extends TestCase
{
use AssertTrait;
public function testUserCreation()
{
$this->assertArrayContains(['name' => 'Alice'], $user->toArray());
}
}
Namespace Conflicts
Ensure no naming collisions with existing assertions (e.g., assertEquals vs. custom assertEqualsCanonicalizing).
Overriding Default Assertions
Avoid shadowing PHPUnit’s built-in methods (e.g., assertArrayHasKey). Prefix custom methods clearly:
ArrayAssert::assertArrayHasKeyIgnoreCase('Name', $array); // Safer than overriding.
Performance with Large Arrays Some assertions (e.g., deep array comparisons) may slow tests. Use sparingly for large datasets.
Static Analysis Tools
Tools like PHPStan may flag unused assertions. Exclude the Assert directory in static analysis configs if needed.
Verbose Failures
Enable PHPUnit’s -v flag to see detailed assertion failures:
phpunit -v tests/Feature/UserTest.php
Custom Exception Messages Extend exceptions for better debugging:
try {
ArrayAssert::assertArrayContains(['missing'], $array);
} catch (\LastDragon\PHPUnitExtensions\Exceptions\ArrayContainsException $e) {
$this->fail($e->getMessage());
}
Autoloading
Ensure the package is listed in composer.json under require-dev and autoload-dev:
"autoload-dev": {
"psr-4": {
"LastDragon\\PHPUnitExtensions\\": "vendor/lastdragon-ru/phpunit-extensions/src"
}
}
PHPUnit Bootstrap
If using Laravel’s phpunit.xml, ensure the bootstrap file loads Composer’s autoloader:
<phpunit>
<bootstrap>vendor/autoload.php</bootstrap>
</phpunit>
Add Custom Assertions
Extend the base Assert class or create new classes in src/Assert:
namespace LastDragon\PHPUnitExtensions\Assert;
class StringAssert extends \PHPUnit\Framework\Assert
{
public static function assertContainsIgnoreCase($needle, $haystack) {
// Custom logic
}
}
Modify Existing Assertions
Override methods in a child class (e.g., CustomArrayAssert extends ArrayAssert).
Contribute Back
Submit PRs to the repo for missing features (e.g., Laravel-specific assertions like assertRedirectsToRoute).
Integration with Laravel Mixins For Laravel, create a test macro or mixin:
use LastDragon\PHPUnitExtensions\Assert\ArrayAssert;
$this->addMacro('assertJsonContains', function ($expected) {
ArrayAssert::assertArrayContains($expected, $this->response->json());
});
How can I help you explore Laravel packages today?