Skip to content

Commit

Permalink
add assertDispatchedTimes for event fakes (#22528)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekateiva authored and taylorotwell committed Dec 24, 2017
1 parent 514cba9 commit a55c4bf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,36 @@ public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
* Assert if an event was dispatched based on a truth-test callback.
*
* @param string $event
* @param callable|null $callback
* @param callable|int|null $callback
* @return void
*/
public function assertDispatched($event, $callback = null)
{
if (is_int($callback)) {
return $this->assertDispatchedTimes($event, $callback);
}

PHPUnit::assertTrue(
$this->dispatched($event, $callback)->count() > 0,
"The expected [{$event}] event was not dispatched."
);
}

/**
* Assert if a event was dispatched a number of times.
*
* @param string $event
* @param int $times
* @return void
*/
public function assertDispatchedTimes($event, $times = 1)
{
PHPUnit::assertTrue(
($count = $this->dispatched($event)->count()) === $times,
"The expected [{$event}] event was dispatched {$count} times instead of {$times} times."
);
}

/**
* Determine if an event was dispatched based on a truth-test callback.
*
Expand Down
65 changes: 65 additions & 0 deletions tests/Support/SupportTestingEventFakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Illuminate\Tests\Support;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Testing\Fakes\EventFake;

class SupportTestingEventFakeTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->fake = new EventFake(m::mock(Dispatcher::class));
}

public function testAssertDispacthed()
{
$this->fake->dispatch(EventStub::class);

$this->fake->assertDispatched(EventStub::class);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times.
*/
public function testAssertDispatchedWithCallbackInt()
{
$this->fake->dispatch(EventStub::class);
$this->fake->dispatch(EventStub::class);

$this->fake->assertDispatched(EventStub::class, 2);
$this->fake->assertDispatched(EventStub::class, 1);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The expected [Illuminate\Tests\Support\EventStub] event was dispatched 2 times instead of 1 times.
*/
public function testAssertDispatchedTimes()
{
$this->fake->dispatch(EventStub::class);
$this->fake->dispatch(EventStub::class);

$this->fake->assertDispatchedTimes(EventStub::class, 2);
$this->fake->assertDispatchedTimes(EventStub::class, 1);
}

/**
* @expectedException PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessage The unexpected [Illuminate\Tests\Support\EventStub] event was dispatched.
*/
public function testAssertNotDispatched()
{
$this->fake->dispatch(EventStub::class);

$this->fake->assertNotDispatched(EventStub::class);
}
}

class EventStub
{
}

0 comments on commit a55c4bf

Please sign in to comment.