Skip to content

Commit

Permalink
Wrote initial README
Browse files Browse the repository at this point in the history
  • Loading branch information
jlndk committed Apr 9, 2020
1 parent b5c8ae8 commit d87bf81
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

All notable changes to `laravel-test-broadcaster` will be documented in this file

## 1.0.0 - 08-04-2020
## 0.0.1 - 09-04-2020

- initial release
- initial release of the code. Features are still missing and API will most likely change.
100 changes: 94 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,112 @@
# Very short description of the package
# Test Laravel Event Broadcasting

[![Latest Version on Packagist](https://img.shields.io/packagist/v/jlndk/laravel-test-broadcaster.svg?style=flat-square)](https://packagist.org/packages/jlndk/laravel-test-broadcaster)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/jlndk/laravel-test-broadcaster/run-tests?label=tests)](https://github.com/jlndk/laravel-test-broadcaster/actions?query=workflow%3Arun-tests+branch%3Amaster)
[![Total Downloads](https://img.shields.io/packagist/dt/jlndk/laravel-test-broadcaster.svg?style=flat-square)](https://packagist.org/packages/jlndk/laravel-test-broadcaster)

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.
This package lets you test if Laravel events has been broadcasted. This is useful for TDD and End-to-end testing.


## Installation

You can install the package via composer:
1. Install the package via composer:

```bash
composer require jlndk/laravel-test-broadcaster
```

## Usage
2. Add the test broadcaster to the `connections` array in `app/config/broadcasting.php`.
```php
'connections' => [
...
'test' => [
'driver' => 'test'
],
],

```

3. Set the default broadcaster for testing in the `php` element of `phpunit.xml`.
```xml
<php>
...
<env name="BROADCAST_DRIVER" value="test"/>
</php>
```

4. Finally add the `Jlndk\TestBroadcaster\CanTestBroadcasting` trait to `tests/TestCase.php`.
```php
use Jlndk\TestBroadcaster\CanTestBroadcasting;
abstract class TestCase extends BaseTestCase
{
use CanTestBroadcasting;
}
```

## Usage
This package adds the `assertEventBroadcasted` method to your testing.
``` php
$skeleton = new Jlndk\Skeleton();
echo $skeleton->echoPhrase('Hello, Jlndk!');
/**
* @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
```php
/**
* @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);
}
```

```php
/**
* @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.
```php
/**
* @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
Expand Down

0 comments on commit d87bf81

Please sign in to comment.