Browse Source

4.x - Explicit write & close of the session
- only executed if CakePHP ServerRequest is used (we know that it uses CakePHP Session object)
- users of different PSR request implementations do have to handle their own session manually

Ján Súkeník 6 years ago
parent
commit
a66b2373ca
2 changed files with 29 additions and 3 deletions
  1. 3 1
      src/Http/Server.php
  2. 26 2
      tests/TestCase/Http/ServerTest.php

+ 3 - 1
src/Http/Server.php

@@ -89,7 +89,9 @@ class Server implements EventDispatcherInterface
 
         $response = $this->runner->run($middleware, $request, $this->app);
 
-        $request->getSession()->close();
+        if ($request instanceof ServerRequest) {
+            $request->getSession()->close();
+        }
 
         return $response;
     }

+ 26 - 2
tests/TestCase/Http/ServerTest.php

@@ -192,9 +192,9 @@ class ServerTest extends TestCase
     }
 
     /**
-     * Test that run closes session after invoking the application.
+     * Test that run closes session after invoking the application (if CakePHP ServerRequest is used).
      */
-    public function testRunClosesSession()
+    public function testRunClosesSessionIfServerRequestUsed()
     {
         $sessionMock = $this->createMock(Session::class);
 
@@ -220,6 +220,30 @@ class ServerTest extends TestCase
     }
 
     /**
+     * Test that run does not close the session if CakePHP ServerRequest is not used.
+     */
+    public function testRunDoesNotCloseSessionIfServerRequestNotUsed()
+    {
+        $request = new \Zend\Diactoros\ServerRequest();
+
+        $app = new MiddlewareApplication($this->config);
+        $server = new Server($app);
+        $res = $server->run($request);
+
+        // assert that app was executed correctly
+        $this->assertSame(
+            200,
+            $res->getStatusCode(),
+            "Application was expected to be executed"
+        );
+        $this->assertSame(
+            'source header',
+            $res->getHeaderLine('X-testing'),
+            "Application was expected to be executed"
+        );
+    }
+
+    /**
      * Test that emit invokes the appropriate methods on the emitter.
      *
      * @return void