Skip to content

Instantly share code, notes, and snippets.

@paulhennell
Created December 13, 2019 16:51
Show Gist options
  • Save paulhennell/e28709083c69a10cb4f6fd9480001426 to your computer and use it in GitHub Desktop.
Save paulhennell/e28709083c69a10cb4f6fd9480001426 to your computer and use it in GitHub Desktop.
Laravel Event Testing
<?php
class MyTestEvent
{
public function __construct(myClass $class)
{
$this->class = $class;
}
}
//----
class MyTestEvent
{
public function handle($event)
{
$event->class->hello();
}
}
//----
class myClass
{
public function hello()
{
echo "hello";
}
}
//-----
class EventTest extends TestCase
{
/** @test */
public function test_an_event_is_triggered()
{
$this->expectsEvents(MyTestEvent::class);
event(new MyTestEvent());
}
/** @test */
public function test_a_listener_is_triggered()
{
$myClass = Mockery::spy(myClass::class);
$listener = Mockery::spy(MyTestListener::class);
app()->instance(MyTestListener::class, $listener);
event(new MyTestEvent($myClass));
$listener->shouldHaveReceived('handle')->once();;
}
/** @test */
public function a_myClass_class_is_called()
{
$myClass = Mockery::spy(myClass::class);
event(new MyTestEvent($myClass));
$myClass->shouldHaveReceived('hello')->once();;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment