Corbin's avatar

Is there a way to test Redis Pub method with PHPunit?

I would like to test what is stored in the redis pub method using PHPUnit, but I have no idea where to start.

Controller

class MessageController extends Controller
{

    public function store(Conversation $conversation, Request $request)
    {
        $user = Auth::user();

        $message = Message::create([
            'body' => $request->input('message'),
            'conversation_id' => $conversation->id,
            'sender_id' => $user->id,
            'type' => 'user_message'
        ]);
             
        $redis = Redis::connection();
            
        $data = new MessageResource($message);

        $redis->publish('message', $data);

    }
}

Here's the test

/** @test */
public function a_user_can_send_a_message()
{
    $this->actingAs($user = User::factory()->create());

    $message = Message::factory()->make(['sender_id' => $user->id]);

    $response = $this->json('POST', '/api/message/'. $conversation->id, ['message' => $message->body])
        ->assertStatus(201);
    
    $response->assertJsonStructure([
        'data' => [
            'body',
            'sender',
        ]
    ]);
}

I'm basically trying to test this function $redis->publish('message', $data);. Anyone know how I can do this?

0 likes
1 reply
cayes26336's avatar

I think so.

If you are unable to install the phpredis extension, you may install the predis/predis package via Composer. Predis is a Redis client written entirely in PHP and does not require any additional extensions

Please or to participate in this conversation.