Installation:
composer require --dev testo/bridge-mockery
Ensure testo/testo is also installed (core dependency).
First Use Case: Mock a class in a Testo test file:
use Testo\Bridge\Mockery\Mockery;
use Testo\Test;
class MyTest extends Test
{
public function testSomething()
{
$mock = Mockery::mock('MyClass');
$mock->shouldReceive('methodName')->once()->andReturn('foo');
$result = $mock->methodName();
$this->assertEquals('foo', $result);
}
}
Key Files:
vendor/testo/bridge-mockery/src/ for core bridge logic.Mocking Dependencies:
// In a Testo test
$mock = Mockery::mock('MyDependency');
$mock->shouldReceive('fetchData')->andReturn(['data']);
$service = new MyService($mock);
Partial Mocking:
$partialMock = Mockery::mock('MyClass[methodToMock]');
$partialMock->methodToMock()->andReturn('partial');
Testo Assertions with Mockery:
$mock = Mockery::mock('MyClass');
$mock->shouldReceive('save')->once();
$this->assertTrue($mock->save()); // Testo assertion
Mockery Verification:
$mock->shouldHaveReceived('save')->once();
$this->assertTrue($mock->save()); // Optional: Combine with Testo assertions
Service Layer Testing: Mock external APIs, databases, or other services to isolate unit tests.
$apiMock = Mockery::mock('GuzzleHttp\Client');
$apiMock->shouldReceive('request')->andReturn(new Response(200, [], '{}'));
Event Listeners/Observers:
$listener = Mockery::mock('App\Listeners\SendEmail');
$listener->shouldReceive('handle')->once();
Laravel-Specific:
If using Laravel, pair with laravel-bridge (if available) or manually mock Facades:
$mock = Mockery::mock('Illuminate\Support\Facades\Cache');
$mock->shouldReceive('get')->andReturn('cached_value');
Mockery vs. Testo Assertions:
Mockery::mock() with Testo’s native mocking (if any). Stick to one.// ❌ Avoid: Mixing frameworks
$this->mock('MyClass'); // Testo syntax (if exists)
Mockery::mock('MyClass'); // Bridge syntax
Autoloading Issues:
composer dump-autoload runs after installation.composer dump-autoload --optimize
Static Method Mocking:
$mock = Mockery::mock('MyClass')->makePartial();
$mock->shouldReceive('staticMethod')->andReturn('static');
Lifetime Management:
// ❌ Bad: Mock persists across tests
$this->onceBeforeAll(function() {
$this->mock = Mockery::mock('MyClass');
});
Verify Mockery is Loaded:
$this->assertTrue(class_exists(Mockery::class), 'Mockery bridge not loaded');
Check for Double Mocking:
$mock = Mockery::mock('MyClass');
$mock->shouldReceive('method')->andReturn('value');
// Later in the same test:
$mock->method(); // Should return 'value'
Testo-Specific Quirks:
setUp()/tearDown(), avoid mocking in setUp() (see Pitfall #4).Mockery::close() in tearDown() to clean up:
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
Custom Matchers:
$mock->shouldReceive('validate')
->with(Mockery::on(function($input) {
return is_array($input) && isset($input['id']);
}))
->andReturn(true);
Mockery Configuration:
use Mockery\Mockery as M;
trait MockeryConfig
{
public function setUp(): void
{
M::getConfiguration()->setStrictMode(true);
parent::setUp();
}
}
Integration with Testo Plugins:
// Example: Hypothetical Testo-Mockery plugin
$this->plugin('testo/mockery')->enable();
How can I help you explore Laravel packages today?