jlndk / laravel-test-broadcaster
A broadcast provider for testing event broadcasting
Requires
- php: ^7.4
- illuminate/support: ^7.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-11-10 06:28:11 UTC
README
This package lets you test if Laravel events has been broadcasted. This is useful for TDD and End-to-end testing.
Installation
- Install the package via composer:
composer require jlndk/laravel-test-broadcaster
- Add the test broadcaster to the
connections
array inapp/config/broadcasting.php
.
'connections' => [ ... 'test' => [ 'driver' => 'test' ], ],
- Set the default broadcaster for testing in the
php
element ofphpunit.xml
.
<php> ... <env name="BROADCAST_DRIVER" value="test"/> </php>
- Finally add the
Jlndk\TestBroadcaster\CanTestBroadcasting
trait totests/TestCase.php
.
use Jlndk\TestBroadcaster\CanTestBroadcasting; abstract class TestCase extends BaseTestCase { use CanTestBroadcasting; }
Usage
This package adds the assertEventBroadcasted
method to your testing.
/** * @test */ public function it_can_assert_when_an_event_is_broadcasted() { event(new TestEvent()); $this->assertEventBroadcasted(TestEvent::class); }
Futhermore it is also possible to test for how many times an even is broadcasted
/** * @test */ public function it_can_assert_if_an_event_was_broadcasted_a_given_amount_of_times() { event(new TestEvent()); $this->assertEventBroadcasted(TestEvent::class, 1); event(new TestEvent()); $this->assertEventBroadcasted(TestEvent::class, 2); }
/** * @test */ public function it_can_assert_if_an_event_was_broadcasted_a_given_amount_of_times() { event(new TestEvent()); $this->assertEventBroadcasted(TestEvent::class, 1); event(new TestEvent()); $this->assertEventBroadcasted(TestEvent::class, 2); }
The assertEventBroadcasted
method can also assert on which channels the event is broadcasted to.
It can either take a single string, for a single channel, or an array of channel names.
/** * @test */ public function it_can_assert_if_an_event_was_broadcasted_on_multiple_channels() { event(new TestEvent()); // $this->assertEventBroadcasted(TestEvent::class, ['private-channel-name', 'private-another-channel-name']); try { $this->assertEventBroadcasted(TestEvent::class, [ 'private-channel-name', 'somethingelse-fake-channel', ]); $this->fail("assertEventBroadcasted asserted that an event was broadcasted on given channels when it wasn't"); } catch (ExpectationFailedException $e) { } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email xxx instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.