Installation Add the package via Composer:
composer require axstrad/test
Publish the config (if available) with:
php artisan vendor:publish --provider="Axstrad\Test\TestServiceProvider" --tag="config"
First Use Case
The package likely provides a base TestCase class for writing unit/feature tests. Extend it in your test classes:
use Axstrad\Test\TestCase;
class ExampleTest extends TestCase
{
public function test_example()
{
$this->assertTrue(true); // Basic assertion
}
}
Where to Look First
src/TestCase.php for core functionality (e.g., trait inclusions, helper methods).config/test.php (if published) for customizable settings like default assertions or mocking behavior.tests/ in the package repo for usage examples (if provided).Base Test Class Extension
Replace Laravel’s default TestCase with Axstrad\Test\TestCase for consistent test setup:
// phpunit.xml
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
Helper Methods
Leverage package-provided traits (e.g., AssertsHttp, MocksDatabase) for DRY test code:
use Axstrad\Test\Traits\AssertsHttp;
class UserTest extends TestCase
{
use AssertsHttp;
public function test_create_user()
{
$this->postJson('/users', ['name' => 'John'])
->assertCreated()
->assertJsonStructure(['data' => ['id', 'name']]);
}
}
Mocking and Factories
Use built-in mocking utilities (if available) to avoid repetitive create()/factory() calls:
public function test_user_creation_with_mock()
{
$mockUser = $this->mockUser(['email' => 'test@example.com']);
$this->assertDatabaseHas('users', $mockUser->toArray());
}
Custom Assertions
Extend or override assertions in a dedicated TestHelper class:
use Axstrad\Test\TestCase;
class CustomTestCase extends TestCase
{
protected function assertCustomResponse($response, $expected)
{
// Custom logic
}
}
HttpTests), integrate them into your createApplication():
protected function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Axstrad\Test\TestServiceProvider::class)->register();
return $app;
}
public function setUp(): void
{
parent::setUp();
$this->beginDatabaseTransaction();
}
Missing Documentation
TestCase.php for undocumented methods.assertCustom() exists but isn’t documented, check its usage in the package’s tests.Configuration Overrides
config/test.php doesn’t conflict with defaults:
// Avoid:
'assert_strict' => false, // If package expects true
$this->assertEquals(config('test.assert_strict'), true);
Trait Conflicts
RefreshDatabase). Prefix custom traits:
use Axstrad\Test\Traits\CustomRefreshDatabase as RefreshDatabase;
Mocking Quirks
Mockery:
// Potential issue: Mocks may not respect Laravel’s service container bindings.
$mock = $this->mock(User::class);
$this->assertInstanceOf(User::class, $mock);
dd() or dump() from the package’s TestCase to inspect state:
$this->dumpResponse($response); // Hypothetical helper
phpunit.xml:
<php>
<server name="APP_DEBUG" value="true"/>
</php>
Custom Traits
Extend the package’s TestCase to add domain-specific helpers:
namespace Tests;
use Axstrad\Test\TestCase;
trait AssertsApiTokens
{
public function assertTokenValid($response)
{
$response->assertHeader('Authorization', 'Bearer *');
}
}
class ApiTestCase extends TestCase
{
use AssertsApiTokens;
}
Service Provider Hooks
Bind custom test services in the package’s TestServiceProvider:
public function register()
{
$this->app->singleton(TestService::class, function () {
return new TestService(config('test.custom_setting'));
});
}
Event Listeners
Attach listeners to test lifecycle events (e.g., TestCase::booting()):
TestCase::booting(function () {
if (app()->environment('testing')) {
// Setup shared test data
}
});
self::skipIf() to bypass tests in CI:
public static function skipIf(): bool
{
return app()->environment('ci') && !config('test.run_heavy_tests');
}
TestCase supports parallel runs (check for static state).How can I help you explore Laravel packages today?