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

Foundry Laravel Package

zenstruck/foundry

Zenstruck Foundry is a Laravel-friendly factory and fixtures toolkit for building and persisting test data. Define model factories, create realistic related objects, seed databases, and write cleaner, faster tests with helpers for states, Faker, and repositories.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strengths:

    • Test Data Generation: Continues to align perfectly with Laravel’s testing ecosystem, now with explicit handling of Doctrine events in unit tests via withoutDoctrineEvents. This reduces flakiness in isolated unit tests where Doctrine events (e.g., prePersist, postLoad) might interfere.
    • Symfony/Doctrine Compatibility: The fix for withoutDoctrineEvents demonstrates Foundry’s attention to edge cases in Symfony/Doctrine integration, reinforcing its suitability for Laravel’s Eloquent-based applications.
    • Auto-Completion: Remains a key strength, though the new release does not introduce changes to this feature. The core value proposition of reducing manual fixture setup persists.
    • State Management: No changes, but the existing stateful approach remains robust for complex test scenarios.
    • Performance: The fix is non-intrusive and targets a specific edge case, preserving Foundry’s optimized on-demand generation.
  • Potential Gaps:

    • Laravel-Specific Features: Still requires customization for Laravel-specific traits (e.g., HasFactory, Faker integration). The new release does not address this.
    • Migration Testing: No updates to migration testing support. Integration with tools like laravel-shift/database remains manual.
    • Non-Doctrine Models: Unchanged; Foundry’s auto-completion may not apply to non-Eloquent models.

Integration Feasibility

  • High: The new release is a minor fix and does not alter the core integration approach. Foundry remains a natural fit for Laravel’s Eloquent/Doctrine stack.

    • Event Handling: The withoutDoctrineEvents fix is particularly valuable for unit tests where Doctrine events could introduce unintended side effects. This aligns with Laravel’s testing best practices (e.g., isolating unit tests from ORM behavior).
    • Backward Compatibility: The change is a bug fix and does not introduce breaking changes, reducing migration risk.
  • Customization Needs:

    • Event Disabling: Developers may need to explicitly use withoutDoctrineEvents in unit tests to avoid event-related issues. This could require updating test suites to opt into this behavior where needed.
    • Laravel-Specific Wrappers: No changes to the need for custom traits/facades to bridge Laravel-specific features (e.g., Faker seeds).

Technical Risk

  • Low:
    • Dependency Risk: The fix is minor and targeted, with no changes to Foundry’s core architecture or API. The package remains MIT-licensed and actively maintained.
    • Learning Curve: Unchanged; developers familiar with Laravel’s Factory will adapt to Foundry’s stateful paradigm with minimal additional effort.
    • Performance: No impact on performance; the fix addresses a specific edge case without altering the underlying generation logic.
    • Tooling: No changes to IDE support or autocompletion.

Key Questions

  1. Fixture Scope: Unchanged – Will Foundry replace all Laravel factories, or coexist with them?
  2. State Management: Unchanged – How will stateful fixtures integrate with Laravel’s RefreshDatabase or MigrateFresh?
  3. Non-Doctrine Models: Unchanged – Are there non-Eloquent models requiring fixture support?
  4. Testing Workflow: Updated – Should withoutDoctrineEvents be enabled by default in unit tests, or only opt-in? How will this interact with Laravel’s DatabaseTransactions or RefreshDatabase?
  5. Performance: Unchanged – Are there critical paths where auto-completion could slow down test suites?
  6. Migration Testing: Unchanged – How will Foundry integrate with migration testing?
  7. Team Adoption: Unchanged – What training is needed for Foundry’s stateful paradigm?
  8. Doctrine Events: New – How should teams leverage withoutDoctrineEvents in unit tests? Should this be wrapped in a Laravel-specific helper (e.g., unitTest() trait)?
  9. Event-Driven Fixtures: New – Are there fixtures that intentionally rely on Doctrine events (e.g., for testing event listeners)? How will withoutDoctrineEvents affect these?

Integration Approach

