phpunitgoodpractices/traits
A small set of PHP traits to improve PHPUnit test code by reducing boilerplate and encouraging cleaner, reusable assertions and setup patterns. Useful for Laravel and general PHP projects that want more readable, maintainable tests.
Install via Composer: composer require --dev phpunitgoodpractices/traits. No extra configuration required. Start by selecting one or two traits (e.g., AssertsArraySubset, AssertsContains, or AssertsThat) and mix them into your TestCase class:
use PHPUnit\Framework\TestCase;
use PHPUnitGoodPractices\Traits\AssertsContains;
use PHPUnitGoodPractices\Traits\AssertsThat;
class MyTestCase extends TestCase
{
use AssertsContains;
use AssertsThat;
}
Then write expressive assertions like $this->assertContains('foo', $array) or $this->assertThat($user, $this->isInstanceOf(User::class)). Begin with traits that replace verbose built-in assertions or improve readability (e.g., AssertsEmpty, AssertsTrue, AssertsFalse).
TestCase) to enforce consistency (e.g., always use assertArrayHasKey via AssertsArrayHasKey).AssertsApiResponse that composes AssertsContains and AssertsJsonSubset).assertThat() for custom matchers and chain expectations for more fluent APIs (e.g., $this->assertThat($response, $this->statusCode(201)) if supported).BaseTestCase that uses the recommended traits, then inherit from it across your suite.TestCase or other traits define methods/properties with the same name (e.g., assertContains), PHP will throw a fatal error. Use as to alias conflicting traits: use AssertsContains { assertContains as public traitAssertContains; }.$this->assertTrue($bool) over $this->assertThat($bool, $this->isTrue()) unless your team agrees on a convention.TestCase classes use the correct use statements (autocompletion tools like PHPStan may require explicit declarations).TestCase to add logging, custom context, or deprecation warnings—but be cautious about backward-compatibility in assertions.How can I help you explore Laravel packages today?