SessionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Tests the session class
  4. *
  5. * @group ko7
  6. * @group ko7.core
  7. * @group ko7.core.session
  8. *
  9. * @package KO7
  10. * @category Tests
  11. *
  12. * @author Jeremy Bush <contractfrombelow@gmail.com>
  13. * @copyright (c) 2007-2016 Kohana Team
  14. * @copyright (c) since 2016 Koseven Team
  15. * @license https://koseven.dev/LICENSE
  16. */
  17. class KO7_SessionTest extends Unittest_TestCase
  18. {
  19. /**
  20. * Gets a mock of the session class
  21. *
  22. * @return Session
  23. */
  24. // @codingStandardsIgnoreStart
  25. public function getMockSession(array $config = [])
  26. // @codingStandardsIgnoreEnd
  27. {
  28. return $this->getMockForAbstractClass('Session', [$config]);
  29. }
  30. /**
  31. * Check that the constructor will load a session if it's provided
  32. * witha session id
  33. *
  34. * @test
  35. * @covers Session::__construct
  36. * @covers Session::read
  37. */
  38. public function test_constructor_loads_session_with_session_id()
  39. {
  40. $config = [];
  41. $session_id = 'lolums';
  42. // Don't auto-call constructor, we need to setup the mock first
  43. $session = $this->getMockBuilder('Session')
  44. ->disableOriginalConstructor()
  45. ->setMethods(['read'])
  46. ->getMockForAbstractClass();
  47. $session
  48. ->expects($this->once())
  49. ->method('read')
  50. ->with($session_id);
  51. $session->__construct($config, $session_id);
  52. }
  53. /**
  54. * Calling $session->bind() should allow you to bind a variable
  55. * to a session variable
  56. *
  57. * @test
  58. * @covers Session::bind
  59. * @ticket 3164
  60. */
  61. public function test_bind_actually_binds_variable()
  62. {
  63. $session = $this->getMockForAbstractClass('Session');
  64. $var = 'asd';
  65. $session->bind('our_var', $var);
  66. $var = 'foobar';
  67. $this->assertSame('foobar', $session->get('our_var'));
  68. }
  69. /**
  70. * Provides test data for test_get_returns_default_if_var_dnx()
  71. *
  72. * @return array
  73. */
  74. public function provider_get_returns_default_if_var_dnx()
  75. {
  76. return [
  77. ['something_crazy', FALSE],
  78. ['a_true', TRUE],
  79. ['an_int', 158163158],
  80. ];
  81. }
  82. /**
  83. * Make sure that get() is using the default value we provide and
  84. * isn't tampering with it
  85. *
  86. * @test
  87. * @dataProvider provider_get_returns_default_if_var_dnx
  88. * @covers Session::get
  89. */
  90. public function test_get_returns_default_if_var_dnx($var, $default)
  91. {
  92. $session = $this->getMockSession();
  93. $this->assertSame($default, $session->get($var, $default));
  94. }
  95. /**
  96. * By default get() should be using null as the var DNX return value
  97. *
  98. * @test
  99. * @covers Session::get
  100. */
  101. public function test_get_uses_null_as_default_return_value()
  102. {
  103. $session = $this->getMockSession();
  104. $this->assertSame(NULL, $session->get('level_of_cool'));
  105. }
  106. /**
  107. * This test makes sure that session is using array_key_exists
  108. * as isset will return FALSE if the value is NULL
  109. *
  110. * @test
  111. * @covers Session::get
  112. */
  113. public function test_get_returns_value_if_it_equals_null()
  114. {
  115. $session = $this->getMockSession();
  116. $session->set('arkward', NULL);
  117. $this->assertSame(NULL, $session->get('arkward', 'uh oh'));
  118. }
  119. /**
  120. * regenerate() should tell the driver to regenerate its id
  121. *
  122. * @test
  123. * @covers Session::regenerate
  124. */
  125. public function test_regenerate_tells_driver_to_regenerate()
  126. {
  127. $session = $this->getMockSession();
  128. $new_session_id = 'asdnoawdnoainf';
  129. $session->expects($this->once())
  130. ->method('_regenerate')
  131. ->with()
  132. ->will($this->returnValue($new_session_id));
  133. $this->assertSame($new_session_id, $session->regenerate());
  134. }
  135. }