Stack Fit

  • Core Stack:

    • PHP/Laravel: No changes to compatibility. Foundry’s PHP 8.1+ support remains aligned with Laravel’s LTS versions.
    • Doctrine/Eloquent: The withoutDoctrineEvents fix improves compatibility for unit tests, where Doctrine events are often undesirable.
    • Testing Frameworks: Native integration with PHPUnit/Pest remains unchanged.
  • Compatibility:

    • Pros:
      • The fix reduces flakiness in unit tests by disabling Doctrine events when explicitly requested.
      • Continued support for Laravel’s HasFactory trait via custom wrappers.
    • Cons:
      • Event Handling: Developers must now explicitly opt into disabling Doctrine events, which could lead to inconsistent behavior if not standardized across the codebase.
      • Laravel-Specific Features: No updates to Faker integration or other Laravel-specific tools.

Migration Path

  1. Phase 1: Pilot Fixtures Unchanged
    • Replace 1–2 critical factory classes with Foundry equivalents.
    • Validate auto-completion and state management.
  2. Phase 2: Full Replacement Unchanged
    • Register all factories via Foundry’s Loader.
    • Deprecate old Factory classes.
  3. Phase 3: Advanced Features Updated
    • Doctrine Events: Introduce a Laravel-specific helper to standardize withoutDoctrineEvents usage in unit tests. Example:
      // In a custom test trait
      trait UnitTestTrait {
          protected function withoutDoctrineEvents(): void {
              Foundry::withoutDoctrineEvents();
          }
      }
      
      Use this trait in unit test classes to ensure consistent event handling.
    • Implement stateful test scenarios and integrate with RefreshDatabase.

Compatibility

  • Laravel-Specific Adjustments:

    • Doctrine Events: Create a macro or trait to simplify withoutDoctrineEvents usage in unit tests. Example:
      // app/Traits/UnitTestTrait.php
      use Zenstruck\Foundry\Test\TestCase;
      
      trait UnitTestTrait {
          public function setUp(): void {
              parent::setUp();
              if ($this instanceof TestCase && $this->usesUnitTests()) {
                  Foundry::withoutDoctrineEvents();
              }
          }
      }
      
    • Macros: No changes needed for existing Laravel-specific macros.
    • Testing Helpers: Wrap Foundry in a trait for Laravel’s test case methods (e.g., createModel()).
  • Tooling:

    • IDE Support: No changes required.
    • CI/CD: Update test pipelines to include withoutDoctrineEvents in unit test phases if needed.

Sequencing

  1. Pre-Integration: Unchanged
    • Audit factories for complexity and Laravel-specific dependencies.
  2. Integration: Updated
    • Step 1: Replace factories with Foundry equivalents.
    • Step 2: Introduce withoutDoctrineEvents in unit tests. Start with a small subset of tests to validate behavior.
    • Step 3: Gradually roll out the new trait/macro for standardizing event handling.
  3. Post-Integration: Unchanged
    • Deprecate old factories.
    • Optimize slow fixtures.
    • Document Foundry-specific patterns.

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: Unchanged; Foundry’s declarative syntax remains a maintenance advantage.
    • Centralized Definitions: Unchanged; fixtures are defined in one place.
    • State Reusability: Unchanged; encourages DRY test scenarios.
  • Cons:
    • Learning Curve: Updated – Developers must now understand when to use withoutDoctrineEvents, adding a minor cognitive load for unit tests.
    • Tooling Gaps: Updated – Limited Laravel-specific tooling for Foundry (e.g., no built-in support for withoutDoctrineEvents in Laravel’s test case base class). A custom trait/macro will be needed.
    • Migration Debt: Unchanged; replacing factories may still require test updates.

Support

  • Developer Onboarding:

    • Updated – Add documentation for withoutDoctrineEvents, including:
      • When to use it (e.g., unit tests vs. integration tests).
      • How to integrate it with Laravel’s test case setup.
      • Example:
        ## Unit Test Best Practices with Foundry
        To avoid Doctrine event interference in unit tests, use:
        ```php
        use App\Traits\UnitTestTrait;
        
        class UserTest extends TestCase {
            use UnitTestTrait;
            // Events are automatically disabled in setUp()
        }
        
    • Provide a migration guide for updating existing unit tests to use withoutDoctrineEvents.
  • Debugging:

    • Updated – Add logging or assertions to verify withoutDoctrineEvents is working as expected. Example:
      $this->assertFalse(Foundry::hasDoctrineEventsEnabled());
      
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