SessionStorageTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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;
  16. use Cake\Auth\Storage\SessionStorage;
  17. use Cake\Network\Request;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for SessionStorage
  21. *
  22. */
  23. class SessionStorageTest extends TestCase
  24. {
  25. /**
  26. * setup
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->session = $this->getMock('Cake\Network\Session');
  34. $this->request = new Request(['session' => $this->session]);
  35. $this->storage = new SessionStorage($this->request, ['key' => 'Auth.AuthUser']);
  36. $this->user = ['id' => 1];
  37. }
  38. /**
  39. * Test write
  40. *
  41. * @return void
  42. */
  43. public function testWrite()
  44. {
  45. $this->session->expects($this->once())
  46. ->method('write')
  47. ->with('Auth.AuthUser', $this->user)
  48. ->will($this->returnValue(true));
  49. $this->storage->write($this->user);
  50. }
  51. /**
  52. * Test read
  53. *
  54. * @return void
  55. */
  56. public function testRead()
  57. {
  58. $this->session->expects($this->once())
  59. ->method('read')
  60. ->with('Auth.AuthUser')
  61. ->will($this->returnValue($this->user));
  62. $result = $this->storage->read();
  63. $this->assertSame($this->user, $result);
  64. }
  65. /**
  66. * Test read from local var
  67. *
  68. * @return void
  69. */
  70. public function testGetFromLocalVar()
  71. {
  72. $this->storage->write($this->user);
  73. $this->session->expects($this->never())
  74. ->method('read');
  75. $result = $this->storage->read();
  76. $this->assertSame($this->user, $result);
  77. }
  78. /**
  79. * Test delete
  80. *
  81. * @return void
  82. */
  83. public function testDelete()
  84. {
  85. $this->session->expects($this->once())
  86. ->method('delete')
  87. ->with('Auth.AuthUser');
  88. $this->storage->delete();
  89. }
  90. }