Giter Club home page Giter Club logo

laravel-test-broadcaster's Introduction

Test Laravel Event Broadcasting

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package lets you test if Laravel events has been broadcasted. This is useful for TDD and End-to-end testing.

Installation

  1. Install the package via composer:
composer require jlndk/laravel-test-broadcaster
  1. Add the test broadcaster to the connections array in app/config/broadcasting.php.
'connections' => [
    ...
    'test' => [
        'driver' => 'test'
    ],
],
  1. Set the default broadcaster for testing in the php element of phpunit.xml.
<php>
    ...
    <env name="BROADCAST_DRIVER" value="test"/>
</php>
  1. Finally add the VitorHugoRo\TestBroadcaster\CanTestBroadcasting trait to tests/TestCase.php.
use VitorHugoRo\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.

laravel-test-broadcaster's People

Contributors

jlndk avatar vitorhugoro1 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.