Created
December 13, 2019 16:51
-
-
Save paulhennell/e28709083c69a10cb4f6fd9480001426 to your computer and use it in GitHub Desktop.
Laravel Event Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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