summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-12-28 03:28:33 +0100
committermsquare <msquare@notrademark.de>2018-12-28 20:35:44 +0100
commit491ee376517cded3c9c8d2389e3f9f21daa1a407 (patch)
tree7def296bc1e53691bb7d5b79c542e002c009ed24 /tests/Unit/Middleware
parent7b3901211a0165558eebca8fe7490ca79b09f97b (diff)
Don't save sessions permanently on api and metrics paths
closes #530 (Session on API calls)
Diffstat (limited to 'tests/Unit/Middleware')
-rw-r--r--tests/Unit/Middleware/RouteDispatcherTest.php3
-rw-r--r--tests/Unit/Middleware/SessionHandlerServiceProviderTest.php44
-rw-r--r--tests/Unit/Middleware/SessionHandlerTest.php61
3 files changed, 107 insertions, 1 deletions
diff --git a/tests/Unit/Middleware/RouteDispatcherTest.php b/tests/Unit/Middleware/RouteDispatcherTest.php
index 611d3b7c..d6ed3408 100644
--- a/tests/Unit/Middleware/RouteDispatcherTest.php
+++ b/tests/Unit/Middleware/RouteDispatcherTest.php
@@ -32,10 +32,11 @@ class RouteDispatcherTest extends TestCase
->with('HEAD', '/foo!bar')
->willReturn([FastRouteDispatcher::FOUND, $handler, ['foo' => 'bar', 'lorem' => 'ipsum']]);
- $request->expects($this->exactly(3))
+ $request->expects($this->exactly(4))
->method('withAttribute')
->withConsecutive(
['route-request-handler', $handler],
+ ['route-request-path', '/foo!bar'],
['foo', 'bar'],
['lorem', 'ipsum']
)
diff --git a/tests/Unit/Middleware/SessionHandlerServiceProviderTest.php b/tests/Unit/Middleware/SessionHandlerServiceProviderTest.php
new file mode 100644
index 00000000..bb4f7e92
--- /dev/null
+++ b/tests/Unit/Middleware/SessionHandlerServiceProviderTest.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\SessionHandler;
+use Engelsystem\Middleware\SessionHandlerServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use Illuminate\Contracts\Container\ContextualBindingBuilder;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class SessionHandlerServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Middleware\SessionHandlerServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var ContextualBindingBuilder|MockObject $bindingBuilder */
+ $bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
+ $app = $this->getApp(['when']);
+
+ $app->expects($this->once())
+ ->method('when')
+ ->with(SessionHandler::class)
+ ->willReturn($bindingBuilder);
+
+ $bindingBuilder->expects($this->once())
+ ->method('needs')
+ ->with('$paths')
+ ->willReturn($bindingBuilder);
+
+ $bindingBuilder->expects($this->once())
+ ->method('give')
+ ->willReturnCallback(function (callable $callable) {
+ $paths = $callable();
+
+ $this->assertTrue(is_array($paths));
+ $this->assertTrue(in_array('/metrics', $paths));
+ });
+
+ $serviceProvider = new SessionHandlerServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Middleware/SessionHandlerTest.php b/tests/Unit/Middleware/SessionHandlerTest.php
new file mode 100644
index 00000000..4abd71a6
--- /dev/null
+++ b/tests/Unit/Middleware/SessionHandlerTest.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Middleware\SessionHandler;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
+
+class SessionHandlerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\SessionHandler::__construct
+ * @covers \Engelsystem\Middleware\SessionHandler::process
+ */
+ public function testProcess()
+ {
+ /** @var NativeSessionStorage|MockObject $sessionStorage */
+ $sessionStorage = $this->createMock(NativeSessionStorage::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+
+ $handler->expects($this->exactly(2))
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $request->expects($this->exactly(2))
+ ->method('getCookieParams')
+ ->willReturnOnConsecutiveCalls([], ['SESSION' => 'BlaFoo']);
+
+ $request->expects($this->exactly(2))
+ ->method('getAttribute')
+ ->with('route-request-path')
+ ->willReturn('/foo');
+
+ $sessionStorage->expects($this->exactly(2))
+ ->method('getName')
+ ->willReturn('SESSION');
+
+ /** @var SessionHandler|MockObject $middleware */
+ $middleware = $this->getMockBuilder(SessionHandler::class)
+ ->setConstructorArgs([$sessionStorage, ['/foo']])
+ ->setMethods(['destroyNative'])
+ ->getMock();
+
+ $middleware->expects($this->once())
+ ->method('destroyNative')
+ ->willReturn(true);
+
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ }
+}