SessionPersistenceTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Illuminate\Tests\Integration\Session;
  3. use Illuminate\Contracts\Debug\ExceptionHandler;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Session\NullSessionHandler;
  6. use Illuminate\Session\TokenMismatchException;
  7. use Illuminate\Support\Facades\Route;
  8. use Illuminate\Support\Facades\Session;
  9. use Illuminate\Support\Str;
  10. use Mockery;
  11. use Orchestra\Testbench\TestCase;
  12. class SessionPersistenceTest extends TestCase
  13. {
  14. public function testSessionIsPersistedEvenIfExceptionIsThrownFromRoute()
  15. {
  16. $handler = new FakeNullSessionHandler;
  17. $this->assertFalse($handler->written);
  18. Session::extend('fake-null', function () use ($handler) {
  19. return $handler;
  20. });
  21. Route::get('/', function () {
  22. throw new TokenMismatchException;
  23. })->middleware('web');
  24. $this->get('/');
  25. $this->assertTrue($handler->written);
  26. }
  27. protected function getEnvironmentSetUp($app)
  28. {
  29. $app->instance(
  30. ExceptionHandler::class,
  31. $handler = Mockery::mock(ExceptionHandler::class)->shouldIgnoreMissing()
  32. );
  33. $handler->shouldReceive('render')->andReturn(new Response);
  34. $app['config']->set('app.key', Str::random(32));
  35. $app['config']->set('session.driver', 'fake-null');
  36. $app['config']->set('session.expire_on_close', true);
  37. }
  38. }
  39. class FakeNullSessionHandler extends NullSessionHandler
  40. {
  41. public $written = false;
  42. public function write($sessionId, $data)
  43. {
  44. $this->written = true;
  45. return true;
  46. }
  47. }