apnet/functional-test-bundle
Installation Add the package via Composer:
composer require apnet/functional-test-bundle
Register the bundle in config/app.php under providers:
Apnet\FunctionalTestBundle\ApnetFunctionalTestBundle::class,
Basic Setup Publish the default configuration (if available) and service providers:
php artisan vendor:publish --provider="Apnet\FunctionalTestBundle\ApnetFunctionalTestBundle"
Check config/functional_test.php for available configurations (if any).
First Test Case
Create a functional test class extending Apnet\FunctionalTestBundle\TestCase:
use Apnet\FunctionalTestBundle\TestCase;
class UserLoginTest extends TestCase
{
public function test_user_can_login()
{
$response = $this->post('/login', ['email' => 'test@example.com', 'password' => 'password']);
$this->assertEquals(200, $response->getStatusCode());
}
}
Run tests with:
php artisan test
Key Features to Explore
actingAs()).assertSeeInDatabase()).Test Setup and Teardown
Use setUp() and tearDown() to prepare/test environments:
protected function setUp(): void
{
parent::setUp();
$this->artisan('migrate'); // Example: Run migrations before tests
}
Database Testing If the bundle supports database seeding or factories, leverage them:
public function test_user_creation()
{
$user = User::factory()->create();
$response = $this->post('/users', $user->toArray());
$this->assertDatabaseHas('users', ['email' => $user->email]);
}
API Testing Use the HTTP client for API endpoints:
public function test_api_endpoint()
{
$response = $this->json('GET', '/api/users', [], [
'HTTP_Authorization' => 'Bearer token_here',
]);
$this->assertJson($response->getContent());
}
Feature Testing
Combine with Laravel’s built-in testing tools (e.g., browsingTest):
use Laravel\Dusk\Browser;
public function test_user_flow()
{
$browser = Browser::task(function () {
$this->visit('/login')
->type('email', 'test@example.com')
->type('password', 'password')
->press('Login')
->assertPathIs('/dashboard');
});
}
Integration with Laravel Mix/Pest
If using Pest, extend the TestCase in pest.php:
uses(Apnet\FunctionalTestBundle\TestCase::class)->in('Feature');
$this->mock(Service::class, function ($mock) {
$mock->shouldReceive('process')->andReturn(true);
});
Event::fake();
$this->post('/trigger-event');
Event::assertDispatched(UserRegistered::class);
Queue::fake();
$this->post('/dispatch-job');
Queue::assertPushed(ProcessPayment::class);
Archived Status The package is archived, so:
Http\Testing\TestResponse or spatie/laravel-test-factory).Lack of Documentation
Configuration Quirks
Database Transactions
setUp():
$this->beginDatabaseTransaction();
$this->rollBackDatabaseTransaction();
Authentication Issues
actingAs() fails, ensure the bundle doesn’t override Laravel’s auth system.$user = User::factory()->create();
$this->withHeaders(['Authorization' => 'Bearer ' . $user->createToken('test')->plainTextToken]);
Log Output Enable Laravel’s debug mode and check logs:
$this->withoutExceptionHandling(); // Temporarily disable exception skipping
Or use:
php artisan test --verbose
DD() in Tests
Use dump() or dd() sparingly in tests (they may not work as expected in CLI):
$this->dump($this->response->getContent()); // Alternative to dd()
Isolation
php artisan test --parallel
--filter to run specific test classes/methods:
php artisan test --filter="test_user_can_login"
Custom Assertions
If the bundle lacks assertions, create helper methods in TestCase:
protected function assertResponseContainsJson($key, $value)
{
$response = $this->response;
$this->assertTrue($response->contains($key), "Response missing key: {$key}");
$this->assertEquals($value, $response->json($key));
}
Custom Test Cases
Extend the base TestCase for shared logic:
class ApiTestCase extends Apnet\FunctionalTestBundle\TestCase
{
protected function createTestUser()
{
return User::factory()->create(['role' => 'tester']);
}
}
Service Providers Bind custom test services in the bundle’s provider:
$this->app->bind(TestService::class, function () {
return new TestService(config('testing.custom_setting'));
});
Traits Use traits for reusable test logic (e.g., auth, data setup):
trait AuthenticatesUsers
{
protected function authenticateAsTester()
{
$user = User::factory()->create();
$this->actingAs($user);
return $user;
}
}
Event Listeners Add listeners for test lifecycle events (if the bundle supports them):
Event::listen('testing.started', function () {
// Setup before all tests
});
How can I help you explore Laravel packages today?