ssi-anik / amqp

php-amqplib wrapper that eases the consumption of RabbitMQ. A painless way of using RabbitMQ
https://packagist.org/packages/anik/amqp
MIT License
134 stars 23 forks source link

Create AmqpManagerFake for Testing Purposes #21

Closed stefnats closed 2 years ago

stefnats commented 2 years ago

Motivation:

I want to be able to test if some messages have been published / consume by app('amqp') without interacting with a RabbitMQ Server. This is very useful for Test Driven Development using PHPUnit, PEST, etc.

Usage:

With this you can just call:

AmqpManagerFake::register()

This overwrites app('amqp') and lets you publish / consume messages without communicating with an actual RabbitMQ Server. Also, there are a few Assertions for PHPUnit:

AmqpManagerFake::assertPublishedCount($count) // for asserting that a certain number of messages has been published
AmqpManagerFake::assertPublished() // for asserting that at least one message has been published
AmqpManagerFake::assertNothingPublished() // for asserting that no message has been published
AmqpManagerFake::assertConsumedCount($count) // for asserting that a certain number of messages has been consumed
AmqpManagerFake::assertConsumed() // for asserting that at least one message has been consumed
AmqpManagerFake::assertNothingConsumed() // for asserting that no message has been consumed

For publishing, just use the usual app('amqp')->publish(...) code.

For consuming, use the app('amqp')->consume(...) code. It also takes another (nullable) argument that the consumer should be stopped after x seconds.

Please also look into this and give it a test drive. I hope this helps some folks.

ssi-anik commented 2 years ago

Thanks for the PR, I appreciate your help. Recently I have started working on v2. I will try to include this one if possible.

ssi-anik commented 2 years ago

Mate, thanks for the PR. I have moved the Laravel Support to https://github.com/ssi-anik/laravel-amqp which also includes testing.

Thanks for the support.