MetadataBagTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /**
  25. * @var array
  26. */
  27. protected $array = array();
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->bag = new MetadataBag();
  32. $this->array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0);
  33. $this->bag->initialize($this->array);
  34. }
  35. protected function tearDown()
  36. {
  37. $this->array = array();
  38. $this->bag = null;
  39. parent::tearDown();
  40. }
  41. public function testInitialize()
  42. {
  43. $sessionMetadata = array();
  44. $bag1 = new MetadataBag();
  45. $bag1->initialize($sessionMetadata);
  46. $this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
  47. $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
  48. sleep(1);
  49. $bag2 = new MetadataBag();
  50. $bag2->initialize($sessionMetadata);
  51. $this->assertEquals($bag1->getCreated(), $bag2->getCreated());
  52. $this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed());
  53. $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
  54. sleep(1);
  55. $bag3 = new MetadataBag();
  56. $bag3->initialize($sessionMetadata);
  57. $this->assertEquals($bag1->getCreated(), $bag3->getCreated());
  58. $this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed());
  59. $this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed());
  60. }
  61. public function testGetSetName()
  62. {
  63. $this->assertEquals('__metadata', $this->bag->getName());
  64. $this->bag->setName('foo');
  65. $this->assertEquals('foo', $this->bag->getName());
  66. }
  67. public function testGetStorageKey()
  68. {
  69. $this->assertEquals('_sf2_meta', $this->bag->getStorageKey());
  70. }
  71. public function testGetLifetime()
  72. {
  73. $bag = new MetadataBag();
  74. $array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 1000);
  75. $bag->initialize($array);
  76. $this->assertEquals(1000, $bag->getLifetime());
  77. }
  78. public function testGetCreated()
  79. {
  80. $this->assertEquals(1234567, $this->bag->getCreated());
  81. }
  82. public function testGetLastUsed()
  83. {
  84. $this->assertLessThanOrEqual(time(), $this->bag->getLastUsed());
  85. }
  86. public function testClear()
  87. {
  88. $this->bag->clear();
  89. // the clear method has no side effects, we just want to ensure it doesn't trigger any exceptions
  90. $this->addToAssertionCount(1);
  91. }
  92. public function testSkipLastUsedUpdate()
  93. {
  94. $bag = new MetadataBag('', 30);
  95. $timeStamp = time();
  96. $created = $timeStamp - 15;
  97. $sessionMetadata = array(
  98. MetadataBag::CREATED => $created,
  99. MetadataBag::UPDATED => $created,
  100. MetadataBag::LIFETIME => 1000,
  101. );
  102. $bag->initialize($sessionMetadata);
  103. $this->assertEquals($created, $sessionMetadata[MetadataBag::UPDATED]);
  104. }
  105. public function testDoesNotSkipLastUsedUpdate()
  106. {
  107. $bag = new MetadataBag('', 30);
  108. $timeStamp = time();
  109. $created = $timeStamp - 45;
  110. $sessionMetadata = array(
  111. MetadataBag::CREATED => $created,
  112. MetadataBag::UPDATED => $created,
  113. MetadataBag::LIFETIME => 1000,
  114. );
  115. $bag->initialize($sessionMetadata);
  116. $this->assertEquals($timeStamp, $sessionMetadata[MetadataBag::UPDATED]);
  117. }
  118. }