Aucune description

ItemTest.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\Tests;
  18. use Google\Auth\Cache\Item;
  19. use PHPUnit\Framework\TestCase;
  20. class ItemTest extends TestCase
  21. {
  22. public function getItem($key)
  23. {
  24. return new Item($key);
  25. }
  26. public function testGetsKey()
  27. {
  28. $key = 'item';
  29. $this->assertEquals($key, $this->getItem($key)->getKey());
  30. }
  31. public function testGetsNull()
  32. {
  33. $item = $this->getItem('item');
  34. $this->assertNull($item->get());
  35. $this->assertFalse($item->isHit());
  36. }
  37. public function testGetsValue()
  38. {
  39. $value = 'value';
  40. $item = $this->getItem('item');
  41. $item->set($value);
  42. $this->assertEquals('value', $item->get());
  43. }
  44. /**
  45. * @dataProvider values
  46. */
  47. public function testSetsValue($value)
  48. {
  49. $item = $this->getItem('item');
  50. $item->set($value);
  51. $this->assertEquals($value, $item->get());
  52. }
  53. public function values()
  54. {
  55. return [
  56. [1],
  57. [1.5],
  58. [true],
  59. [null],
  60. [new \DateTime()],
  61. [['test']],
  62. ['value']
  63. ];
  64. }
  65. public function testIsHit()
  66. {
  67. $item = $this->getItem('item');
  68. $this->assertFalse($item->isHit());
  69. $item->set('value');
  70. $this->assertTrue($item->isHit());
  71. }
  72. public function testExpiresAt()
  73. {
  74. $item = $this->getItem('item');
  75. $item->set('value');
  76. $item->expiresAt(new \DateTime('now + 1 hour'));
  77. $this->assertTrue($item->isHit());
  78. $item->expiresAt(null);
  79. $this->assertTrue($item->isHit());
  80. $item->expiresAt(new \DateTime('yesterday'));
  81. $this->assertFalse($item->isHit());
  82. }
  83. public function testExpiresAfter()
  84. {
  85. $item = $this->getItem('item');
  86. $item->set('value');
  87. $item->expiresAfter(30);
  88. $this->assertTrue($item->isHit());
  89. $item->expiresAfter(0);
  90. $this->assertFalse($item->isHit());
  91. $item->expiresAfter(new \DateInterval('PT30S'));
  92. $this->assertTrue($item->isHit());
  93. $item->expiresAfter(null);
  94. $this->assertTrue($item->isHit());
  95. }
  96. }