wyrihaximus/test-utilities
Test utilities for api-clients packages, providing a PHPStan extension so test classes can extend WyriHaximus\TestUtilities\TestCase, with automatic registration via phpstan/extension-installer or manual inclusion of extension.neon.
Installation:
composer require wyrihaximus/test-utilities --dev
Add to your composer.json under require-dev if not using global install.
Version Alignment (as of 14.5.0): The package tracks a modern toolchain and pulls it in transitively. Before upgrading, confirm your project matches:
phpunit/phpunit ^13.4.1wyrihaximus/phpstan-rules-wrapper ^14.10.1wyrihaximus/qa-tooling-wrapper ^2.4.0wyrihaximus/makefiles ^0.15.2composer require --dev wyrihaximus/test-utilities:^14.5 phpunit/phpunit:^13.4
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 (^14.10.1 as of 14.5.0) for project-specific rules.
Shared QA Tooling:
The package rides on wyrihaximus/qa-tooling-wrapper (^2.4.0), so lint/static-analysis binaries arrive pinned and pre-wired — prefer those entry points over hand-rolled vendor/bin invocations so your CI matches the package’s expectations.
Reusable GitHub Actions:
The 14.5.0 line is validated against WyriHaximus/github-workflows v1.1.2. If you copy CI from this repo, pin to @v1.1.2 rather than @main:
- uses: WyriHaximus/github-workflows/.github/workflows/[email protected]
Renovate-Driven Updates: Expect frequent patch bumps of the whole toolchain (this release was 10 dependency PRs, 0 code changes). Batch them — a lockfile-only update should never require code changes.
Major Version Jump (13 → 14):
The package went from 13.2.0 to 14.5.0. Major bumps in this repo historically mean "toolchain alignment" (PHPUnit major, PHPStan wrapper major), not API redesign — but your own phpunit.xml schema, Rector sets, and PHPStan baseline formats must be upgraded in lockstep.
^14.5 in a dedicated branch, run composer update --with-all-dependencies, then fix phpunit.xml and baseline issues before touching application code.Namespace Collisions:
$this->assertNamespaceIsUnique().Rector Version Pinning:
icanhaxstring/composer-unused).composer.json for pinned versions if custom Rector rules fail. wyrihaximus/phpstan-rules-wrapper moves fast (14.9.0 → 14.10.1 inside this single release), so a composer update can silently change static-analysis results.PHPUnit Slow Test Detection:
ergebnis/phpunit-slow-test-detector by default.phpunit.xml:
<phpunit ...>
<extensions>
<extension class="Ergebnis\PhpunitSlowTestDetector\SlowTestDetectorExtension"/>
</extensions>
</phpunit>
<extensions> element is present (it is not implied by a composer require).Makefile Targets:
wyrihaximus/makefiles ^0.15.2 is a dev convenience only — do not depend on its targets in production pipelines you don't control. Copy the recipes into your own Makefile if you need stability.
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
vendor/wyrihaximus/phpstan-rules-wrapper/src before regenerating a baseline, so you don't bake in newly-introduced errors."Changed Nothing But CI Broke":
This release contains zero code changes — a red build after upgrading is almost always a transitive toolchain bump. composer why-not phpunit/phpunit ^13.4 is the fastest first move.
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?