SessionMiddlewareTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Http\Response;
  16. class SessionMiddlewareTest extends \Test\TestCase {
  17. /**
  18. * @var ControllerMethodReflector
  19. */
  20. private $reflector;
  21. /**
  22. * @var Request
  23. */
  24. private $request;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->request = new Request(
  28. [],
  29. $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
  30. $this->getMockBuilder('\OCP\IConfig')->getMock()
  31. );
  32. $this->reflector = new ControllerMethodReflector();
  33. }
  34. /**
  35. * @UseSession
  36. */
  37. public function testSessionNotClosedOnBeforeController() {
  38. $session = $this->getSessionMock(0);
  39. $this->reflector->reflect($this, __FUNCTION__);
  40. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  41. $middleware->beforeController($this, __FUNCTION__);
  42. }
  43. /**
  44. * @UseSession
  45. */
  46. public function testSessionClosedOnAfterController() {
  47. $session = $this->getSessionMock(1);
  48. $this->reflector->reflect($this, __FUNCTION__);
  49. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  50. $middleware->afterController($this, __FUNCTION__, new Response());
  51. }
  52. public function testSessionClosedOnBeforeController() {
  53. $session = $this->getSessionMock(1);
  54. $this->reflector->reflect($this, __FUNCTION__);
  55. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  56. $middleware->beforeController($this, __FUNCTION__);
  57. }
  58. public function testSessionNotClosedOnAfterController() {
  59. $session = $this->getSessionMock(0);
  60. $this->reflector->reflect($this, __FUNCTION__);
  61. $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
  62. $middleware->afterController($this, __FUNCTION__, new Response());
  63. }
  64. /**
  65. * @return mixed
  66. */
  67. private function getSessionMock($expectedCloseCount) {
  68. $session = $this->getMockBuilder('\OC\Session\Memory')
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $session->expects($this->exactly($expectedCloseCount))
  72. ->method('close');
  73. return $session;
  74. }
  75. }