-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from ErickTucto/test-session-exception
[5.0] Completed Coverage to 2 traits
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Tests\Stubs; | ||
|
||
use Exception; | ||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Symfony\Component\Console\Application as ConsoleApplication; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class ExceptionHandlerStub implements ExceptionHandler | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function report(Exception $e) | ||
{ | ||
} | ||
|
||
public function shouldReport(Exception $e) | ||
{ | ||
return false; | ||
} | ||
|
||
public function render($request, Exception $e) | ||
{ | ||
if ($e instanceof NotFoundHttpException) { | ||
throw new NotFoundHttpException( | ||
"{$request->method()} {$request->url()}", null, $e->getCode() | ||
); | ||
} | ||
|
||
throw $e; | ||
} | ||
|
||
public function renderForConsole($output, Exception $e) | ||
{ | ||
(new ConsoleApplication)->renderException($e, $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Tests\Stubs; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Formatter\OutputFormatterInterface; | ||
|
||
class OutputStub implements OutputInterface | ||
{ | ||
public function write($messages, $newline = false, $options = 0) {} | ||
public function writeln($messages, $options = 0) {} | ||
public function setVerbosity($level) {} | ||
public function getVerbosity() {} | ||
public function isQuiet() {} | ||
public function isVerbose() {} | ||
public function isVeryVerbose() {} | ||
public function isDebug() {} | ||
public function setDecorated($decorated) {} | ||
public function isDecorated() {} | ||
public function setFormatter(OutputFormatterInterface $formatter) {} | ||
public function getFormatter() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace Laravel\BrowserKitTesting\Tests\Unit; | ||
|
||
use Exception; | ||
use Illuminate\Foundation\Application; | ||
use Laravel\BrowserKitTesting\Tests\TestCase; | ||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Laravel\BrowserKitTesting\Tests\Stubs\OutputStub; | ||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Laravel\BrowserKitTesting\Tests\Stubs\ExceptionHandlerStub; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Laravel\BrowserKitTesting\Concerns\InteractsWithExceptionHandling; | ||
|
||
class InteractsWithExceptionHandlingTest extends TestCase | ||
{ | ||
use InteractsWithExceptionHandling; | ||
/** | ||
* @test | ||
*/ | ||
public function withExceptionHandling_restore_exception_handling() | ||
{ | ||
$this->app = new Application(); | ||
$this->previousExceptionHandler = "MyExceptionHandler"; | ||
$this->withExceptionHandling(); | ||
$this->assertEquals( | ||
app(ExceptionHandler::class), | ||
$this->previousExceptionHandler | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function withoutExceptionHandling_disable_exception_handling_for_the_test() | ||
{ | ||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new ExceptionHandlerStub()); | ||
$this->assertNull($this->previousExceptionHandler); | ||
$this->withoutExceptionHandling(); | ||
$this->assertInstanceOf( | ||
ExceptionHandler::class, | ||
$this->previousExceptionHandler | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function withExceptionHandling_throw_exception_NotFoundHttpException() | ||
{ | ||
$this->expectException(NotFoundHttpException::class); | ||
$this->expectExceptionMessage('Abort 404'); | ||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new class {}); | ||
|
||
$this->withoutExceptionHandling(); | ||
abort(404, 'Abort 404'); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
*/ | ||
public function report_of_instance_ExceptionHandler_on_Application_does_nothing() | ||
{ | ||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new class {}); | ||
|
||
$this->withoutExceptionHandling(); | ||
$this->assertNull(app(ExceptionHandler::class)->report(new Exception)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function render_of_instance_ExceptionHandler_on_Application_throw_exception_NotFoundHttpException() | ||
{ | ||
$this->expectException(NotFoundHttpException::class); | ||
$this->expectExceptionMessage('GET http://localhost'); | ||
|
||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new class {}); | ||
|
||
$request = new class { | ||
public function method() { return "GET"; } | ||
public function url() { return "http://localhost"; } | ||
public function getCode() { return 404; } | ||
}; | ||
|
||
$this->withoutExceptionHandling(); | ||
app(ExceptionHandler::class)->render($request, new NotFoundHttpException); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function render_of_instance_ExceptionHandler_on_Application_throw_exception_anyone() | ||
{ | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('My Exception'); | ||
|
||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new class {}); | ||
|
||
$request = new class {}; | ||
|
||
$this->withoutExceptionHandling(); | ||
|
||
app(ExceptionHandler::class)->render($request, new Exception('My Exception')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function renderForConsole_throw_exception_to_console_and_does_nothing() | ||
{ | ||
$this->app = new Application(); | ||
$this->app->instance(ExceptionHandler::class, new class {}); | ||
$output = new OutputStub; | ||
$this->withoutExceptionHandling(); | ||
|
||
$this->assertNull( | ||
app(ExceptionHandler::class) | ||
->renderForConsole($output, new Exception) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters