Skip to content

Commit

Permalink
Merge pull request #93 from ErickTucto/test-session-exception
Browse files Browse the repository at this point in the history
[5.0] Completed Coverage to 2 traits
  • Loading branch information
taylorotwell authored Mar 3, 2019
2 parents d4c5b43 + 05470f8 commit bfb016e
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Concerns/InteractsWithExceptionHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function report(Exception $e)
{
}

public function shouldReport(Exception $e)
{
return false;
}

public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
Expand Down
40 changes: 40 additions & 0 deletions tests/Stubs/ExceptionHandlerStub.php
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);
}
}
22 changes: 22 additions & 0 deletions tests/Stubs/OutputStub.php
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() {}
}
129 changes: 129 additions & 0 deletions tests/Unit/InteractsWithExceptionHandlingTest.php
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)
);
}
}
20 changes: 19 additions & 1 deletion tests/Unit/InteractsWithSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Laravel\BrowserKitTesting\Tests\Unit;

use Laravel\BrowserKitTesting\Concerns\InteractsWithSession;
use Illuminate\Foundation\Application;
use Laravel\BrowserKitTesting\Tests\TestCase;
use Laravel\BrowserKitTesting\Concerns\InteractsWithSession;

class InteractsWithSessionTest extends TestCase
{
Expand Down Expand Up @@ -160,6 +161,23 @@ public function has($key) { return true; }
$this->assertSessionHasErrors(['foo', 'bar']);
}

/**
* @test
*/
public function check_if_exists_errors_with_value_on_session()
{
$this->app = new Application();
$this->app['session.store'] = new class {
public function get($key) {
return new class {
public function get($key) { return ['bar']; }
};
}
public function has($key) { return true; }
};
$this->assertSessionHasErrors(['foo' => 'bar']);
}

/**
* @test
*/
Expand Down

0 comments on commit bfb016e

Please sign in to comment.