MetadataBagTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  13. /**
  14. * Test class for MetadataBag.
  15. *
  16. * @group time-sensitive
  17. */
  18. class MetadataBagTest extends TestCase
  19. {
  20. /**
  21. * @var MetadataBag
  22. */
  23. protected $bag;
  24. protected $array = [];
  25. protected function setUp()
  26. {
  27. parent::setUp();
  28. $this->bag = new MetadataBag();
  29. $this->array = [MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0];
  30. $this->bag->initialize($this->array);
  31. }
  32. protected function tearDown()
  33. {
  34. $this->array = [];
  35. $this->bag = null;
  36. parent::tearDown();
  37. }
  38. public function testInitialize()
  39. {
  40. $sessionMetadata = [];
  41. $bag1 = new MetadataBag();
  42. $bag1->initialize($sessionMetadata);
  43. $this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
  44. $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
  45. sleep(1);
  46. $bag2 = new MetadataBag();
  47. $bag2->initialize($sessionMetadata);
  48. $this->assertEquals($bag1->getCreated(), $bag2->getCreated());
  49. $this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed());
  50. $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
  51. sleep(1);
  52. $bag3 = new MetadataBag();
  53. $bag3->initialize($sessionMetadata);
  54. $this->assertEquals($bag1->getCreated(), $bag3->getCreated());
  55. $this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed());
  56. $this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed());
  57. }
  58. public function testGetSetName()
  59. {
  60. $this->assertEquals('__metadata', $this->bag->getName());
  61. $this->bag->setName('foo');
  62. $this->assertEquals('foo', $this->bag->getName());
  63. }
  64. public function testGetStorageKey()
  65. {
  66. $this->assertEquals('_sf2_meta', $this->bag->getStorageKey());
  67. }
  68. public function testGetLifetime()
  69. {
  70. $bag = new MetadataBag();
  71. $array = [MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 1000];
  72. $bag->initialize($array);
  73. $this->assertEquals(1000, $bag->getLifetime());
  74. }
  75. public function testGetCreated()
  76. {
  77. $this->assertEquals(1234567, $this->bag->getCreated());
  78. }
  79. public function testGetLastUsed()
  80. {
  81. $this->assertLessThanOrEqual(time(), $this->bag->getLastUsed());
  82. }
  83. public function testClear()
  84. {
  85. $this->bag->clear();
  86. // the clear method has no side effects, we just want to ensure it doesn't trigger any exceptions
  87. $this->addToAssertionCount(1);
  88. }
  89. public function testSkipLastUsedUpdate()
  90. {
  91. $bag = new MetadataBag('', 30);
  92. $timeStamp = time();
  93. $created = $timeStamp - 15;
  94. $sessionMetadata = [
  95. MetadataBag::CREATED => $created,
  96. MetadataBag::UPDATED => $created,
  97. MetadataBag::LIFETIME => 1000,
  98. ];
  99. $bag->initialize($sessionMetadata);
  100. $this->assertEquals($created, $sessionMetadata[MetadataBag::UPDATED]);
  101. }
  102. public function testDoesNotSkipLastUsedUpdate()
  103. {
  104. $bag = new MetadataBag('', 30);
  105. $timeStamp = time();
  106. $created = $timeStamp - 45;
  107. $sessionMetadata = [
  108. MetadataBag::CREATED => $created,
  109. MetadataBag::UPDATED => $created,
  110. MetadataBag::LIFETIME => 1000,
  111. ];
  112. $bag->initialize($sessionMetadata);
  113. $this->assertEquals($timeStamp, $sessionMetadata[MetadataBag::UPDATED]);
  114. }
  115. }