Diff
checker
Texto
Texto
Imágenes
Documentos
Excel
Carpetas
Legal
Enterprise
Aplicación de escritorio
Precios
Iniciar sesión
Descargar Diffchecker Desktop
Comparar texto
Encuentra la diferencia entre dos archivos de texto
Herramientas
Historial
Editor live
Ocultar sin cambios
Sin ajuste de línea
Vista
Dividido
Unificado
Nivel de detalle
Inteligente
Palabra
Letra
Resaltado de sintaxis
Elegir sintaxis
Ignorar
Transformar texto
Ir al primer cambio
Editar entrada
Diffchecker Desktop
La forma más segura de usar Diffchecker. ¡Obtén la app de Diffchecker Desktop: tus diffs nunca salen de tu computadora!
Obtener Desktop
MockApplicationServices.php
Creado
hace 6 años
El diff nunca expira
Borrar
Exportar
Compartir
Explicar
148 eliminaciones
Líneas
Total
Eliminado
Caracteres
Total
Eliminado
Para continuar usando esta función, actualice a
Diff
checker
Pro
Ver precios
416 líneas
Copiar todo
11 adiciones
Líneas
Total
Añadido
Caracteres
Total
Añadido
Para continuar usando esta función, actualice a
Diff
checker
Pro
Ver precios
287 líneas
Copiar todo
<?php
<?php
Copiar
Copiado
Copiar
Copiado
namespace
Laravel\BrowserKitTesting
\Concerns;
namespace
Illuminate\Foundation\Testing
\Concerns;
Copiar
Copiado
Copiar
Copiado
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;
Copiar
Copiado
Copiar
Copiado
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()
{
{
Copiar
Copiado
Copiar
Copiado
$mock = Mockery::mock(
'Illuminate\Contracts\
Events
\
Dispatcher
'
);
$mock = Mockery::mock(
Events
Dispatcher
Contract::class)->shouldIgnoreMissing(
);
Copiar
Copiado
Copiar
Copiado
$mock->shouldReceive(
'fire',
'dispatch', '
getCommandHandler
')->andReturnUsing(function ($called) {
$mock->shouldReceive(
'dispatch', '
until
')->andReturnUsing(function ($called) {
$this->firedEvents[] = $called;
$this->firedEvents[] = $called;
});
});
Copiar
Copiado
Copiar
Copiado
Event::clearResolvedInstances();
$this->app->instance('events', $mock);
$this->app->instance('events', $mock);
return $this;
return $this;
}
}
/**
/**
Copiar
Copiado
Copiar
Copiado
* 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);
}
}
/**
/**
Copiar
Copiado
Copiar
Copiado
* 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()
{
{
Copiar
Copiado
Copiar
Copiado
$mock = Mockery::mock(
'Illuminate\Contracts\
Bus
\
Dispatcher
'
);
$mock = Mockery::mock(
Bus
Dispatcher
Contract::class)->shouldIgnoreMissing(
);
Copiar
Copiado
Copiar
Copiado
$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(
Copiar
Copiado
Copiar
Copiado
'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;
}
}
}
}
Copiar
Copiado
Copiar
Copiado
$this->fail('The following expected notification
was
not dispatched: ['.$notification.']');
$this->fail('The following expected notification
were
not dispatched: ['.$notification.']');
});
});
return $this;
return $this;
}
}
}
}
Diferencias guardadas
Texto original
Abrir archivo
<?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; } }
Texto modificado
Abrir archivo
<?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; } }
Encontrar la diferencia