Skip to content

Commit d6bcc25

Browse files
authored
Merge pull request #5 from Codeception/laravel7
Laravel 7 support
2 parents 1cc1134 + fbf963f commit d6bcc25

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

src/Codeception/Lib/Connector/Laravel5.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace Codeception\Lib\Connector;
33

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

227228
// Replace the Laravel exception handler with our decorated exception handler,
228229
// so exceptions can be intercepted for the disable_exception_handling functionality.
229-
$decorator = new ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
230+
if (version_compare(Application::VERSION, '7.0.0', '<')) {
231+
$decorator = new Laravel5ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
232+
} else {
233+
$decorator = new Laravel7ExceptionHandlerDecorator($this->app['Illuminate\Contracts\Debug\ExceptionHandler']);
234+
}
235+
230236
$decorator->exceptionHandlingDisabled($this->exceptionHandlingDisabled);
231237
$this->app->instance('Illuminate\Contracts\Debug\ExceptionHandler', $decorator);
232238

src/Codeception/Lib/Connector/Laravel5/ExceptionHandlerDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function report(Exception $e)
4949
{
5050
$this->laravelExceptionHandler->report($e);
5151
}
52-
52+
5353
/**
5454
* Determine if the exception should be reported.
5555
*
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
namespace Codeception\Lib\Connector\Laravel7;
3+
4+
use Throwable;
5+
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
6+
7+
/**
8+
* Class ExceptionHandlerDecorator
9+
*
10+
* @package Codeception\Lib\Connector\Laravel7
11+
*/
12+
class ExceptionHandlerDecorator implements ExceptionHandlerContract
13+
{
14+
/**
15+
* @var ExceptionHandlerContract
16+
*/
17+
private $laravelExceptionHandler;
18+
19+
/**
20+
* @var boolean
21+
*/
22+
private $exceptionHandlingDisabled = true;
23+
24+
/**
25+
* ExceptionHandlerDecorator constructor.
26+
*
27+
* @param object $laravelExceptionHandler
28+
*/
29+
public function __construct($laravelExceptionHandler)
30+
{
31+
$this->laravelExceptionHandler = $laravelExceptionHandler;
32+
}
33+
34+
/**
35+
* @param boolean $exceptionHandlingDisabled
36+
*/
37+
public function exceptionHandlingDisabled($exceptionHandlingDisabled)
38+
{
39+
$this->exceptionHandlingDisabled = $exceptionHandlingDisabled;
40+
}
41+
42+
/**
43+
* Report or log an exception.
44+
*
45+
* @param \Throwable $e
46+
* @return void
47+
*/
48+
public function report(Throwable $e)
49+
{
50+
$this->laravelExceptionHandler->report($e);
51+
}
52+
53+
/**
54+
* Determine if the exception should be reported.
55+
*
56+
* @param \Throwable $e
57+
* @return bool
58+
*/
59+
public function shouldReport(Throwable $e)
60+
{
61+
return $this->exceptionHandlingDisabled;
62+
}
63+
64+
/**
65+
* @param $request
66+
* @param Throwable $e
67+
* @return \Symfony\Component\HttpFoundation\Response
68+
* @throws Throwable
69+
*/
70+
public function render($request, Throwable $e)
71+
{
72+
$response = $this->laravelExceptionHandler->render($request, $e);
73+
74+
if ($this->exceptionHandlingDisabled && $this->isSymfonyExceptionHandlerOutput($response->getContent())) {
75+
// If content was generated by the \Symfony\Component\Debug\ExceptionHandler class
76+
// the Laravel application could not handle the exception,
77+
// so re-throw this exception if the Codeception user disabled Laravel's exception handling.
78+
throw $e;
79+
}
80+
81+
return $response;
82+
}
83+
84+
/**
85+
* Check if the response content is HTML output of the Symfony exception handler class.
86+
*
87+
* @param string $content
88+
* @return bool
89+
*/
90+
private function isSymfonyExceptionHandlerOutput($content)
91+
{
92+
return strpos($content, '<div id="sf-resetcontent" class="sf-reset">') !== false ||
93+
strpos($content, '<div class="exception-summary">') !== false;
94+
}
95+
96+
/**
97+
* Render an exception to the console.
98+
*
99+
* @param \Symfony\Component\Console\Output\OutputInterface $output
100+
* @param \Throwable $e
101+
* @return void
102+
*/
103+
public function renderForConsole($output, Throwable $e)
104+
{
105+
$this->laravelExceptionHandler->renderForConsole($output, $e);
106+
}
107+
108+
/**
109+
* @param string $method
110+
* @param array $args
111+
* @return mixed
112+
*/
113+
public function __call($method, $args)
114+
{
115+
return call_user_func_array([$this->laravelExceptionHandler, $method], $args);
116+
}
117+
}

0 commit comments

Comments
 (0)