Skip to content

Commit

Permalink
Merge pull request #11 from UnicornGlobal/feat/with-exception
Browse files Browse the repository at this point in the history
Feat/with exception
  • Loading branch information
albertcht authored Dec 1, 2019
2 parents 1fe2aa3 + 4daa5af commit fabf164
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 63 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "albertcht/lumen-testing",
"name": "unicorn/lumen-testing",
"description": "Testing Suite For Lumen like Laravel does.",
"keywords": ["phpunit", "test", "lumen", "testing", "laravel", "laravel"],
"license": "MIT",
Expand All @@ -10,7 +10,6 @@
"require": {
"php": "^7.1",
"laravel/lumen-framework": "~5.3|~6.0"

},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand Down
26 changes: 26 additions & 0 deletions src/Concerns/InteractsWithContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace AlbertCht\Lumen\Testing\Concerns;

use Closure;
use Mockery;

trait InteractsWithContainer
{
/**
Expand All @@ -27,4 +30,27 @@ protected function instance($abstract, $instance)
$this->app->instance($abstract, $instance);
return $instance;
}

/**
* Mock an instance of an object in the container.
*
* @param string $abstract
* @param \Closure|null $mock
* @return object
*/
protected function mock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args())));
}
/**
* Spy an instance of an object in the container.
*
* @param string $abstract
* @param \Closure|null $mock
* @return object
*/
protected function spy($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
}
}
143 changes: 82 additions & 61 deletions src/Concerns/InteractsWithExceptionHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ trait InteractsWithExceptionHandling
protected function withExceptionHandling()
{
if ($this->originalExceptionHandler) {
$this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler);
$this->app->instance(ExceptionHandler::class,
$this->originalExceptionHandler);
}

return $this;
Expand All @@ -35,6 +36,7 @@ protected function withExceptionHandling()
* Only handle the given exceptions via the exception handler.
*
* @param array $exceptions
*
* @return $this
*/
protected function handleExceptions(array $exceptions)
Expand All @@ -56,6 +58,7 @@ protected function handleValidationExceptions()
* Disable exception handling for the test.
*
* @param array $except
*
* @return $this
*/
protected function withoutExceptionHandling(array $except = [])
Expand All @@ -64,72 +67,90 @@ protected function withoutExceptionHandling(array $except = [])
$this->originalExceptionHandler = app(ExceptionHandler::class);
}

$this->app->instance(ExceptionHandler::class, new class($this->originalExceptionHandler, $except) implements ExceptionHandler {
protected $except;
protected $originalHandler;

/**
* Create a new class instance.
*
* @param \Illuminate\Contracts\Debug\ExceptionHandler
* @param array $except
* @return void
*/
public function __construct($originalHandler, $except = [])
{
$this->except = $except;
$this->originalHandler = $originalHandler;
}

/**
* Report the given exception.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
//
}

/**
* Render the given exception.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Exception
*/
public function render($request, Exception $e)
$this->app->instance(ExceptionHandler::class,
new class($this->originalExceptionHandler, $except) implements
ExceptionHandler
{
if ($e instanceof NotFoundHttpException) {
throw new NotFoundHttpException(
"{$request->method()} {$request->url()}", null, $e->getCode()
);
protected $except;
protected $originalHandler;

/**
* Create a new class instance.
*
* @param \Illuminate\Contracts\Debug\ExceptionHandler
* @param array $except
*
* @return void
*/
public function __construct($originalHandler, $except = [])
{
$this->except = $except;
$this->originalHandler = $originalHandler;
}

foreach ($this->except as $class) {
if ($e instanceof $class) {
return $this->originalHandler->render($request, $e);
/**
* Report the given exception.
*
* @param \Exception $e
*
* @return void
*/
public function report(Exception $e)
{
//
}

/**
* Render the given exception.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
*
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Exception
*/
public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
throw new NotFoundHttpException("{$request->method()} {$request->url()}",
null, $e->getCode());
}

foreach ($this->except as $class) {
if ($e instanceof $class) {
return $this->originalHandler->render($request, $e);
}
}

throw $e;
}

throw $e;
}

/**
* Render the exception for the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface
* @param \Exception $e
* @return void
*/
public function renderForConsole($output, Exception $e)
{
(new ConsoleApplication)->renderException($e, $output);
}
});
/**
* Render the exception for the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface
* @param \Exception $e
*
* @return void
*/
public function renderForConsole($output, Exception $e)
{
(new ConsoleApplication)->renderException($e, $output);
}

/**
* Determine if the exception should be reported.
*
* @param \Exception $e
*
* @return bool
*/
public function shouldReport(Exception $e)
{
return false;
}
});

return $this;
}
Expand Down
Loading

0 comments on commit fabf164

Please sign in to comment.