carlescliment/chained-stubs-bundle
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,
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()
Where to Look First
src/BladeTesterServiceProvider.php for core registration.src/BladeTester.php for API methods.tests/ for usage patterns.Stubbing Chained Methods
// Stub a chain: `method1()->method2()->method3()`
BladeTester::stub('method1')
->with('arg1')
->method('method2')
->with('arg2')
->returns('mocked_result');
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);
}
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());
Partial Stubbing Stub only specific parts of a chain:
BladeTester::stub('order')
->method('items')
->method('first')
->returns(['name' => 'Test']);
BladeTester::stub('checkStatus')->returns(function ($status) {
return $status === 'active' ? 'Active' : 'Inactive';
});
BladeTester::reset();
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');
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(...);
Global State
Stubs persist across tests. Use BladeTester::reset() to avoid test pollution.
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
BladeTester::stubbed() to list active stubs:
dd(BladeTester::stubbed());
BladeTester::debug(true);
setUp():
public function setUp(): void
{
BladeTester::reset();
parent::setUp();
}
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
});
});
Integration with Mockery Combine with Mockery for complex objects:
$mock = Mockery::mock('alias:User');
$mock->shouldReceive('name')->andReturn('Mocked');
view()->share('user', $mock);
Laravel Mix/Blade Compilation Ensure Blade templates are recompiled after stub changes:
php artisan view:clear
How can I help you explore Laravel packages today?