SessionStorageTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @var \Cake\Http\Session|\PHPUnit\Framework\MockObject\MockObject
  29. */
  30. protected $session;
  31. /**
  32. * @var \Cake\Auth\Storage\SessionStorage
  33. */
  34. protected $storage;
  35. /**
  36. * @var array<string, mixed>
  37. */
  38. protected $user;
  39. /**
  40. * setup
  41. */
  42. public function setUp(): void
  43. {
  44. parent::setUp();
  45. $this->session = $this->getMockBuilder(Session::class)->getMock();
  46. $request = new ServerRequest(['session' => $this->session]);
  47. $response = new Response();
  48. $this->storage = new SessionStorage($request, $response, ['key' => 'Auth.AuthUser']);
  49. $this->user = ['id' => 1];
  50. }
  51. /**
  52. * Test write
  53. */
  54. public function testWrite(): void
  55. {
  56. $this->session->expects($this->once())
  57. ->method('write')
  58. ->with('Auth.AuthUser', $this->user)
  59. ->will($this->returnValue(true));
  60. $this->storage->write($this->user);
  61. }
  62. /**
  63. * Test read
  64. */
  65. public function testRead(): void
  66. {
  67. $this->session->expects($this->once())
  68. ->method('read')
  69. ->with('Auth.AuthUser')
  70. ->will($this->returnValue($this->user));
  71. $result = $this->storage->read();
  72. $this->assertSame($this->user, $result);
  73. }
  74. /**
  75. * Test read from local var
  76. */
  77. public function testGetFromLocalVar(): void
  78. {
  79. $this->storage->write($this->user);
  80. $this->session->expects($this->never())
  81. ->method('read');
  82. $result = $this->storage->read();
  83. $this->assertSame($this->user, $result);
  84. }
  85. /**
  86. * Test delete
  87. */
  88. public function testDelete(): void
  89. {
  90. $this->session->expects($this->once())
  91. ->method('delete')
  92. ->with('Auth.AuthUser');
  93. $this->storage->delete();
  94. }
  95. /**
  96. * Test redirectUrl()
  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->assertSame($url, $result);
  111. $this->session->expects($this->once())
  112. ->method('delete')
  113. ->with('Auth.redirectUrl');
  114. $this->storage->redirectUrl(false);
  115. }
  116. }