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

Fixtures Bundle Laravel Package

alexislefebvre/fixtures-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require liip/functional-test-bundle
    

    Ensure config_test.yml includes the bundle in bundles and configures the session and profiler sections as shown in the docs.

  2. Extend Base Class: Replace Symfony\Bundle\FrameworkBundle\Test\WebTestCase with:

    use Liip\FunctionalTestBundle\Test\WebTestCase;
    
  3. First Test Case:

    class MyTest extends WebTestCase
    {
        public function testHomepage()
        {
            $client = $this->makeClient();
            $crawler = $client->request('GET', '/');
            $this->assertStatusCode(200, $client);
        }
    }
    

Key First Use Cases

  • Authenticated Requests: Use makeClient(true) with config_test.yml credentials or pass credentials directly.
  • Fixtures: Load test data via loadFixtures() (e.g., LoadUserData).
  • Query Monitoring: Enable in config_test.yml to track excessive queries.

Implementation Patterns

Fixture Loading Workflows

  1. Basic Fixture Loading:

    $this->loadFixtures([
        'AppBundle\DataFixtures\ORM\LoadUserData',
        'AppBundle\DataFixtures\ORM\LoadProductData'
    ]);
    
  2. Dependency Injection in Fixtures: Use services in fixtures by declaring them in config.yml:

    services:
        my_fixture_service:
            class: AppBundle\Service\MyFixtureService
    

    Then inject via constructor in your fixture class.

  3. Alice Fixtures: Create YAML files (e.g., user.yml) and load them:

    $this->loadFixtures(['AppBundle\DataFixtures\ORM\user.yml']);
    

Authentication Patterns

  1. Config-Driven Login:

    alexis_lefebvre_fixtures:
        authentication:
            username: "testuser"
            password: "password"
    

    Then use:

    $client = $this->makeClient(true); // Auto-logged in
    
  2. Dynamic Login:

    $credentials = ['username' => 'user', 'password' => 'pass'];
    $client = $this->makeClient($credentials);
    
  3. Fixture-Based Login:

    $fixtures = $this->loadFixtures(['LoadUserData'])->getReferenceRepository();
    $this->loginAs($fixtures->getReference('user_ref'), 'main');
    $client = $this->makeClient();
    

Query and Response Handling

  1. Assertions:

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

    $crawler = $this->fetchCrawler('/path');
    $content = $this->fetchContent('/path');
    
  3. Routing:

    $url = $this->getUrl('route_name', ['param' => 'value']);
    

Unit Testing with Mocks

  1. Service Mocking:
    $mock = $this->getServiceMockBuilder('MyService')->getMock();
    $mock->expects($this->once())->method('doSomething');
    

Gotchas and Tips

Common Pitfalls

  1. Session Storage Errors:

    • Error: Missing session.storage.options#name.
    • Fix: Add name: MOCKSESSID under framework.session in config_test.yml.
  2. Query Counter Conflicts:

    • Issue: @QueryCount breaks PHPUnit annotations (e.g., @dataProvider).
    • Fix: Use @IgnoreAnnotation:
      /**
       * @IgnoreAnnotation("dataProvider")
       */
      
  3. Fixture Dependencies:

    • Issue: Fixtures failing due to missing references.
    • Fix: Ensure fixtures are loaded in the correct order or use getReferenceRepository() to access loaded data.
  4. HTTP Basic Auth:

    • Tip: Configure security.firewalls in config_test.yml for seamless auth:
      security:
          firewalls:
              main:
                  http_basic: ~
      

Debugging Tips

  1. Inspect Fixtures: Use getReferenceRepository() to debug loaded fixtures:

    $fixtures = $this->loadFixtures(['LoadUserData'])->getReferenceRepository();
    dump($fixtures->getReferences());
    
  2. Query Logging: Enable Symfony’s profiler (profiler.enabled: true) to inspect queries during tests.

  3. Client State: Reset client state between tests if needed:

    $client->getContainer()->get('session')->invalidate();
    

Extension Points

  1. Custom Fixture Providers: Extend Alice’s providers for reusable fixture logic:

    # user_with_custom_provider.yml
    App\DataFixtures\Faker\Provider\CustomProvider:
        methods: ['customUser']
    
  2. Query Count Overrides: Use @QueryCount annotations for test-specific limits:

    /**
     * @QueryCount(100)
     */
    public function testHighQueryPage() { ... }
    
  3. Custom Assertions: Extend WebTestCase to add domain-specific assertions:

    class CustomTestCase extends WebTestCase {
        protected function assertPageContains($text) {
            $content = $this->fetchContent('/');
            $this->assertContains($text, $content);
        }
    }
    

Performance Tips

  1. Parallel Testing: Use paratest or fastest for faster test suites (see doc/paratest.md).

  2. Fixture Caching: Cache fixtures in phpunit.xml:

    <php>
        <env name="FIxturesBundle_CACHE" value="true"/>
    </php>
    
  3. Selective Fixture Loading: Load only necessary fixtures per test to reduce overhead.

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