Summer Sale! All accounts are 50% off this week.

Loots's avatar
Level 4

Testing pushRaw

Hi,

I'm building an app where some of the heavy lifting is done via AWS Lambda to make my app more scalable. I'm using AWS SQS to communicate from my Laravel app to AWS Lambda. For this I'm using Queue::connection('sqs')->pushRaw(...) instead of the standard Queue::connection('sqs')->push() mechanism. The problem with this is that I can't figure out how to test this code properly. I can't find a appropriate Queue fake like this: Queue::assertPushedRaw. I might create a pull request for this on the Laravel project but before I do this I must be sure there is no other convenient option that I'm missing.

0 likes
3 replies
LaryAI's avatar
Level 58

One possible solution is to create a custom assertion method in your test case that checks if the raw payload was pushed to the SQS queue. Here's an example:

use Illuminate\Support\Facades\Queue;
use PHPUnit\Framework\Assert;

class ExampleTest extends TestCase
{
    public function testPushRawToSqs()
    {
        Queue::fake();

        $payload = 'some raw payload';
        Queue::connection('sqs')->pushRaw($payload);

        Assert::assertTrue(Queue::connection('sqs')->assertPushed(function ($job) use ($payload) {
            return $job->payload() === $payload;
        }));
    }
}

In this example, we first fake the queue using Queue::fake(). Then we push a raw payload to the SQS queue using Queue::connection('sqs')->pushRaw($payload). Finally, we use Queue::connection('sqs')->assertPushed() to check if the payload was pushed to the queue. The assertPushed() method takes a closure that receives the job instance and should return a boolean indicating if the job matches the expected payload. In this case, we check if the payload of the job is equal to the original payload we pushed.

Note that this solution assumes that you have set up the SQS queue driver correctly in your Laravel app and that you have the necessary AWS credentials and permissions to push messages to the queue.

lucasgbsampaio's avatar

I have the same scenario, did you get something? i tried the AI code but it didn't work.

Loots's avatar
Level 4

@lucasgbsampaio

No, the AI code didn't work for me either. At the moment I'm too busy but I want to create a PR to add this assertion.

Please or to participate in this conversation.