MockApplicationServices.php

Created Diff never expires
141 removals
416 lines
12 additions
287 lines
<?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(EventsDispatcherContract::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(BusDispatcherContract::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
BusDispatcherContract::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;
}
}
}
}