SessionStorageTest.php 3.3 KB

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