From 8b5ca1bf9a5fdecdbf5ad18e94638f0c34d2e6e1 Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Sun, 12 Aug 2018 22:30:25 +0200 Subject: [PATCH] Non faked events should be properly dispatched --- .../Support/Testing/Fakes/EventFake.php | 6 +- tests/Integration/Events/EventFakeTest.php | 96 +++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 tests/Integration/Events/EventFakeTest.php diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index edb5928dea1b..57e47c89f378 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -135,7 +135,7 @@ public function hasDispatched($event) */ public function listen($events, $listener) { - // + $this->dispatcher->listen($events, $listener); } /** @@ -146,7 +146,7 @@ public function listen($events, $listener) */ public function hasListeners($eventName) { - // + return $this->dispatcher->hasListeners($eventName); } /** @@ -169,7 +169,7 @@ public function push($event, $payload = []) */ public function subscribe($subscriber) { - // + $this->dispatcher->subscribe($subscriber); } /** diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php new file mode 100644 index 000000000000..aa1594ba3d15 --- /dev/null +++ b/tests/Integration/Events/EventFakeTest.php @@ -0,0 +1,96 @@ +set('app.debug', 'true'); + + // Database configuration + $app['config']->set('database.default', 'testbench'); + + $app['config']->set('database.connections.testbench', [ + 'driver' => 'mysql', + 'host' => env('DB_HOST', '127.0.0.1'), + 'username' => 'root', + 'password' => '', + 'database' => 'forge', + 'prefix' => '', + ]); + } + + /** + * Setup the test environment. + * + * @return void + */ + protected function setUp() + { + parent::setUp(); + + Schema::create('posts', function (Blueprint $table) { + $table->increments('id'); + $table->string('title'); + $table->string('slug')->unique(); + $table->timestamps(); + }); + } + + /** + * Clean up the testing environment before the next test. + * + * @return void + */ + protected function tearDown() + { + Schema::dropIfExists('posts'); + + parent::tearDown(); + } + + public function testNonFakedEventGetsProperlyDispatched() + { + Event::fake(NonImportantEvent::class); + Post::observe([PostObserver::class]); + + $post = new Post(); + $post->title = 'xyz'; + $post->save(); + + $this->assertSame('xyz-Test', $post->slug); + + Event::assertNotDispatched(NonImportantEvent::class); + } +} + +class Post extends Model +{ + public $table = 'posts'; +} + +class NonImportantEvent +{ +} + +class PostObserver +{ + public function saving(Post $post) + { + $post->slug = sprintf('%s-Test', $post->title); + } +}