1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
<?php
namespace Illuminate\Tests\Integration\Events;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase;
class EventFakeTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Contracts\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' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
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(): void
{
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);
}
public function testNonFakedEventGetsProperlyDispatchedAndReturnsResponses()
{
Event::fake(NonImportantEvent::class);
Event::listen('test', function () {
// one
});
Event::listen('test', function () {
return 'two';
});
Event::listen('test', function () {
//
});
$this->assertEquals([null, 'two', null], Event::dispatch('test'));
Event::assertNotDispatched(NonImportantEvent::class);
}
public function testNonFakedEventGetsProperlyDispatchedAndCancelsFutureListeners()
{
Event::fake(NonImportantEvent::class);
Event::listen('test', function () {
// one
});
Event::listen('test', function () {
return false;
});
Event::listen('test', function () {
$this->fail('should not be called');
});
$this->assertEquals([null], Event::dispatch('test'));
Event::assertNotDispatched(NonImportantEvent::class);
}
public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse()
{
Event::fake(NonImportantEvent::class);
Event::listen('test', function () {
// one
});
Event::listen('test', function () {
return 'two';
});
Event::listen('test', function () {
$this->fail('should not be called');
});
$this->assertSame('two', Event::until('test'));
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);
}
}
|