Skip to content

Commit

Permalink
add fakes for bus, events, mail, queue, notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 8, 2016
1 parent 64a34f8 commit 5deab59
Show file tree
Hide file tree
Showing 11 changed files with 1,030 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Facades/Bus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

namespace Illuminate\Support\Facades;

use Illuminate\Support\Testing\Fakes\BusFake;

/**
* @see \Illuminate\Contracts\Bus\Dispatcher
*/
class Bus extends Facade
{
/**
* Replace the bound instance with a fake.
*
* @return void
*/
public static function fake()
{
static::swap(new BusFake);
}

/**
* Get the registered name of the component.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Facades/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

namespace Illuminate\Support\Facades;

use Illuminate\Support\Testing\Fakes\EventFake;

/**
* @see \Illuminate\Events\Dispatcher
*/
class Event extends Facade
{
/**
* Replace the bound instance with a fake.
*
* @return void
*/
public static function fake()
{
static::swap(new EventFake);
}

/**
* Get the registered name of the component.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Facades/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

namespace Illuminate\Support\Facades;

use Illuminate\Support\Testing\Fakes\MailFake;

/**
* @see \Illuminate\Mail\Mailer
*/
class Mail extends Facade
{
/**
* Replace the bound instance with a fake.
*
* @return void
*/
public static function fake()
{
static::swap(new MailFake);
}

/**
* Get the registered name of the component.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Facades/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

namespace Illuminate\Support\Facades;

use Illuminate\Support\Testing\Fakes\NotificationFake;

/**
* @see \Illuminate\Notifications\ChannelManager
*/
class Notification extends Facade
{
/**
* Replace the bound instance with a fake.
*
* @return void
*/
public static function fake()
{
static::swap(new NotificationFake);
}

/**
* Get the registered name of the component.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Facades/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace Illuminate\Support\Facades;

use Illuminate\Support\Testing\Fakes\QueueFake;

/**
* @see \Illuminate\Queue\QueueManager
* @see \Illuminate\Queue\Queue
*/
class Queue extends Facade
{
/**
* Replace the bound instance with a fake.
*
* @return void
*/
public static function fake()
{
static::swap(new QueueFake);
}

/**
* Get the registered name of the component.
*
Expand Down
117 changes: 117 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Illuminate\Support\Testing\Fakes;

use Illuminate\Contracts\Bus\Dispatcher;
use PHPUnit_Framework_Assert as PHPUnit;

class BusFake implements Dispatcher
{
/**
* The commands that have been dispatched.
*
* @var array
*/
protected $commands = [];

/**
* Assert if a job was dispatched based on a truth-test callback.
*
* @param string $command
* @param callable|null $callback
* @return void
*/
public function assertDispatched($command, $callback = null)
{
PHPUnit::assertTrue(
$this->dispatched($command, $callback)->count() > 0,
"The expected [{$command}] job was not dispatched."
);
}

/**
* Determine if a job was dispatched based on a truth-test callback.
*
* @param string $command
* @param callable|null $callback
* @return void
*/
public function assertNotDispatched($command, $callback = null)
{
PHPUnit::assertTrue(
$this->dispatched($command, $callback)->count() === 0,
"The unexpected [{$command}] job was dispatched."
);
}

/**
* Get all of the jobs matching a truth-test callback.
*
* @param string $command
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function dispatched($command, $callback = null)
{
if (! $this->hasDispatched($command)) {
return collect();
}

$callback = $callback ?: function () {
return true;
};

if (is_null($callback)) {

This comment has been minimized.

Copy link
@ConnorVG

ConnorVG Sep 8, 2016

Contributor

This will never be true?

This comment has been minimized.

Copy link
@kamranahmedse

kamranahmedse Sep 12, 2016

Indeed. If it is ever null, it will be assigned the default closure at line#60.

return collect($this->commands[$command]);
}

return collect($this->commands[$command])->filter(function ($command) use ($callback) {
return $callback($command);
});
}

/**
* Determine if there are any stored commands for a given class.
*
* @param string $command
* @return bool
*/
public function hasDispatched($command)
{
return isset($this->commands[$command]) && ! empty($this->commands[$command]);

This comment has been minimized.

Copy link
@kamranahmedse

kamranahmedse Sep 12, 2016

return !empty($this->commands[$command]);

would suffice as empty does check for the existence as well. http://php.net/manual/en/function.empty.php

}

/**
* Dispatch a command to its appropriate handler.
*
* @param mixed $command
* @return mixed
*/
public function dispatch($command)
{
return $this->dispatchNow($command);
}

/**
* Dispatch a command to its appropriate handler in the current process.
*
* @param mixed $command
* @param mixed $handler
* @return mixed
*/
public function dispatchNow($command, $handler = null)
{
$this->commands[get_class($command)][] = $command;
}

/**
* Set the pipes commands should be piped through before dispatching.
*
* @param array $pipes
* @return $this
*/
public function pipeThrough(array $pipes)
{
//
}
}
Loading

0 comments on commit 5deab59

Please sign in to comment.