Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
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; } }
尋找差異