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

Chained Stubs Bundle Laravel Package

carlescliment/chained-stubs-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require carlescliment/chained-stubs-bundle
    

    Register the bundle in config/app.php under providers:

    CarlesCliment\BladeTester\BladeTesterServiceProvider::class,
    
  2. First Use Case: Stubbing Chained Blade Methods Use the stub() helper to mock chained Blade method calls in tests:

    use CarlesCliment\BladeTester\BladeTester;
    
    // In your test setup
    BladeTester::stub('html')->with('content')->returns('<div>Mocked</div>');
    
    // In your Blade template
    @html('content')->with('extra')->toArray()
    
  3. Where to Look First

    • Service Provider: src/BladeTesterServiceProvider.php for core registration.
    • Facade: src/BladeTester.php for API methods.
    • Test Examples: Check tests/ for usage patterns.

Implementation Patterns

Common Workflows

  1. Stubbing Chained Methods

    // Stub a chain: `method1()->method2()->method3()`
    BladeTester::stub('method1')
        ->with('arg1')
        ->method('method2')
        ->with('arg2')
        ->returns('mocked_result');
    
  2. Dynamic Stubbing in Tests

    public function test_chained_methods()
    {
        BladeTester::stub('user')->method('name')->returns('John Doe');
    
        $result = view('user.profile')->render();
        $this->assertStringContainsString('John Doe', $result);
    }
    
  3. Integration with Laravel Views Use with View::make() or @include directives:

    BladeTester::stub('getData')->returns(['key' => 'value']);
    $view = View::make('template');
    $this->assertEquals(['key' => 'value'], $view->getData());
    
  4. Partial Stubbing Stub only specific parts of a chain:

    BladeTester::stub('order')
        ->method('items')
        ->method('first')
        ->returns(['name' => 'Test']);
    

Advanced Patterns

  • Conditional Stubs: Use closures for dynamic returns:
    BladeTester::stub('checkStatus')->returns(function ($status) {
        return $status === 'active' ? 'Active' : 'Inactive';
    });
    
  • Reset Stubs: Clear all stubs between tests:
    BladeTester::reset();
    

Gotchas and Tips

Pitfalls

  1. Method Order Sensitivity Chained stubs must match the exact call order in Blade. Reordering breaks stubs:

    // Fails if Blade calls `method2()` before `method1()`
    BladeTester::stub('method1')->method('method2');
    
  2. Argument Mismatches Stubbed methods must match arguments (type/value) or they won’t trigger:

    // Fails if Blade passes `['id' => 1]` instead of `1`
    BladeTester::stub('getUser')->with(1)->returns(...);
    
  3. Global State Stubs persist across tests. Use BladeTester::reset() to avoid test pollution.

  4. Blade Directives vs. PHP Methods The package targets PHP method chains, not raw Blade directives (e.g., @if). Use PHP helpers like:

    @php echo $user->name()->toUpper(); @endphp
    

Debugging Tips

  • Verify Stubbed Calls: Use BladeTester::stubbed() to list active stubs:
    dd(BladeTester::stubbed());
    
  • Check Call Stack: Enable debug mode to log unmatched method calls:
    BladeTester::debug(true);
    
  • Test Isolation: Reset stubs in setUp():
    public function setUp(): void
    {
        BladeTester::reset();
        parent::setUp();
    }
    

Extension Points

  1. Custom Stub Logic Extend the CarlesCliment\BladeTester\Stub class to add pre/post-call hooks:

    BladeTester::extend(function ($stub) {
        $stub->onCall(function () {
            // Pre-call logic
        });
    });
    
  2. Integration with Mockery Combine with Mockery for complex objects:

    $mock = Mockery::mock('alias:User');
    $mock->shouldReceive('name')->andReturn('Mocked');
    view()->share('user', $mock);
    
  3. Laravel Mix/Blade Compilation Ensure Blade templates are recompiled after stub changes:

    php artisan view:clear
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky