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

Test Bundle Laravel Package

alexislefebvre/test-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require liip/functional-test-bundle
    

    Register the bundle in config/bundles.php:

    return [
        // ...
        Liip\FunctionalTestBundle\LiipFunctionalTestBundle::class => ['test' => true],
    ];
    
  2. Configure config/packages/test/alexis_lefebvre_test.yaml:

    alexis_lefebvre_test:
        authentication:
            username: "test_user"
            password: "test_pass"
    
  3. Extend WebTestCase:

    use Liip\FunctionalTestBundle\Test\WebTestCase;
    
    class MyTest extends WebTestCase { ... }
    

First Use Case

Test a controller with pre-loaded fixtures and auto-login:

public function testHomepage()
{
    $this->loadFixtures(['AppBundle\DataFixtures\ORM\LoadUserData']);
    $client = $this->makeClient(); // Auto-logged in via config
    $crawler = $client->request('GET', '/');
    $this->assertStatusCode(200, $client);
}

Implementation Patterns

1. Fixture Workflows

  • Load Fixtures:
    $this->loadFixtures([
        'AppBundle\DataFixtures\ORM\LoadUsers',
        'AppBundle\DataFixtures\ORM\LoadPosts'
    ]);
    
  • Access Fixture References:
    $fixtures = $this->loadFixtures(['LoadUsers'])->getReferenceRepository();
    $user = $fixtures->getReference('user_alpha');
    

2. Authentication Patterns

  • Auto-Login via Config:

    alexis_lefebvre_test:
        authentication:
            username: "admin"
            password: "secret"
    
    $client = $this->makeClient(); // Auto-logged in
    
  • Dynamic Login:

    $this->loginAs($fixtures->getReference('user_beta'), 'main');
    $client = $this->makeClient();
    
  • HTTP Basic Auth (Recommended):

    security:
        firewalls:
            main:
                http_basic: ~
    

3. Test Helpers

  • Assertions:

    $this->assertStatusCode(200, $client);
    $this->assertValidationErrors(['field.name'], $client->getContainer());
    
  • Crawler/Content:

    $crawler = $this->fetchCrawler('/profile');
    $content = $this->fetchContent('/dashboard');
    
  • Routing:

    $url = $this->getUrl('app_home', ['_locale' => 'en']);
    

4. Unit Testing with Mocks

$mockService = $this->getServiceMockBuilder('AppBundle\Service\FooService')
    ->disableOriginalConstructor()
    ->getMock();

$mockService->expects($this->once())
    ->method('doSomething')
    ->willReturn('mocked');

5. Query Optimization

  • Config-Based Limit:
    alexis_lefebvre_test:
        query:
            max_query_count: 10
    
  • Per-Test Override:
    /**
     * @QueryCount(50)
     */
    public function testComplexPage() { ... }
    

Gotchas and Tips

Pitfalls

  1. Session Storage Error:

    • Symptom: Missing session.storage.options#name error.
    • Fix: Add to config/packages/test/framework.yaml:
      session:
          name: MOCKSESSID
      
  2. Query Counter Conflicts:

    • Symptom: @dataProvider or @depends annotations break.
    • Fix: Add @IgnoreAnnotation:
      /**
       * @IgnoreAnnotation("dataProvider")
       * @IgnoreAnnotation("depends")
       */
      
  3. Fixture Dependencies:

    • Tip: Use orderBy: [ { "LoadUsers", "LoadPosts" } ] in LoadUserData.php to enforce load order.

Debugging Tips

  • Inspect Fixtures:

    $fixtures = $this->loadFixtures(['LoadUsers']);
    dump($fixtures->getReferenceRepository()->getReferences());
    
  • Log Client Requests:

    $client = $this->makeClient();
    $client->enableProfiler(); // Enable Symfony profiler
    
  • Disable Query Counter Temporarily:

    alexis_lefebvre_test:
        query:
            enabled: false
    

Extension Points

  1. Custom Fixture Providers:

    • Extend Faker\Provider\Base and register in config/packages/test/alexis_lefebvre_test.yaml:
      alexis_lefebvre_test:
          fixtures:
              providers:
                  - AppBundle\DataFixtures\Faker\Provider\CustomProvider
      
  2. Override WebTestCase:

    • Extend the base class to add custom assertions or helpers:
      class CustomWebTestCase extends WebTestCase {
          public function assertContainsText(string $text, string $content) { ... }
      }
      
  3. Parallel Testing:

    • Use paratest or fastest with:
      alexis_lefebvre_test:
          parallel:
              enabled: true
      

Performance Quirks

  • Fixture Loading:

    • Load only necessary fixtures per test to avoid slowdowns.
    • Use purgeDatabase() between tests if needed:
      $this->purgeDatabase();
      
  • Query Counter Overhead:

    • Disable for non-critical tests:
      $this->disableQueryCounter();
      
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware