Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 11, 2018
1 parent 846193b commit eb65449
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 17 deletions.
34 changes: 22 additions & 12 deletions src/Illuminate/Session/Middleware/StartSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,31 @@ public function __construct(SessionManager $manager)
*/
public function handle($request, Closure $next)
{
if ($this->sessionConfigured()) {
$request->setLaravelSession(
$session = $this->startSession($request)
);
if (! $this->sessionConfigured()) {
return next($request);
}

$this->collectGarbage($session);
// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
$request->setLaravelSession(
$session = $this->startSession($request)
);

$this->storeCurrentUrl($request, $session);
$this->collectGarbage($session);

$response = $next($request);
$this->storeCurrentUrl($request, $session);

$this->addCookieToResponse($response, $session);
$this->addCookieToResponse(
$response = $next($request), $session
);

$this->manager->driver()->save();
}
// Again, if the session has been configured we will need to close out the session
// so that the attributes may be persisted to some storage medium. We will also
// add the session identifier cookie to the application response headers now.
$this->manager->driver()->save();

return $response ?? $next($request);
return $response;
}

/**
Expand Down Expand Up @@ -168,7 +176,9 @@ protected function getCookieExpirationDate()
{
$config = $this->manager->getSessionConfig();

return $config['expire_on_close'] ? 0 : Date::instance(Carbon::now()->addRealMinutes($config['lifetime']));
return $config['expire_on_close'] ? 0 : Date::instance(
Carbon::now()->addRealMinutes($config['lifetime'])
);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Illuminate/Session/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,9 @@ protected function createCacheHandler($driver)
*/
protected function buildSession($handler)
{
if ($this->app['config']['session.encrypt']) {
return $this->buildEncryptedSession($handler);
}

return new Store($this->app['config']['session.cookie'], $handler);
return $this->app['config']['session.encrypt']
? $this->buildEncryptedSession($handler)
: new Store($this->app['config']['session.cookie'], $handler);
}

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/Integration/Session/SessionPersistenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Illuminate\Tests\Integration\Session;

use Mockery;
use Illuminate\Http\Response;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Session\NullSessionHandler;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Contracts\Debug\ExceptionHandler;

/**
* @group integration
*/
class SessionPersistenceTest extends TestCase
{
public function test_session_is_persisted_even_if_exception_is_thrown_from_route()
{
$handler = new FakeNullSessionHandler;
$this->assertFalse($handler->written);

Session::extend('fake-null', function () use ($handler) {
return $handler;
});

Route::get('/', function () {
throw new TokenMismatchException;
})->middleware('web');

$response = $this->get('/');
$this->assertTrue($handler->written);
}

protected function getEnvironmentSetUp($app)
{
$app->instance(
ExceptionHandler::class,
$handler = Mockery::mock(ExceptionHandler::class)->shouldIgnoreMissing()
);

$handler->shouldReceive('render')->andReturn(new Response);

$app['config']->set('app.key', str_random(32));
$app['config']->set('session.driver', 'fake-null');
$app['config']->set('session.expire_on_close', true);
}
}

class FakeNullSessionHandler extends NullSessionHandler
{
public $written = false;

public function write($sessionId, $data)
{
$this->written = true;
return true;
}
}

0 comments on commit eb65449

Please sign in to comment.