I have a command and some methods related to Redis connection are running in it. I want to test some of these methods and for this I want to mock the Redis connection. I approached it like this:
protected function mockRedis(): void
{
$redisConnection = Mockery::mock(Connection::class);
app()->instance(Connection::class, $redisConnection);
Redis::shouldReceive('connection')
->once()
->andReturn($redisConnection);
Redis::connection()
->shouldReceive('client')
->once()
->andReturnSelf();
Redis::connection()
->client()
->shouldReceive('ping')
->once()
->andReturn(true);
Redis::connection()
->client()
->shouldReceive('close')
->once();
Redis::shouldReceive('subscribe')
->once()
->with(['socket-data'], Mockery::type('callable'))
->andReturnUsing(function ($channels, $callback) {
$message1 = json_encode([
'type' => 'alive',
'interval' => 10,
'time' => '2023-10-04T10:00:00+00:00',
]);
$message2 = json_encode([
'type' => 'some_type',
'data' => 'some_data',
]);
$callback($message1);
$callback($message2);
});
}
The lines I want to test are as follows:
// some openswoole methods...
// ...
Redis::connection()->client()->ping();
// ...
Redis::connection()->client()->close();
// ...
Redis::subscribe(['socket-data'], function (string $message) {
//...
});
Caught error:
1) TestsFeatureRedisSubscribeTest::test_redis_subscribe_command
MockeryExceptionBadMethodCallException: Received Mockery_0_Illuminate_Redis_Connections_Connection::client(), but no expectations were specified
Any idea?
