SessionPayloadTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use Laravel\Session\Payload;
  3. class SessionPayloadTest extends PHPUnit_Framework_TestCase {
  4. public function test_has_method_indicates_if_item_exists_in_payload()
  5. {
  6. $payload = new Payload($this->getDummyData());
  7. $this->assertTrue($payload->has('name'));
  8. $this->assertTrue($payload->has('age'));
  9. $this->assertTrue($payload->has('gender'));
  10. $this->assertFalse($payload->has('something'));
  11. $this->assertFalse($payload->has('id'));
  12. $this->assertFalse($payload->has('last_activity'));
  13. }
  14. public function test_get_method_returns_item_from_payload()
  15. {
  16. $payload = new Payload($this->getDummyData());
  17. $this->assertEquals($payload->get('name'), 'Taylor');
  18. $this->assertEquals($payload->get('age'), 25);
  19. $this->assertEquals($payload->get('gender'), 'male');
  20. }
  21. public function test_get_method_returns_default_when_item_doesnt_exist()
  22. {
  23. $payload = new Payload($this->getDummyData());
  24. $this->assertNull($payload->get('something'));
  25. $this->assertEquals('Taylor', $payload->get('something', 'Taylor'));
  26. $this->assertEquals('Taylor', $payload->get('something', function() {return 'Taylor';}));
  27. }
  28. public function test_put_method_adds_to_payload()
  29. {
  30. $payload = new Payload($this->getDummyData());
  31. $payload->put('name', 'Weldon');
  32. $payload->put('workmate', 'Joe');
  33. $this->assertEquals($payload->session['data']['name'], 'Weldon');
  34. $this->assertEquals($payload->session['data']['workmate'], 'Joe');
  35. $this->assertInstanceOf('Laravel\\Session\\Payload', $payload->put('something', 'test'));
  36. }
  37. public function test_flash_method_puts_item_in_flash_data()
  38. {
  39. $payload = new Payload(array());
  40. $payload->flash('name', 'Taylor');
  41. $this->assertEquals($payload->session['data'][':new:name'], 'Taylor');
  42. $this->assertInstanceOf('Laravel\\Session\\Payload', $payload->flash('something', 'test'));
  43. }
  44. public function test_reflash_keeps_all_session_data()
  45. {
  46. $payload = new Payload(array('data' => array(':old:name' => 'Taylor', ':old:age' => 25)));
  47. $payload->reflash();
  48. $this->assertTrue(isset($payload->session['data'][':new:name']));
  49. $this->assertTrue(isset($payload->session['data'][':new:age']));
  50. $this->assertFalse(isset($payload->session['data'][':old:name']));
  51. $this->assertFalse(isset($payload->session['data'][':old:age']));
  52. }
  53. public function test_keep_method_keeps_specified_session_data()
  54. {
  55. $payload = new Payload(array('data' => array(':old:name' => 'Taylor', ':old:age' => 25)));
  56. $payload->keep('name');
  57. $this->assertTrue(isset($payload->session['data'][':new:name']));
  58. $this->assertFalse(isset($payload->session['data'][':old:name']));
  59. $payload = new Payload(array('data' => array(':old:name' => 'Taylor', ':old:age' => 25)));
  60. $payload->keep(array('name', 'age'));
  61. $this->assertTrue(isset($payload->session['data'][':new:name']));
  62. $this->assertTrue(isset($payload->session['data'][':new:age']));
  63. $this->assertFalse(isset($payload->session['data'][':old:name']));
  64. $this->assertFalse(isset($payload->session['data'][':old:age']));
  65. }
  66. public function test_flush_method_clears_payload_data()
  67. {
  68. $payload = new Payload(array('data' => array('name' => 'Taylor')));
  69. $payload->flush();
  70. $this->assertEquals(count($payload->session['data']), 0);
  71. }
  72. public function test_regenerate_session_sets_new_session_id()
  73. {
  74. $payload = new Payload(array('id' => 'something'));
  75. $payload->regenerate();
  76. $this->assertTrue($payload->regenerated);
  77. $this->assertEquals(strlen($payload->session['id']), 40);
  78. }
  79. public function test_age_method_sets_last_activity_time()
  80. {
  81. $data = $this->getDummyData();
  82. unset($data['last_activity']);
  83. $payload = new Payload($data);
  84. $payload->age();
  85. $this->assertTrue(isset($payload->session['last_activity']));
  86. }
  87. public function test_age_method_ages_all_flash_data()
  88. {
  89. $payload = new Payload($this->getDummyData());
  90. $payload->age();
  91. $this->assertTrue(isset($payload->session['data'][':old:age']));
  92. $this->assertFalse(isset($payload->session['data'][':old:gender']));
  93. }
  94. public function test_age_method_returns_session_array()
  95. {
  96. $payload = new Payload($this->getDummyData());
  97. $age = $payload->age();
  98. $this->assertEquals($age['id'], 'something');
  99. }
  100. // ---------------------------------------------------------------------
  101. // Support Functions
  102. // ---------------------------------------------------------------------
  103. public function getDummyData()
  104. {
  105. return array('id' => 'something', 'last_activity' => time(), 'data' => array(
  106. 'name' => 'Taylor',
  107. ':new:age' => 25,
  108. ':old:gender' => 'male',
  109. 'state' => 'Oregon',
  110. ));
  111. }
  112. }