MetadataBagTest.php 3.8 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 Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  12. /**
  13. * Test class for MetadataBag.
  14. *
  15. * @group time-sensitive
  16. */
  17. class MetadataBagTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var MetadataBag
  21. */
  22. protected $bag;
  23. /**
  24. * @var array
  25. */
  26. protected $array = array();
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->bag = new MetadataBag();
  31. $this->array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 0);
  32. $this->bag->initialize($this->array);
  33. }
  34. protected function tearDown()
  35. {
  36. $this->array = array();
  37. $this->bag = null;
  38. parent::tearDown();
  39. }
  40. public function testInitialize()
  41. {
  42. $sessionMetadata = array();
  43. $bag1 = new MetadataBag();
  44. $bag1->initialize($sessionMetadata);
  45. $this->assertGreaterThanOrEqual(time(), $bag1->getCreated());
  46. $this->assertEquals($bag1->getCreated(), $bag1->getLastUsed());
  47. sleep(1);
  48. $bag2 = new MetadataBag();
  49. $bag2->initialize($sessionMetadata);
  50. $this->assertEquals($bag1->getCreated(), $bag2->getCreated());
  51. $this->assertEquals($bag1->getLastUsed(), $bag2->getLastUsed());
  52. $this->assertEquals($bag2->getCreated(), $bag2->getLastUsed());
  53. sleep(1);
  54. $bag3 = new MetadataBag();
  55. $bag3->initialize($sessionMetadata);
  56. $this->assertEquals($bag1->getCreated(), $bag3->getCreated());
  57. $this->assertGreaterThan($bag2->getLastUsed(), $bag3->getLastUsed());
  58. $this->assertNotEquals($bag3->getCreated(), $bag3->getLastUsed());
  59. }
  60. public function testGetSetName()
  61. {
  62. $this->assertEquals('__metadata', $this->bag->getName());
  63. $this->bag->setName('foo');
  64. $this->assertEquals('foo', $this->bag->getName());
  65. }
  66. public function testGetStorageKey()
  67. {
  68. $this->assertEquals('_sf2_meta', $this->bag->getStorageKey());
  69. }
  70. public function testGetLifetime()
  71. {
  72. $bag = new MetadataBag();
  73. $array = array(MetadataBag::CREATED => 1234567, MetadataBag::UPDATED => 12345678, MetadataBag::LIFETIME => 1000);
  74. $bag->initialize($array);
  75. $this->assertEquals(1000, $bag->getLifetime());
  76. }
  77. public function testGetCreated()
  78. {
  79. $this->assertEquals(1234567, $this->bag->getCreated());
  80. }
  81. public function testGetLastUsed()
  82. {
  83. $this->assertLessThanOrEqual(time(), $this->bag->getLastUsed());
  84. }
  85. public function testClear()
  86. {
  87. $this->bag->clear();
  88. }
  89. public function testSkipLastUsedUpdate()
  90. {
  91. $bag = new MetadataBag('', 30);
  92. $timeStamp = time();
  93. $created = $timeStamp - 15;
  94. $sessionMetadata = array(
  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 = array(
  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. }