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

Alice Fixtures Bundle Laravel Package

h4cc/alice-fixtures-bundle

Symfony2 bundle for flexible data fixtures using nelmio/alice and Faker. Load fixtures from YAML/PHP, decouple from Doctrine DataFixtures, and optionally drop & recreate Doctrine ORM schema. Supports Doctrine ORM and MongoDB ODM. Work in progress (<1.0).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require h4cc/alice-fixtures-bundle
    

    Add to AppKernel.php under dev/test environments:

    $bundles[] = new h4cc\AliceFixturesBundle\h4ccAliceFixturesBundle();
    
  2. Basic Configuration (config_dev.yml):

    h4cc_alice_fixtures:
        locale: en_US
        seed: 1
        do_flush: true
    
  3. First Fixture (src/Acme/DemoBundle/DataFixtures/Alice/Users.yml):

    App\Entity\User:
        name: '<name>'
        email: '<email>'
    
  4. Load Fixtures:

    php bin/console h4cc_alice_fixtures:load:files src/Acme/DemoBundle/DataFixtures/Alice/Users.yml
    

First Use Case: Quick Data Population

Use the load:files command to populate a database with test data from YAML/PHP files. Ideal for:

  • Local development environments.
  • Testing scenarios where consistent data is needed.
  • Rapid prototyping.

Example:

php bin/console h4cc_alice_fixtures:load:files src/Acme/DemoBundle/DataFixtures/Alice/Users.yml src/Acme/DemoBundle/DataFixtures/Alice/Posts.yml

Implementation Patterns

Workflow: Fixture Management

  1. Define Fixtures:

    • Use YAML (recommended) or PHP files in DataFixtures/Alice/ directory.
    • Example (Users.yml):
      App\Entity\User:
          name: '<firstName> <lastName>'
          email: '<email>'
          roles: ['ROLE_USER']
      
  2. Load Fixtures Programmatically:

    $manager = $this->get('h4cc_alice_fixtures.manager');
    $objects = $manager->loadFiles([__DIR__.'/Users.yml']);
    $manager->persist($objects, $dropSchema = false);
    
  3. Use FixtureSets for Complex Scenarios:

    $set = $manager->createFixtureSet();
    $set->addFile(__DIR__.'/Users.yml');
    $set->addFile(__DIR__.'/Posts.yml');
    $set->setLocale('de_DE');
    $set->setDoDrop(true); // Drops and recreates schema
    $manager->load($set);
    
  4. Autoload FixtureSets: Place FixtureSet classes in DataFixtures/Alice/ with Set.php suffix. Load all with:

    php bin/console h4cc_alice_fixtures:load:sets
    

Integration Tips

  1. Testing: Load fixtures in setUp() for PHPUnit tests:

    public function setUp(): void {
        $client = static::createClient();
        $manager = $client->getContainer()->get('h4cc_alice_fixtures.manager');
        $manager->load(require(__DIR__.'/Fixtures/TestSet.php'));
    }
    
  2. Custom Providers: Extend Faker with custom providers (e.g., for domain-specific data):

    services:
        app.faker.provider:
            class: App\Fixtures\CustomFakerProvider
            tags:
                - { name: h4cc_alice_fixtures.provider }
    

    Use in fixtures:

    App\Entity\Product:
        name: '<customProvider()->generateName()>'
    
  3. Processors: Modify entities before/after persistence:

    services:
        app.alice.processor:
            class: App\Fixtures\Processor\EncryptEmailProcessor
            tags:
                - { name: h4cc_alice_fixtures.processor }
    
  4. Multi-Database Support: Configure multiple managers in config_dev.yml:

    h4cc_alice_fixtures:
        managers:
            orm:
                doctrine: orm
            mongodb:
                doctrine: mongodb-odm
    

    Access via:

    $ormManager = $this->get('h4cc_alice_fixtures.orm_manager');
    $mongoManager = $this->get('h4cc_alice_fixtures.mongodb_manager');
    

Gotchas and Tips

Pitfalls

  1. Schema Drop Warning:

    • setDoDrop(true) drops all tables managed by Doctrine. Use only in dev/test environments.
    • Avoid in production or shared databases.
  2. Circular References:

    • Fixtures loaded via load:sets share a global reference pool. Circular references (e.g., User referencing Post which references User) will fail.
    • Solution: Load dependent fixtures in the correct order or use loadFiles() for isolated loads.
  3. Faker Seed Consistency:

    • Set a fixed seed for reproducible data:
      h4cc_alice_fixtures:
          seed: 42
      
    • Override per FixtureSet:
      $set->setSeed(123);
      
  4. Performance:

    • Loading fixtures with do_flush: true can be slow for large datasets. Disable flushing if not needed:
      h4cc_alice_fixtures:
          do_flush: false
      
  5. Bundle Order:

    • FixtureSets are loaded in AppKernel bundle order. Use setOrder() to customize:
      $set->setOrder(10); // Load after default order (1)
      

Debugging Tips

  1. Verify Fixture Loading:

    • Check loaded objects before persistence:
      $objects = $manager->loadFiles([$file]);
      dump(count($objects)); // Debug count
      
  2. Locale Issues:

    • Ensure Faker locale matches your fixtures (e.g., de_DE for German names):
      h4cc_alice_fixtures:
          locale: de_DE
      
  3. Processor Debugging:

    • Add dump() in custom processors to inspect entity states:
      public function prePersist($entity) {
          dump($entity); // Debug before persistence
      }
      
  4. Schema Drop Errors:

    • If schema drop fails, check:
      • Doctrine connection permissions.
      • Custom schema_manager service configuration.

Extension Points

  1. Custom Loaders:

    • Extend Alice loaders by patching the h4cc_alice_fixtures.loader.factory service. Example:
      services:
          custom_loader_factory:
              class: App\Fixtures\CustomLoaderFactory
              decorates: h4cc_alice_fixtures.loader.factory
      
  2. Dynamic Fixture Generation:

    • Use FixtureSets to generate data dynamically:
      $set = $manager->createFixtureSet();
      for ($i = 0; $i < 10; $i++) {
          $set->addFile(__DIR__.'/dynamic/Post_'.$i.'.yml');
      }
      
  3. Conditional Loading:

    • Load fixtures conditionally in commands/tests:
      if ($this->getEnvironment() === 'test') {
          $manager->load($set);
      }
      
  4. MongoDB-Specific Quirks:

    • For MongoDB-ODM, ensure doctrine: mongodb-odm is set and collections are properly mapped. Example config:
      h4cc_alice_fixtures:
          managers:
              mongodb:
                  doctrine: mongodb-odm
                  schema_tool: ~ # MongoDB doesn't need schema drops
      
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.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony