SessionStorageTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Auth\Storage;
  17. use Cake\Auth\Storage\SessionStorage;
  18. use Cake\Http\Response;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Http\Session;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Test case for SessionStorage
  24. */
  25. class SessionStorageTest extends TestCase
  26. {
  27. /**
  28. * setup
  29. *
  30. * @return void
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->session = $this->getMockBuilder(Session::class)->getMock();
  36. $this->request = new ServerRequest(['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(): void
  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(): void
  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(): void
  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(): void
  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(): void
  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. }