Installation
composer require --dev antimattr/test-case
Add the trait to your test class:
use Antimattr\TestCase\TestCase;
class ExampleTest extends TestCase
First Use Case
Replace assertTrue() with assertIsTrue() or leverage built-in assertions like:
$this->assertIsTrue(true);
$this->assertIsFalse(false);
$this->assertIsNull(null);
$this->assertIsNotNull("string");
Key Features to Explore
@dataProvider with dataProvider() method.mock() helper for cleaner mocking.src/TestCase.php for all available assertions (e.g., assertIsArray(), assertContains()).Test Data Management
Use dataProvider() for reusable test data:
public function dataProvider()
{
return [
'valid case' => [true, true],
'invalid case' => [false, false],
];
}
public function testBooleanValues(bool $input, bool $expected)
{
$this->assertIsTrue($input === $expected);
}
Mocking Objects
Replace PHPUnit’s createMock() with:
$mock = $this->mock(MyClass::class);
$mock->method('doSomething')->willReturn('result');
Assertion Chaining Combine assertions for cleaner tests:
$this->assertIsArray($data)
->assertArrayHasKey('id')
->assertIsNotEmpty();
Illuminate\Foundation\Testing\TestCase and include TestCase trait:
use Antimattr\TestCase\TestCase;
use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;
class FeatureTest extends LaravelTestCase
{
use TestCase;
}
protected function assertIsCustom($value, $message = '')
{
$this->assertIsTrue($value === 'custom', $message);
}
Assertion Method Conflicts
Avoid naming custom methods assert*—they may override package assertions.
Fix: Prefix with customAssert*.
Mocking Quirks
The mock() helper uses PHPUnit’s createMock() under the hood. Ensure your mocks extend the correct class:
// ❌ Fails if MyClass doesn’t extend ArrayAccess
$mock = $this->mock(MyClass::class);
// ✅ Works
$mock = $this->mock(MyClass::class)->makePartial();
Data Provider Caching
@dataProvider runs once per test method. For dynamic data, use yield:
public function dataProvider()
{
yield 'dynamic' => [rand(), rand()];
}
$this->fail() for explicit test failures with messages:
$this->fail('Expected failure: ' . $this->getLastAssertionMessage());
->debug() to dump variables:
$this->assertIsArray($data)->debug($data)->assertNotEmpty();
protected function assertIsJson($value, $message = '')
{
$this->assertIsTrue(
is_string($value) && json_validate($value),
$message
);
}
setUp()/tearDown() for shared test logic:
protected function setUp(): void
{
parent::setUp();
$this->app->singleton(MyService::class, fn() => new MyService());
}
TestCase constructor:
public function __construct()
{
parent::__construct();
$this->useShortAssertions = false; // Disable short assertion syntax
}
How can I help you explore Laravel packages today?