Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] add assertDispatchedTimes for event fakes #22528

Merged
merged 1 commit into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
}