SessionStorageTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Auth\Storage;
  16. use Cake\Auth\Storage\SessionStorage;
  17. use Cake\Http\Response;
  18. use Cake\Http\ServerRequest;
  19. use Cake\Http\Session;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Test case for SessionStorage
  23. */
  24. class SessionStorageTest extends TestCase
  25. {
  26. /**
  27. * setup
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. $this->session = $this->getMockBuilder(Session::class)->getMock();
  35. $this->request = new ServerRequest(['session' => $this->session]);
  36. $this->response = new Response();
  37. $this->storage = new SessionStorage($this->request, $this->response, ['key' => 'Auth.AuthUser']);
  38. $this->user = ['id' => 1];
  39. }
  40. /**
  41. * Test write
  42. *
  43. * @return void
  44. */
  45. public function testWrite()
  46. {
  47. $this->session->expects($this->once())
  48. ->method('write')
  49. ->with('Auth.AuthUser', $this->user)
  50. ->will($this->returnValue(true));
  51. $this->storage->write($this->user);
  52. }
  53. /**
  54. * Test read
  55. *
  56. * @return void
  57. */
  58. public function testRead()
  59. {
  60. $this->session->expects($this->once())
  61. ->method('read')
  62. ->with('Auth.AuthUser')
  63. ->will($this->returnValue($this->user));
  64. $result = $this->storage->read();
  65. $this->assertSame($this->user, $result);
  66. }
  67. /**
  68. * Test read from local var
  69. *
  70. * @return void
  71. */
  72. public function testGetFromLocalVar()
  73. {
  74. $this->storage->write($this->user);
  75. $this->session->expects($this->never())
  76. ->method('read');
  77. $result = $this->storage->read();
  78. $this->assertSame($this->user, $result);
  79. }
  80. /**
  81. * Test delete
  82. *
  83. * @return void
  84. */
  85. public function testDelete()
  86. {
  87. $this->session->expects($this->once())
  88. ->method('delete')
  89. ->with('Auth.AuthUser');
  90. $this->storage->delete();
  91. }
  92. /**
  93. * Test redirectUrl()
  94. *
  95. * @return void
  96. */
  97. public function redirectUrl()
  98. {
  99. $url = '/url';
  100. $this->session->expects($this->once())
  101. ->method('write')
  102. ->with('Auth.redirectUrl', $url);
  103. $this->storage->redirectUrl($url);
  104. $this->session->expects($this->once())
  105. ->method('read')
  106. ->with('Auth.redirectUrl')
  107. ->will($this->returnValue($url));
  108. $result = $this->storage->redirectUrl();
  109. $this->assertEquals($url, $result);
  110. $this->session->expects($this->once())
  111. ->method('delete')
  112. ->with('Auth.redirectUrl');
  113. $this->storage->redirectUrl(false);
  114. }
  115. }