Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Codeception/Lib/Connector/Laravel5.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
namespace Codeception\Lib\Connector;

use Codeception\Lib\Connector\Laravel5\ExceptionHandlerDecorator;
use Codeception\Lib\Connector\Laravel5\ExceptionHandlerDecorator as Laravel5ExceptionHandlerDecorator;
use Codeception\Lib\Connector\Laravel7\ExceptionHandlerDecorator as Laravel7ExceptionHandlerDecorator;
use Codeception\Stub;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Application;
Expand Down Expand Up @@ -226,7 +227,12 @@ private function initialize($request = null)

// Replace the Laravel exception handler with our decorated exception handler,
// so exceptions can be intercepted for the disable_exception_handling functionality.
$decorator = new ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
if (version_compare(Application::VERSION, '7.0.0', '<')) {
$decorator = new Laravel5ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
} else {
$decorator = new Laravel7ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
}

$decorator->exceptionHandlingDisabled($this->exceptionHandlingDisabled);
$this->app->instance('Illuminate\Contracts\Debug\ExceptionHandler', $decorator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function report(Exception $e)
{
$this->laravelExceptionHandler->report($e);
}

/**
* Determine if the exception should be reported.
*
Expand Down
117 changes: 117 additions & 0 deletions src/Codeception/Lib/Connector/Laravel7/ExceptionHandlerDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
namespace Codeception\Lib\Connector\Laravel7;

use Throwable;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;

/**
* Class ExceptionHandlerDecorator
*
* @package Codeception\Lib\Connector\Laravel7
*/
class ExceptionHandlerDecorator implements ExceptionHandlerContract
{
/**
* @var ExceptionHandlerContract
*/
private $laravelExceptionHandler;

/**
* @var boolean
*/
private $exceptionHandlingDisabled = true;

/**
* ExceptionHandlerDecorator constructor.
*
* @param object $laravelExceptionHandler
*/
public function __construct($laravelExceptionHandler)
{
$this->laravelExceptionHandler = $laravelExceptionHandler;
}

/**
* @param boolean $exceptionHandlingDisabled
*/
public function exceptionHandlingDisabled($exceptionHandlingDisabled)
{
$this->exceptionHandlingDisabled = $exceptionHandlingDisabled;
}

/**
* Report or log an exception.
*
* @param \Throwable $e
* @return void
*/
public function report(Throwable $e)
{
$this->laravelExceptionHandler->report($e);
}

/**
* Determine if the exception should be reported.
*
* @param \Throwable $e
* @return bool
*/
public function shouldReport(Throwable $e)
{
return $this->exceptionHandlingDisabled;
}

/**
* @param $request
* @param Throwable $e
* @return \Symfony\Component\HttpFoundation\Response
* @throws Throwable
*/
public function render($request, Throwable $e)
{
$response = $this->laravelExceptionHandler->render($request, $e);

if ($this->exceptionHandlingDisabled && $this->isSymfonyExceptionHandlerOutput($response->getContent())) {
// If content was generated by the \Symfony\Component\Debug\ExceptionHandler class
// the Laravel application could not handle the exception,
// so re-throw this exception if the Codeception user disabled Laravel's exception handling.
throw $e;
}

return $response;
}

/**
* Check if the response content is HTML output of the Symfony exception handler class.
*
* @param string $content
* @return bool
*/
private function isSymfonyExceptionHandlerOutput($content)
{
return strpos($content, '<div id="sf-resetcontent" class="sf-reset">') !== false ||
strpos($content, '<div class="exception-summary">') !== false;
}

/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Throwable $e
* @return void
*/
public function renderForConsole($output, Throwable $e)
{
$this->laravelExceptionHandler->renderForConsole($output, $e);
}

/**
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
return call_user_func_array([$this->laravelExceptionHandler, $method], $args);
}
}