SessionMiddlewareTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later. See the COPYING file.
  7. *
  8. * @author Thomas Müller <deepdiver@owncloud.com>
  9. * @copyright Thomas Müller 2014
  10. */
  11. namespace Test\AppFramework\Middleware;
  12. use OC\AppFramework\Http\Request;
  13. use OC\AppFramework\Middleware\SessionMiddleware;
  14. use OC\AppFramework\Utility\ControllerMethodReflector;
  15. use OCP\AppFramework\Controller;
  16. use OCP\AppFramework\Http\Response;
  17. use OCP\IConfig;
  18. class SessionMiddlewareTest extends \Test\TestCase {
  19. /** @var ControllerMethodReflector */
  20. private $reflector;
  21. /** @var Request */
  22. private $request;
  23. /** @var Controller */
  24. private $controller;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->request = new Request(
  28. [],
  29. $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
  30. $this->getMockBuilder(IConfig::class)->getMock()
  31. );
  32. $this->reflector = new ControllerMethodReflector();
  33. $this->controller = $this->createMock(Controller::class);
  34. }
  35. /**
  36. * @UseSession
  37. */
  38. public function testSessionNotClosedOnBeforeController() {
  39. $session = $this->getSessionMock(0);
  40. $this->reflector->reflect($this, __FUNCTION__);
  41. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  42. $middleware->beforeController($this->controller, __FUNCTION__);
  43. }
  44. /**
  45. * @UseSession
  46. */
  47. public function testSessionClosedOnAfterController() {
  48. $session = $this->getSessionMock(1);
  49. $this->reflector->reflect($this, __FUNCTION__);
  50. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  51. $middleware->afterController($this->controller, __FUNCTION__, new Response());
  52. }
  53. public function testSessionClosedOnBeforeController() {
  54. $session = $this->getSessionMock(1);
  55. $this->reflector->reflect($this, __FUNCTION__);
  56. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  57. $middleware->beforeController($this->controller, __FUNCTION__);
  58. }
  59. public function testSessionNotClosedOnAfterController() {
  60. $session = $this->getSessionMock(0);
  61. $this->reflector->reflect($this, __FUNCTION__);
  62. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  63. $middleware->afterController($this->controller, __FUNCTION__, new Response());
  64. }
  65. /**
  66. * @return mixed
  67. */
  68. private function getSessionMock($expectedCloseCount) {
  69. $session = $this->getMockBuilder('\OC\Session\Memory')
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $session->expects($this->exactly($expectedCloseCount))
  73. ->method('close');
  74. return $session;
  75. }
  76. }