Skip to content

Commit

Permalink
Merge pull request #10 from UnicornGlobal/console-concern
Browse files Browse the repository at this point in the history
Console and Container Concerns
  • Loading branch information
albertcht authored Dec 1, 2019
2 parents 7d9182d + f23fd21 commit 1fe2aa3
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ There are some traits you can use in your test case (including original ones in
* `AlbertCht\Lumen\Testing\Concerns\RefreshDatabase`
* `AlbertCht\Lumen\Testing\Concerns\WithFaker`
* `AlbertCht\Lumen\Testing\Concerns\InteractsWithRedis`
* `AlbertCht\Lumen\Testing\Concerns\InteractsWithConsole`
* `AlbertCht\Lumen\Testing\Concerns\InteractsWithContainer`
* `Laravel\Lumen\Testing\DatabaseMigrations`
* `Laravel\Lumen\Testing\DatabaseTransactions`
* `Laravel\Lumen\Testing\WithoutMiddleware`
Expand Down
12 changes: 12 additions & 0 deletions src/Concerns/InteractsWithAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ public function assertInvalidCredentials(array $credentials, $guard = null)
return $this;
}

/**
* Set the currently logged in user for the application.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string|null $driver
* @return void
*/
public function be($user, $driver = null)
{
$this->app['auth']->guard($driver)->setUser($user);
}

/**
* Return true if the credentials are valid, false otherwise.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Concerns/InteractsWithConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace AlbertCht\Lumen\Testing\Concerns;

use Illuminate\Contracts\Console\Kernel;

trait InteractsWithConsole
{
/**
* Call artisan command and return code.
*
* @param string $command
* @param array $parameters
* @return int
*/
public function artisan($command, $parameters = [])
{
return $this->app[Kernel::class]->call($command, $parameters);
}

/**
* Disable mocking the console output.
*
* @return $this
*/
protected function withoutMockingConsoleOutput()
{
$this->mockConsoleOutput = false;
$this->app->offsetUnset(OutputStyle::class);
return $this;
}
}
30 changes: 30 additions & 0 deletions src/Concerns/InteractsWithContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace AlbertCht\Lumen\Testing\Concerns;

trait InteractsWithContainer
{
/**
* Register an instance of an object in the container.
*
* @param string $abstract
* @param object $instance
* @return object
*/
protected function swap($abstract, $instance)
{
return $this->instance($abstract, $instance);
}
/**
* Register an instance of an object in the container.
*
* @param string $abstract
* @param object $instance
* @return object
*/
protected function instance($abstract, $instance)
{
$this->app->instance($abstract, $instance);
return $instance;
}
}
40 changes: 40 additions & 0 deletions src/Concerns/MocksApplicationServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ 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);
if ($jobsNotDispatched = array_diff($jobs, $dispatched)) {
throw new Exception(
'These expected jobs were not dispatched: ['.implode(', ', $jobsNotDispatched).']'
);
}
});
return $this;
}

/**
* Specify a list of jobs that should not be dispatched for the given operation.
*
Expand All @@ -96,6 +119,23 @@ protected function doesntExpectJobs($jobs)
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')->andReturnUsing(function ($dispatched) {
$this->dispatchedJobs[] = $dispatched;
});
$this->app->instance(
'Illuminate\Contracts\Bus\Dispatcher', $mock
);
return $this;
}

/**
* Filter the given jobs against the dispatched jobs.
*
Expand Down
6 changes: 4 additions & 2 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
abstract class TestCase extends LumenTestCase
{
use Concerns\MakesHttpRequests,
Concerns\InteractsWithAuthentication,
Concerns\InteractsWithConsole,
Concerns\InteractsWithContainer,
Concerns\InteractsWithDatabase,
Concerns\InteractsWithExceptionHandling,
Concerns\InteractsWithAuthentication,
Concerns\MocksApplicationServices;

protected static $appPath;
Expand Down Expand Up @@ -64,4 +66,4 @@ protected function getAppPath()

return static::$appPath;
}
}
}

0 comments on commit 1fe2aa3

Please sign in to comment.