wyrihaximus/test-utilities
A set of PHP test utilities for package development: a PHPUnit TestCase with helpers like random namespaces and temp directories, plus ready-made configuration defaults for PHPStan and Rector (paths and docblock-to-attribute conversions).
Installation:
composer require wyrihaximus/test-utilities --dev
Add to your composer.json under require-dev if not using global install.
PHPUnit Integration:
Extend your test case with WyriHaximus\TestUtilities\TestCase:
use WyriHaximus\TestUtilities\TestCase;
class MyTest extends TestCase
{
// Tests here
}
First Use Case: Leverage the built-in random namespace/directory utilities for file storage tests:
public function testFileStorage()
{
$filePath = $this->getRandomFilePath();
// Use $filePath in your test
}
Rector Configuration:
Use the provided RectorConfig for migrations:
// rector.php
use WyriHaximus\TestUtilities\RectorConfig;
return RectorConfig::configure(__DIR__ . '/../src');
Test Isolation:
Use $this->getRandomNamespace() and $this->getRandomDirectory() to avoid test pollution:
$namespace = $this->getRandomNamespace();
$this->assertNamespaceIsUnique($namespace);
Mocking & Factories:
Combine with Laravel’s createMock() or factories for complex test data:
$mock = $this->createMock(User::class);
$mock->method('getName')->willReturn($this->getRandomString());
Assertion Helpers:
Utilize built-in assertions like assertNamespaceIsUnique() or assertDirectoryIsEmpty().
Automated Refactoring: Run Rector with the provided config to standardize docblocks to attributes:
vendor/bin/rector process src --dry-run
Custom Rules:
Extend RectorConfig for project-specific rules:
return RectorConfig::configure(__DIR__)
->withRule(SetTypehintRule::class)
->withRule(AddFinalPrivateRule::class);
Extension Config: Use the package’s PHPStan extension for stricter type checking:
// phpstan.neon
includes:
- vendor/wyrihaximus/test-utilities/phpstan.neon
Custom Rules:
Combine with wyrihaximus/phpstan-rules-wrapper for project-specific rules.
Namespace Collisions:
$this->assertNamespaceIsUnique().Rector Version Pinning:
icanhaxstring/composer-unused).composer.json for pinned versions if custom Rector rules fail.PHPUnit Slow Test Detection:
ergebnis/phpunit-slow-test-detector by default.phpunit.xml:
<phpunit ...>
<extensions>
<extension class="Ergebnis\PhpunitSlowTestDetector\SlowTestDetectorExtension"/>
</extensions>
</phpunit>
Randomness Issues:
$this->getRandomString() with a seed for reproducibility:
$this->seedRandom(1234);
$randomString = $this->getRandomString();
File/Directory Permissions:
$this->getRandomDirectory()) have write permissions:
$dir = $this->getRandomDirectory();
$this->assertDirectoryIsWritable($dir);
PHPStan False Positives:
ignoreErrors:
- WyriHaximus\TestUtilities\Rules\*.php
Custom Test Case:
Override TestCase methods for project-specific behavior:
class CustomTestCase extends TestCase
{
protected function getRandomNamespace(): string
{
return 'Custom\\' . parent::getRandomNamespace();
}
}
Rector Rule Sets:
Create custom rule sets by extending RectorConfig:
class ProjectRectorConfig extends RectorConfig
{
public function __construct(string $path)
{
parent::__construct($path);
$this->withRule(YourCustomRule::class);
}
}
PHPUnit Listeners:
Add custom listeners via setUp():
protected function setUp(): void
{
parent::setUp();
$this->addListener(new YourCustomListener());
}
How can I help you explore Laravel packages today?