Installation
composer require myqee/test --dev
Add to composer.json under require-dev if not auto-installed.
Basic Test File Structure
Place test files in tests/Feature/ (for HTTP tests) or tests/Unit/ (for logic tests).
Example minimal test:
// tests/Feature/ExampleTest.php
use Myqee\Test\TestCase;
class ExampleTest extends TestCase
{
public function test_example()
{
$this->assertTrue(true);
}
}
First Use Case: HTTP Assertions
public function test_homepage_returns_success()
{
$response = $this->get('/');
$response->assertStatus(200);
}
HTTP Testing
route() helper to test named routes.
$this->get(route('login'))->assertStatus(302);
$this->actingAs(User::first())->get('/dashboard');
Database Transactions Automatically rolled back after each test (default behavior). Disable with:
public function setUp(): void
{
parent::setUp();
$this->withoutExceptionHandling();
$this->withoutTransactions();
}
Mocking Services Use Laravel’s built-in mocking:
$this->mock(EmailService::class)->shouldReceive('send')
->once()->with('welcome');
Custom Assertions
Extend TestCase to add reusable assertions:
class CustomTestCase extends TestCase
{
protected function assertJsonHasError($response, $field, $message)
{
$response->assertJsonStructure([$field => [$message]]);
}
}
Laravel Mix/Pest
If using Pest, alias TestCase in pest.php:
uses(Myqee\Test\TestCase::class)->in('Feature');
API Testing
Use ->assertJson() for API responses:
$this->postJson('/api/login', ['email' => 'test@example.com'])
->assertJson(['status' => 'success']);
Event Testing Assert events fired:
$this->assertEventFired(UserRegistered::class);
Queue Testing
Use Queue::fake():
Queue::fake();
$this->post('/send-email');
Queue::assertPushed(SendEmailJob::class);
Missing TestCase Import
Forgetting to extend Myqee\Test\TestCase (not Laravel’s default) will break assertions.
Fix: Always extend use Myqee\Test\TestCase.
Database Seeding
If using DatabaseMigrations trait, ensure php artisan migrate runs before tests.
Tip: Add to phpunit.xml:
<env name="DB_CONNECTION" value="sqlite_memory"/>
Assertion Chaining
Chained assertions (e.g., assertStatus()->assertSee()) may fail silently.
Tip: Use ->assertStatus(200)->assertSee('text') explicitly.
Slow Tests
Avoid withoutTransactions() in CI; use refreshDatabase() instead:
public function setUp(): void
{
parent::setUp();
$this->refreshDatabase();
}
Dump Responses
Use dd($response->getContent()) or dump($response->json()).
Enable Exception Handling
Temporarily disable in setUp():
$this->withoutExceptionHandling();
Log Test Output
Add to phpunit.xml:
<listeners>
<listener class="Illuminate\Foundation\Testing\TestListener">
<arguments>
<object class="Illuminate\Log\TestLogListener"/>
</arguments>
</listener>
</listeners>
Custom Test Traits Create reusable traits:
trait AssertsApiErrors
{
protected function assertApiValidationError($response, $field)
{
$response->assertJsonValidationErrors([$field]);
}
}
Hooks
Override setUp()/tearDown() for shared logic:
protected function setUp(): void
{
parent::setUp();
$this->seed(); // Custom seed
}
Configuration Publish config (if available) with:
php artisan vendor:publish --provider="Myqee\Test\TestServiceProvider"
Note: Package lacks config; check for defaults in TestCase.
How can I help you explore Laravel packages today?