Diff
checker
テキスト
テキスト
画像
ドキュメント
Excel
フォルダ
Legal
Enterprise
デスクトップ
料金
ログイン
Diffchecker デスクトップのダウンロード
テキスト比較
2 つのテキスト ファイルの違いを見つける
ツール
履歴
ライブエディター
未変更行を折りたたむ
折り返しなし
レイアウト
分割
統合
比較精度
スマート
単語
文字
シンタックスハイライト
構文を選択
無視
テキスト変換
最初の差分へ移動
入力を編集
Diffchecker Desktop
Diffcheckerを実行する最も安全な方法。Diffchecker Desktopアプリを入手:あなたの差分はコンピューターから出ることはありません!
Desktopを入手
MockApplicationServices.php
作成日
6 年前
差分は期限切れになりません
クリア
エクスポート
共有
説明
148 削除
行
合計
削除
文字
合計
削除
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
416 行
すべてコピー
11 追加
行
合計
追加
文字
合計
追加
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
287 行
すべてコピー
<?php
<?php
コピー
コピー済み
コピー
コピー済み
namespace
Laravel\BrowserKitTesting
\Concerns;
namespace
Illuminate\Foundation\Testing
\Concerns;
コピー
コピー済み
コピー
コピー済み
use Exception;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcherContract;
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;
コピー
コピー済み
コピー
コピー済み
use Illuminate\
Database\Eloquent\Model
;
use Illuminate\
Support\Facades\Event
;
use Mockery;
use Mockery;
trait MocksApplicationServices
trait MocksApplicationServices
{
{
/**
/**
* All of the fired events.
* All of the fired events.
*
*
* @var array
* @var array
*/
*/
protected $firedEvents = [];
protected $firedEvents = [];
/**
/**
* All of the fired model events.
* All of the fired model events.
*
*
* @var array
* @var array
*/
*/
protected $firedModelEvents = [];
protected $firedModelEvents = [];
/**
/**
* All of the dispatched jobs.
* All of the dispatched jobs.
*
*
* @var array
* @var array
*/
*/
protected $dispatchedJobs = [];
protected $dispatchedJobs = [];
/**
/**
* All of the dispatched notifications.
* All of the dispatched notifications.
*
*
* @var array
* @var array
*/
*/
protected $dispatchedNotifications = [];
protected $dispatchedNotifications = [];
/**
/**
* Specify a list of events that should be fired for the given operation.
* Specify a list of events that should be fired for the given operation.
*
*
* These events will be mocked, so that handlers will not actually be executed.
* These events will be mocked, so that handlers will not actually be executed.
*
*
* @param array|string $events
* @param array|string $events
* @return $this
* @return $this
*
*
* @throws \Exception
* @throws \Exception
*/
*/
public function expectsEvents($events)
public function expectsEvents($events)
{
{
$events = is_array($events) ? $events : func_get_args();
$events = is_array($events) ? $events : func_get_args();
$this->withoutEvents();
$this->withoutEvents();
$this->beforeApplicationDestroyed(function () use ($events) {
$this->beforeApplicationDestroyed(function () use ($events) {
$fired = $this->getFiredEvents($events);
$fired = $this->getFiredEvents($events);
$this->assertEmpty(
$this->assertEmpty(
$eventsNotFired = array_diff($events, $fired),
$eventsNotFired = array_diff($events, $fired),
'These expected events were not fired: ['.implode(', ', $eventsNotFired).']'
'These expected events were not fired: ['.implode(', ', $eventsNotFired).']'
);
);
});
});
return $this;
return $this;
}
}
/**
/**
* Specify a list of events that should not be fired for the given operation.
* Specify a list of events that should not be fired for the given operation.
*
*
* These events will be mocked, so that handlers will not actually be executed.
* These events will be mocked, so that handlers will not actually be executed.
*
*
* @param array|string $events
* @param array|string $events
* @return $this
* @return $this
*/
*/
public function doesntExpectEvents($events)
public function doesntExpectEvents($events)
{
{
$events = is_array($events) ? $events : func_get_args();
$events = is_array($events) ? $events : func_get_args();
$this->withoutEvents();
$this->withoutEvents();
$this->beforeApplicationDestroyed(function () use ($events) {
$this->beforeApplicationDestroyed(function () use ($events) {
$this->assertEmpty(
$this->assertEmpty(
$fired = $this->getFiredEvents($events),
$fired = $this->getFiredEvents($events),
'These unexpected events were fired: ['.implode(', ', $fired).']'
'These unexpected events were fired: ['.implode(', ', $fired).']'
);
);
});
});
return $this;
return $this;
}
}
/**
/**
* Mock the event dispatcher so all events are silenced and collected.
* Mock the event dispatcher so all events are silenced and collected.
*
*
* @return $this
* @return $this
*/
*/
protected function withoutEvents()
protected function withoutEvents()
{
{
コピー
コピー済み
コピー
コピー済み
$mock = Mockery::mock(
'Illuminate\Contracts\
Events
\
Dispatcher
'
);
$mock = Mockery::mock(
Events
Dispatcher
Contract::class)->shouldIgnoreMissing(
);
コピー
コピー済み
コピー
コピー済み
$mock->shouldReceive(
'fire',
'dispatch', '
getCommandHandler
')->andReturnUsing(function ($called) {
$mock->shouldReceive(
'dispatch', '
until
')->andReturnUsing(function ($called) {
$this->firedEvents[] = $called;
$this->firedEvents[] = $called;
});
});
コピー
コピー済み
コピー
コピー済み
Event::clearResolvedInstances();
$this->app->instance('events', $mock);
$this->app->instance('events', $mock);
return $this;
return $this;
}
}
/**
/**
コピー
コピー済み
コピー
コピー済み
* Specify a list of events that should be fired for the given operation.
*
* These events will be mocked, so that handlers will not actually be executed.
*
* @param string $model
* @param array|string $events
* @return $this
*
* @throws \Exception
*/
public function expectsModelEvents($model, $events)
{
$events = $this->formatModelEvents($model, $events);
$this->withoutModelEvents();
$this->beforeApplicationDestroyed(function () use ($events) {
$fired = $this->getFiredModelEvents($events);
if ($eventsNotFired = array_diff($events, $fired)) {
throw new Exception(
'These expected Eloquent events were not fired: ['.implode(', ', $eventsNotFired).']'
);
}
});
return $this;
}
/**
* Specify a list of events that should not be fired for the given operation.
*
* These events will be mocked, so that handlers will not actually be executed.
*
* @param string $model
* @param array|string $events
* @return $this
*
* @throws \Exception
*/
public function doesntExpectModelEvents($model, $events)
{
$events = $this->formatModelEvents($model, $events);
$this->withoutModelEvents();
$this->beforeApplicationDestroyed(function () use ($events) {
if ($fired = $this->getFiredModelEvents($events)) {
throw new Exception(
'These unexpected Eloquent events were fired: ['.implode(', ', $fired).']'
);
}
});
return $this;
}
/**
* Convert a model and a list of events into the Eloquent's format.
*
* @param string $model
* @param array|string $events
* @return string[]
*/
private function formatModelEvents($model, $events)
{
$events = (array) $events;
return array_map(function ($event) use ($model) {
return "eloquent.{$event}: {$model}";
}, (array) $events);
}
/**
* Mock the model event dispatcher so all Eloquent events are silenced.
*
* @return $this
*/
protected function withoutModelEvents()
{
$mock = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$mock->shouldReceive('dispatch')->andReturnUsing(function ($called) {
$this->firedModelEvents[] = $called;
});
$mock->shouldReceive('until')->andReturnUsing(function ($called) {
$this->firedModelEvents[] = $called;
return true;
});
$mock->shouldReceive('listen')->andReturnUsing(function ($event, $listener) {
//
});
Model::setEventDispatcher($mock);
return $this;
}
/**
* Specify a list of observers that will not run for the given operation.
*
* @param array|string $observers
* @return $this
*/
public function withoutObservers($observers)
{
$observers = is_array($observers) ? $observers : [$observers];
array_map(function ($observer) {
$this->app->bind($observer, function () use ($observer) {
return $this->getMockBuilder($observer)->disableOriginalConstructor()->getMock();
});
}, $observers);
return $this;
}
/**
* Filter the given events against the fired events.
* Filter the given events against the fired events.
*
*
* @param array $events
* @param array $events
* @return array
* @return array
*/
*/
protected function getFiredEvents(array $events)
protected function getFiredEvents(array $events)
{
{
return $this->getDispatched($events, $this->firedEvents);
return $this->getDispatched($events, $this->firedEvents);
}
}
/**
/**
コピー
コピー済み
コピー
コピー済み
* Filter the given events against the fired events.
*
* @param array $events
* @return array
*/
protected function getFiredModelEvents(array $events)
{
return $this->getDispatched($events, $this->firedModelEvents);
}
/**
* Specify a list of jobs that should be dispatched for the given operation.
* Specify a list of jobs that should be dispatched for the given operation.
*
*
* These jobs will be mocked, so that handlers will not actually be executed.
* These jobs will be mocked, so that handlers will not actually be executed.
*
*
* @param array|string $jobs
* @param array|string $jobs
* @return $this
* @return $this
*/
*/
protected function expectsJobs($jobs)
protected function expectsJobs($jobs)
{
{
$jobs = is_array($jobs) ? $jobs : func_get_args();
$jobs = is_array($jobs) ? $jobs : func_get_args();
$this->withoutJobs();
$this->withoutJobs();
$this->beforeApplicationDestroyed(function () use ($jobs) {
$this->beforeApplicationDestroyed(function () use ($jobs) {
$dispatched = $this->getDispatchedJobs($jobs);
$dispatched = $this->getDispatchedJobs($jobs);
$this->assertEmpty(
$this->assertEmpty(
$jobsNotDispatched = array_diff($jobs, $dispatched),
$jobsNotDispatched = array_diff($jobs, $dispatched),
'These expected jobs were not dispatched: ['.implode(', ', $jobsNotDispatched).']'
'These expected jobs were not dispatched: ['.implode(', ', $jobsNotDispatched).']'
);
);
});
});
return $this;
return $this;
}
}
/**
/**
* Specify a list of jobs that should not be dispatched for the given operation.
* Specify a list of jobs that should not be dispatched for the given operation.
*
*
* These jobs will be mocked, so that handlers will not actually be executed.
* These jobs will be mocked, so that handlers will not actually be executed.
*
*
* @param array|string $jobs
* @param array|string $jobs
* @return $this
* @return $this
*/
*/
protected function doesntExpectJobs($jobs)
protected function doesntExpectJobs($jobs)
{
{
$jobs = is_array($jobs) ? $jobs : func_get_args();
$jobs = is_array($jobs) ? $jobs : func_get_args();
$this->withoutJobs();
$this->withoutJobs();
$this->beforeApplicationDestroyed(function () use ($jobs) {
$this->beforeApplicationDestroyed(function () use ($jobs) {
$this->assertEmpty(
$this->assertEmpty(
$dispatched = $this->getDispatchedJobs($jobs),
$dispatched = $this->getDispatchedJobs($jobs),
'These unexpected jobs were dispatched: ['.implode(', ', $dispatched).']'
'These unexpected jobs were dispatched: ['.implode(', ', $dispatched).']'
);
);
});
});
return $this;
return $this;
}
}
/**
/**
* Mock the job dispatcher so all jobs are silenced and collected.
* Mock the job dispatcher so all jobs are silenced and collected.
*
*
* @return $this
* @return $this
*/
*/
protected function withoutJobs()
protected function withoutJobs()
{
{
コピー
コピー済み
コピー
コピー済み
$mock = Mockery::mock(
'Illuminate\Contracts\
Bus
\
Dispatcher
'
);
$mock = Mockery::mock(
Bus
Dispatcher
Contract::class)->shouldIgnoreMissing(
);
コピー
コピー済み
コピー
コピー済み
$mock->shouldReceive('dispatch', 'dispatchNow'
, 'getCommandHandler'
)->andReturnUsing(function ($dispatched) {
$mock->shouldReceive('dispatch', 'dispatchNow'
)->andReturnUsing(function ($dispatched) {
$this->dispatchedJobs[] = $dispatched;
$this->dispatchedJobs[] = $dispatched;
});
});
$this->app->instance(
$this->app->instance(
コピー
コピー済み
コピー
コピー済み
'Illuminate\Contracts\
Bus
\
Dispatcher
'
, $mock
Bus
Dispatcher
Contract::class
, $mock
);
);
return $this;
return $this;
}
}
/**
/**
* Filter the given jobs against the dispatched jobs.
* Filter the given jobs against the dispatched jobs.
*
*
* @param array $jobs
* @param array $jobs
* @return array
* @return array
*/
*/
protected function getDispatchedJobs(array $jobs)
protected function getDispatchedJobs(array $jobs)
{
{
return $this->getDispatched($jobs, $this->dispatchedJobs);
return $this->getDispatched($jobs, $this->dispatchedJobs);
}
}
/**
/**
* Filter the given classes against an array of dispatched classes.
* Filter the given classes against an array of dispatched classes.
*
*
* @param array $classes
* @param array $classes
* @param array $dispatched
* @param array $dispatched
* @return array
* @return array
*/
*/
protected function getDispatched(array $classes, array $dispatched)
protected function getDispatched(array $classes, array $dispatched)
{
{
return array_filter($classes, function ($class) use ($dispatched) {
return array_filter($classes, function ($class) use ($dispatched) {
return $this->wasDispatched($class, $dispatched);
return $this->wasDispatched($class, $dispatched);
});
});
}
}
/**
/**
* Check if the given class exists in an array of dispatched classes.
* Check if the given class exists in an array of dispatched classes.
*
*
* @param string $needle
* @param string $needle
* @param array $haystack
* @param array $haystack
* @return bool
* @return bool
*/
*/
protected function wasDispatched($needle, array $haystack)
protected function wasDispatched($needle, array $haystack)
{
{
foreach ($haystack as $dispatched) {
foreach ($haystack as $dispatched) {
if ((is_string($dispatched) && ($dispatched === $needle || is_subclass_of($dispatched, $needle))) ||
if ((is_string($dispatched) && ($dispatched === $needle || is_subclass_of($dispatched, $needle))) ||
$dispatched instanceof $needle) {
$dispatched instanceof $needle) {
return true;
return true;
}
}
}
}
return false;
return false;
}
}
/**
/**
* Mock the notification dispatcher so all notifications are silenced.
* Mock the notification dispatcher so all notifications are silenced.
*
*
* @return $this
* @return $this
*/
*/
protected function withoutNotifications()
protected function withoutNotifications()
{
{
$mock = Mockery::mock(NotificationDispatcher::class);
$mock = Mockery::mock(NotificationDispatcher::class);
$mock->shouldReceive('send')->andReturnUsing(function ($notifiable, $instance, $channels = []) {
$mock->shouldReceive('send')->andReturnUsing(function ($notifiable, $instance, $channels = []) {
$this->dispatchedNotifications[] = compact(
$this->dispatchedNotifications[] = compact(
'notifiable', 'instance', 'channels'
'notifiable', 'instance', 'channels'
);
);
});
});
$this->app->instance(NotificationDispatcher::class, $mock);
$this->app->instance(NotificationDispatcher::class, $mock);
return $this;
return $this;
}
}
/**
/**
* Specify a notification that is expected to be dispatched.
* Specify a notification that is expected to be dispatched.
*
*
* @param mixed $notifiable
* @param mixed $notifiable
* @param string $notification
* @param string $notification
* @return $this
* @return $this
*/
*/
protected function expectsNotification($notifiable, $notification)
protected function expectsNotification($notifiable, $notification)
{
{
$this->withoutNotifications();
$this->withoutNotifications();
$this->beforeApplicationDestroyed(function () use ($notifiable, $notification) {
$this->beforeApplicationDestroyed(function () use ($notifiable, $notification) {
foreach ($this->dispatchedNotifications as $dispatched) {
foreach ($this->dispatchedNotifications as $dispatched) {
$notified = $dispatched['notifiable'];
$notified = $dispatched['notifiable'];
if (($notified === $notifiable ||
if (($notified === $notifiable ||
$notified->getKey() == $notifiable->getKey()) &&
$notified->getKey() == $notifiable->getKey()) &&
get_class($dispatched['instance']) === $notification
get_class($dispatched['instance']) === $notification
) {
) {
return $this;
return $this;
}
}
}
}
コピー
コピー済み
コピー
コピー済み
$this->fail('The following expected notification
was
not dispatched: ['.$notification.']');
$this->fail('The following expected notification
were
not dispatched: ['.$notification.']');
});
});
return $this;
return $this;
}
}
}
}
保存された差分
原文
ファイルを開く
<?php namespace Laravel\BrowserKitTesting\Concerns; use Exception; use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher; use Illuminate\Database\Eloquent\Model; use Mockery; trait MocksApplicationServices { /** * All of the fired events. * * @var array */ protected $firedEvents = []; /** * All of the fired model events. * * @var array */ protected $firedModelEvents = []; /** * All of the dispatched jobs. * * @var array */ protected $dispatchedJobs = []; /** * All of the dispatched notifications. * * @var array */ protected $dispatchedNotifications = []; /** * Specify a list of events that should be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param array|string $events * @return $this * * @throws \Exception */ public function expectsEvents($events) { $events = is_array($events) ? $events : func_get_args(); $this->withoutEvents(); $this->beforeApplicationDestroyed(function () use ($events) { $fired = $this->getFiredEvents($events); $this->assertEmpty( $eventsNotFired = array_diff($events, $fired), 'These expected events were not fired: ['.implode(', ', $eventsNotFired).']' ); }); return $this; } /** * Specify a list of events that should not be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param array|string $events * @return $this */ public function doesntExpectEvents($events) { $events = is_array($events) ? $events : func_get_args(); $this->withoutEvents(); $this->beforeApplicationDestroyed(function () use ($events) { $this->assertEmpty( $fired = $this->getFiredEvents($events), 'These unexpected events were fired: ['.implode(', ', $fired).']' ); }); return $this; } /** * Mock the event dispatcher so all events are silenced and collected. * * @return $this */ protected function withoutEvents() { $mock = Mockery::mock('Illuminate\Contracts\Events\Dispatcher'); $mock->shouldReceive('fire', 'dispatch', 'getCommandHandler')->andReturnUsing(function ($called) { $this->firedEvents[] = $called; }); $this->app->instance('events', $mock); return $this; } /** * Specify a list of events that should be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param string $model * @param array|string $events * @return $this * * @throws \Exception */ public function expectsModelEvents($model, $events) { $events = $this->formatModelEvents($model, $events); $this->withoutModelEvents(); $this->beforeApplicationDestroyed(function () use ($events) { $fired = $this->getFiredModelEvents($events); if ($eventsNotFired = array_diff($events, $fired)) { throw new Exception( 'These expected Eloquent events were not fired: ['.implode(', ', $eventsNotFired).']' ); } }); return $this; } /** * Specify a list of events that should not be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param string $model * @param array|string $events * @return $this * * @throws \Exception */ public function doesntExpectModelEvents($model, $events) { $events = $this->formatModelEvents($model, $events); $this->withoutModelEvents(); $this->beforeApplicationDestroyed(function () use ($events) { if ($fired = $this->getFiredModelEvents($events)) { throw new Exception( 'These unexpected Eloquent events were fired: ['.implode(', ', $fired).']' ); } }); return $this; } /** * Convert a model and a list of events into the Eloquent's format. * * @param string $model * @param array|string $events * @return string[] */ private function formatModelEvents($model, $events) { $events = (array) $events; return array_map(function ($event) use ($model) { return "eloquent.{$event}: {$model}"; }, (array) $events); } /** * Mock the model event dispatcher so all Eloquent events are silenced. * * @return $this */ protected function withoutModelEvents() { $mock = Mockery::mock('Illuminate\Contracts\Events\Dispatcher'); $mock->shouldReceive('dispatch')->andReturnUsing(function ($called) { $this->firedModelEvents[] = $called; }); $mock->shouldReceive('until')->andReturnUsing(function ($called) { $this->firedModelEvents[] = $called; return true; }); $mock->shouldReceive('listen')->andReturnUsing(function ($event, $listener) { // }); Model::setEventDispatcher($mock); return $this; } /** * Specify a list of observers that will not run for the given operation. * * @param array|string $observers * @return $this */ public function withoutObservers($observers) { $observers = is_array($observers) ? $observers : [$observers]; array_map(function ($observer) { $this->app->bind($observer, function () use ($observer) { return $this->getMockBuilder($observer)->disableOriginalConstructor()->getMock(); }); }, $observers); return $this; } /** * Filter the given events against the fired events. * * @param array $events * @return array */ protected function getFiredEvents(array $events) { return $this->getDispatched($events, $this->firedEvents); } /** * Filter the given events against the fired events. * * @param array $events * @return array */ protected function getFiredModelEvents(array $events) { return $this->getDispatched($events, $this->firedModelEvents); } /** * Specify a list of jobs that should be dispatched for the given operation. * * These jobs will be mocked, so that handlers will not actually be executed. * * @param array|string $jobs * @return $this */ protected function expectsJobs($jobs) { $jobs = is_array($jobs) ? $jobs : func_get_args(); $this->withoutJobs(); $this->beforeApplicationDestroyed(function () use ($jobs) { $dispatched = $this->getDispatchedJobs($jobs); $this->assertEmpty( $jobsNotDispatched = array_diff($jobs, $dispatched), 'These expected jobs were not dispatched: ['.implode(', ', $jobsNotDispatched).']' ); }); return $this; } /** * Specify a list of jobs that should not be dispatched for the given operation. * * These jobs will be mocked, so that handlers will not actually be executed. * * @param array|string $jobs * @return $this */ protected function doesntExpectJobs($jobs) { $jobs = is_array($jobs) ? $jobs : func_get_args(); $this->withoutJobs(); $this->beforeApplicationDestroyed(function () use ($jobs) { $this->assertEmpty( $dispatched = $this->getDispatchedJobs($jobs), 'These unexpected jobs were dispatched: ['.implode(', ', $dispatched).']' ); }); return $this; } /** * Mock the job dispatcher so all jobs are silenced and collected. * * @return $this */ protected function withoutJobs() { $mock = Mockery::mock('Illuminate\Contracts\Bus\Dispatcher'); $mock->shouldReceive('dispatch', 'dispatchNow', 'getCommandHandler')->andReturnUsing(function ($dispatched) { $this->dispatchedJobs[] = $dispatched; }); $this->app->instance( 'Illuminate\Contracts\Bus\Dispatcher', $mock ); return $this; } /** * Filter the given jobs against the dispatched jobs. * * @param array $jobs * @return array */ protected function getDispatchedJobs(array $jobs) { return $this->getDispatched($jobs, $this->dispatchedJobs); } /** * Filter the given classes against an array of dispatched classes. * * @param array $classes * @param array $dispatched * @return array */ protected function getDispatched(array $classes, array $dispatched) { return array_filter($classes, function ($class) use ($dispatched) { return $this->wasDispatched($class, $dispatched); }); } /** * Check if the given class exists in an array of dispatched classes. * * @param string $needle * @param array $haystack * @return bool */ protected function wasDispatched($needle, array $haystack) { foreach ($haystack as $dispatched) { if ((is_string($dispatched) && ($dispatched === $needle || is_subclass_of($dispatched, $needle))) || $dispatched instanceof $needle) { return true; } } return false; } /** * Mock the notification dispatcher so all notifications are silenced. * * @return $this */ protected function withoutNotifications() { $mock = Mockery::mock(NotificationDispatcher::class); $mock->shouldReceive('send')->andReturnUsing(function ($notifiable, $instance, $channels = []) { $this->dispatchedNotifications[] = compact( 'notifiable', 'instance', 'channels' ); }); $this->app->instance(NotificationDispatcher::class, $mock); return $this; } /** * Specify a notification that is expected to be dispatched. * * @param mixed $notifiable * @param string $notification * @return $this */ protected function expectsNotification($notifiable, $notification) { $this->withoutNotifications(); $this->beforeApplicationDestroyed(function () use ($notifiable, $notification) { foreach ($this->dispatchedNotifications as $dispatched) { $notified = $dispatched['notifiable']; if (($notified === $notifiable || $notified->getKey() == $notifiable->getKey()) && get_class($dispatched['instance']) === $notification ) { return $this; } } $this->fail('The following expected notification was not dispatched: ['.$notification.']'); }); return $this; } }
変更されたテキスト
ファイルを開く
<?php namespace Illuminate\Foundation\Testing\Concerns; use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract; use Illuminate\Contracts\Events\Dispatcher as EventsDispatcherContract; use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher; use Illuminate\Support\Facades\Event; use Mockery; trait MocksApplicationServices { /** * All of the fired events. * * @var array */ protected $firedEvents = []; /** * All of the fired model events. * * @var array */ protected $firedModelEvents = []; /** * All of the dispatched jobs. * * @var array */ protected $dispatchedJobs = []; /** * All of the dispatched notifications. * * @var array */ protected $dispatchedNotifications = []; /** * Specify a list of events that should be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param array|string $events * @return $this * * @throws \Exception */ public function expectsEvents($events) { $events = is_array($events) ? $events : func_get_args(); $this->withoutEvents(); $this->beforeApplicationDestroyed(function () use ($events) { $fired = $this->getFiredEvents($events); $this->assertEmpty( $eventsNotFired = array_diff($events, $fired), 'These expected events were not fired: ['.implode(', ', $eventsNotFired).']' ); }); return $this; } /** * Specify a list of events that should not be fired for the given operation. * * These events will be mocked, so that handlers will not actually be executed. * * @param array|string $events * @return $this */ public function doesntExpectEvents($events) { $events = is_array($events) ? $events : func_get_args(); $this->withoutEvents(); $this->beforeApplicationDestroyed(function () use ($events) { $this->assertEmpty( $fired = $this->getFiredEvents($events), 'These unexpected events were fired: ['.implode(', ', $fired).']' ); }); return $this; } /** * Mock the event dispatcher so all events are silenced and collected. * * @return $this */ protected function withoutEvents() { $mock = Mockery::mock(EventsDispatcherContract::class)->shouldIgnoreMissing(); $mock->shouldReceive('dispatch', 'until')->andReturnUsing(function ($called) { $this->firedEvents[] = $called; }); Event::clearResolvedInstances(); $this->app->instance('events', $mock); return $this; } /** * Filter the given events against the fired events. * * @param array $events * @return array */ protected function getFiredEvents(array $events) { return $this->getDispatched($events, $this->firedEvents); } /** * Specify a list of jobs that should be dispatched for the given operation. * * These jobs will be mocked, so that handlers will not actually be executed. * * @param array|string $jobs * @return $this */ protected function expectsJobs($jobs) { $jobs = is_array($jobs) ? $jobs : func_get_args(); $this->withoutJobs(); $this->beforeApplicationDestroyed(function () use ($jobs) { $dispatched = $this->getDispatchedJobs($jobs); $this->assertEmpty( $jobsNotDispatched = array_diff($jobs, $dispatched), 'These expected jobs were not dispatched: ['.implode(', ', $jobsNotDispatched).']' ); }); return $this; } /** * Specify a list of jobs that should not be dispatched for the given operation. * * These jobs will be mocked, so that handlers will not actually be executed. * * @param array|string $jobs * @return $this */ protected function doesntExpectJobs($jobs) { $jobs = is_array($jobs) ? $jobs : func_get_args(); $this->withoutJobs(); $this->beforeApplicationDestroyed(function () use ($jobs) { $this->assertEmpty( $dispatched = $this->getDispatchedJobs($jobs), 'These unexpected jobs were dispatched: ['.implode(', ', $dispatched).']' ); }); return $this; } /** * Mock the job dispatcher so all jobs are silenced and collected. * * @return $this */ protected function withoutJobs() { $mock = Mockery::mock(BusDispatcherContract::class)->shouldIgnoreMissing(); $mock->shouldReceive('dispatch', 'dispatchNow')->andReturnUsing(function ($dispatched) { $this->dispatchedJobs[] = $dispatched; }); $this->app->instance( BusDispatcherContract::class, $mock ); return $this; } /** * Filter the given jobs against the dispatched jobs. * * @param array $jobs * @return array */ protected function getDispatchedJobs(array $jobs) { return $this->getDispatched($jobs, $this->dispatchedJobs); } /** * Filter the given classes against an array of dispatched classes. * * @param array $classes * @param array $dispatched * @return array */ protected function getDispatched(array $classes, array $dispatched) { return array_filter($classes, function ($class) use ($dispatched) { return $this->wasDispatched($class, $dispatched); }); } /** * Check if the given class exists in an array of dispatched classes. * * @param string $needle * @param array $haystack * @return bool */ protected function wasDispatched($needle, array $haystack) { foreach ($haystack as $dispatched) { if ((is_string($dispatched) && ($dispatched === $needle || is_subclass_of($dispatched, $needle))) || $dispatched instanceof $needle) { return true; } } return false; } /** * Mock the notification dispatcher so all notifications are silenced. * * @return $this */ protected function withoutNotifications() { $mock = Mockery::mock(NotificationDispatcher::class); $mock->shouldReceive('send')->andReturnUsing(function ($notifiable, $instance, $channels = []) { $this->dispatchedNotifications[] = compact( 'notifiable', 'instance', 'channels' ); }); $this->app->instance(NotificationDispatcher::class, $mock); return $this; } /** * Specify a notification that is expected to be dispatched. * * @param mixed $notifiable * @param string $notification * @return $this */ protected function expectsNotification($notifiable, $notification) { $this->withoutNotifications(); $this->beforeApplicationDestroyed(function () use ($notifiable, $notification) { foreach ($this->dispatchedNotifications as $dispatched) { $notified = $dispatched['notifiable']; if (($notified === $notifiable || $notified->getKey() == $notifiable->getKey()) && get_class($dispatched['instance']) === $notification ) { return $this; } } $this->fail('The following expected notification were not dispatched: ['.$notification.']'); }); return $this; } }
違いを見つける