Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Functional Test Bundle Laravel Package

apnet/functional-test-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

First Steps

  1. 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,
    
  2. 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).

  3. 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
    
  4. Key Features to Explore

    • HTTP Client: Check if the bundle provides a custom HTTP client or helpers.
    • Database Transactions: Verify if tests run in transactions by default.
    • Authentication: Look for built-in auth helpers (e.g., actingAs()).
    • Assertions: Check if custom assertions are available (e.g., assertSeeInDatabase()).

Implementation Patterns

Common Workflows

  1. 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
    }
    
  2. 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]);
    }
    
  3. 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());
    }
    
  4. 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');
        });
    }
    
  5. Integration with Laravel Mix/Pest If using Pest, extend the TestCase in pest.php:

    uses(Apnet\FunctionalTestBundle\TestCase::class)->in('Feature');
    

Integration Tips

  • Service Containers: If the bundle provides mocking helpers, use them to isolate services:
    $this->mock(Service::class, function ($mock) {
        $mock->shouldReceive('process')->andReturn(true);
    });
    
  • Event Testing: Listen for events during tests:
    Event::fake();
    $this->post('/trigger-event');
    Event::assertDispatched(UserRegistered::class);
    
  • Queue Testing: Pause queues and assert jobs:
    Queue::fake();
    $this->post('/dispatch-job');
    Queue::assertPushed(ProcessPayment::class);
    

Gotchas and Tips

Pitfalls

  1. Archived Status The package is archived, so:

    • Expect no active maintenance (bug fixes, updates).
    • Check for alternatives (e.g., Laravel’s built-in Http\Testing\TestResponse or spatie/laravel-test-factory).
    • Fork the repo if critical features are missing.
  2. Lack of Documentation

    • Assume minimal built-in features (e.g., no custom assertions or advanced mocking).
    • Rely on Laravel’s native testing tools for most use cases.
  3. Configuration Quirks

    • If the bundle publishes configs, verify they don’t override Laravel’s defaults unintentionally.
    • Check for hardcoded paths or service bindings that might conflict.
  4. Database Transactions

    • If tests don’t use transactions by default, add this to setUp():
      $this->beginDatabaseTransaction();
      
    • Rollback manually if needed:
      $this->rollBackDatabaseTransaction();
      
  5. Authentication Issues

    • If actingAs() fails, ensure the bundle doesn’t override Laravel’s auth system.
    • Fall back to manual token generation:
      $user = User::factory()->create();
      $this->withHeaders(['Authorization' => 'Bearer ' . $user->createToken('test')->plainTextToken]);
      

Debugging Tips

  1. Log Output Enable Laravel’s debug mode and check logs:

    $this->withoutExceptionHandling(); // Temporarily disable exception skipping
    

    Or use:

    php artisan test --verbose
    
  2. 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()
    
  3. Isolation

    • Run tests in parallel if using PHPUnit:
      php artisan test --parallel
      
    • Use --filter to run specific test classes/methods:
      php artisan test --filter="test_user_can_login"
      
  4. 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));
    }
    

Extension Points

  1. 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']);
        }
    }
    
  2. Service Providers Bind custom test services in the bundle’s provider:

    $this->app->bind(TestService::class, function () {
        return new TestService(config('testing.custom_setting'));
    });
    
  3. 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;
        }
    }
    
  4. Event Listeners Add listeners for test lifecycle events (if the bundle supports them):

    Event::listen('testing.started', function () {
        // Setup before all tests
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui