Skip to content

Commit

Permalink
Tweak Symfony adapter test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
eddmann committed Sep 21, 2018
1 parent a827232 commit 1588cc2
Showing 1 changed file with 50 additions and 26 deletions.
76 changes: 50 additions & 26 deletions tests/Bridge/Symfony/SymfonyAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

class SymfonyAdapterTest extends TestCase
{
private const ROUTE_WITHOUT_SESSION = '/';
private const ROUTE_WITH_SESSION = '/session';
private const ROUTE_NOT_FOUND = '/not-found';

public function setUp()
{
parent::setUp();
Expand All @@ -33,47 +37,56 @@ public function setUp()

public function test Symfony applications are adapted()
{
$kernel = $this->createKernel();
$kernel->boot();
$adapter = new SymfonyAdapter($kernel);
$response = $adapter->handle(new ServerRequest([], [], '/foo'));
$adapter = new SymfonyAdapter($this->createKernel());

$response = $adapter->handle(new ServerRequest([], [], self::ROUTE_WITHOUT_SESSION));

self::assertSame('Hello world!', (string) $response->getBody());
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('Hello world!', (string) $response->getBody());
}

public function test 404 are PSR7 responses and not exceptions()
{
$kernel = $this->createKernel();
$kernel->boot();
$adapter = new SymfonyAdapter($kernel);
$adapter = new SymfonyAdapter($this->createKernel());

$response = $adapter->handle(new ServerRequest([], [], '/bar'));
$response = $adapter->handle(new ServerRequest([], [], self::ROUTE_NOT_FOUND));

self::assertSame(404, $response->getStatusCode());
self::assertEquals(404, $response->getStatusCode());
self::assertEquals('Not found', (string) $response->getBody());
}

public function test an active session is created()
public function test session is not created when sessions not used()
{
$kernel = $this->createKernel();
$kernel->boot();
$adapter = new SymfonyAdapter($kernel);
$adapter = new SymfonyAdapter($this->createKernel());

$response = $adapter->handle(new ServerRequest([], [], '/foo'));
$response = $adapter->handle(new ServerRequest([], [], self::ROUTE_WITHOUT_SESSION));

self::assertArrayHasKey('Set-Cookie', $response->getHeaders());
self::assertArrayNotHasKey('Set-Cookie', $response->getHeaders());
}

public function test an active session is retrieved()
public function test an active session is created when sessions used()
{
$kernel = $this->createKernel();
$kernel->boot();
$adapter = new SymfonyAdapter($kernel);
$adapter = new SymfonyAdapter($kernel = $this->createKernel());

$response = $adapter->handle(new ServerRequest([], [], self::ROUTE_WITH_SESSION));

$symfonySessionId = $kernel->getContainer()->get('session')->getId();
self::assertEquals($symfonySessionId, (string) $response->getBody());
self::assertEquals(
sprintf("%s=%s; path=/", \session_name(), $symfonySessionId),
$response->getHeaders()['Set-Cookie'][0]
);
}

public function test an existing session is used when session provided()
{
$adapter = new SymfonyAdapter($this->createKernel());

$response = $adapter->handle(
new ServerRequest(
[],
[],
'/foo',
self::ROUTE_WITH_SESSION,
null,
'php://input',
[],
Expand All @@ -82,11 +95,12 @@ public function test an active session is retrieved()
);

self::assertArrayNotHasKey('Set-Cookie', $response->getHeaders());
self::assertEquals('SESSIONID', (string) $response->getBody());
}

private function createKernel(): HttpKernelInterface
{
return new class('dev', false) extends Kernel implements EventSubscriberInterface {
$kernel = new class('dev', false) extends Kernel implements EventSubscriberInterface {
use MicroKernelTrait;

public function registerBundles()
Expand All @@ -108,16 +122,22 @@ protected function configureContainer(ContainerBuilder $c)

protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/foo', 'kernel:testAction');
$routes->add('/', 'kernel:testActionWithoutSession');
$routes->add('/session', 'kernel:testActionWithSession');
}

public function testAction(Session $session)
public function testActionWithoutSession()
{
$session->set('ACTIVATE', 'SESSIONS');

return new Response('Hello world!');
}

public function testActionWithSession(Session $session)
{
$session->set('ACTIVATE', 'SESSIONS'); // ensure that Symfony starts/uses sessions

return new Response($session->getId());
}

public static function getSubscribedEvents()
{
return [KernelEvents::EXCEPTION => 'onKernelException'];
Expand All @@ -133,5 +153,9 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}
}
};

$kernel->boot();

return $kernel;
}
}

0 comments on commit 1588cc2

Please sign in to comment.