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.6] Non faked events should be properly dispatched #25185

Merged
merged 1 commit into from
Aug 13, 2018
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
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function hasDispatched($event)
*/
public function listen($events, $listener)
{
//
$this->dispatcher->listen($events, $listener);
}

/**
Expand All @@ -146,7 +146,7 @@ public function listen($events, $listener)
*/
public function hasListeners($eventName)
{
//
return $this->dispatcher->hasListeners($eventName);
}

/**
Expand All @@ -169,7 +169,7 @@ public function push($event, $payload = [])
*/
public function subscribe($subscriber)
{
//
$this->dispatcher->subscribe($subscriber);
}

/**
Expand Down
96 changes: 96 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Illuminate\Tests\Integration\Events;

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;

class EventFakeTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->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);
}
}