定制 digitalcloud/testable-event-listener 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

digitalcloud/testable-event-listener

Composer 安装命令:

composer require digitalcloud/testable-event-listener

包简介

A Laravel Testable Event Listener.

README 文档

README

When developing using TTD pattern, Laravel allow you to fake any event. faking event will prevent the dispatcher from firing that event and so, no listener for this event will be executed. But in some scenarios, we need to fire this event but execute some listener and ignore others, here is why we create this package.

This package allow you to fire an event during testing, but restrict the listener for this event.

Installation

You can install the package via composer:

composer require digitalcloud/testable-event-listener

Usage Example

Suppose we have an event fired when we create a new user, and in the normal behavior we have many listener to this event, but during testing we want some of those event to be executed and the other to be ignored.

The user model will fire the \App\Events\UserCreating Event when the Eloquent creating event fired.

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $dispatchesEvents = [
        'creating' => \App\Events\UserCreating::class
    ];
}

Our EventServiceProvider look like this:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    // ...
    
    protected $listen = [
        \App\Events\UserCreating::class => [
            // ...
            \App\Listeners\UserCreating::class,
            \App\Listeners\UserCreated::class,
            // ...
        ],
    ];
    
    // ...
}

But when testing we need to run only one listener, \App\Listeners\UserCreating::class, and ignoring all other listener. To do this we can call the EventFaker:fake and pass it the array of fackable event with the required listener to be executed.

<?php

namespace Tests\Unit;

use App\User;
use DigitalCloud\TestableEventListener\EventFaker;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        EventFaker::fake([
            \App\Events\UserCreating::class => [
                \App\Listeners\UserCreating::class
            ],
        ]);
        $user = factory(User::class)->create();
        EventFaker::assertDispatched(\App\Events\UserCreating::class);
    }
}

the result is:

PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

"App\Listeners\UserCreating::handle"

Time: 863 ms, Memory: 14.00MB

OK (1 test, 1 assertion)

Process finished with exit code 0

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固