No Description

SysVCacheItemPoolTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * Copyright 2018 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\SysVCacheItemPool;
  19. use PHPUnit\Framework\TestCase;
  20. class SysVCacheItemPoolTest extends TestCase
  21. {
  22. private $pool;
  23. public function setUp()
  24. {
  25. if (! extension_loaded('sysvshm')) {
  26. $this->markTestSkipped(
  27. 'sysvshm extension is required for running the test'
  28. );
  29. }
  30. $this->pool = new SysVCacheItemPool(['variableKey' => 99]);
  31. $this->pool->clear();
  32. }
  33. public function saveItem($key, $value)
  34. {
  35. $item = $this->pool->getItem($key);
  36. $item->set($value);
  37. $this->assertTrue($this->pool->save($item));
  38. return $item;
  39. }
  40. public function testGetsFreshItem()
  41. {
  42. $item = $this->pool->getItem('item');
  43. $this->assertInstanceOf('Google\Auth\Cache\Item', $item);
  44. $this->assertNull($item->get());
  45. $this->assertFalse($item->isHit());
  46. }
  47. public function testCacheAmongProcesses()
  48. {
  49. $expectedValue = 'val-' . rand();
  50. exec(sprintf('php %s/sysv_cache_creator.php %s', __DIR__, $expectedValue));
  51. $this->assertEquals(
  52. $expectedValue,
  53. $this->pool->getItem('separate-process-item')->get()
  54. );
  55. }
  56. public function testGetsExistingItem()
  57. {
  58. $key = 'item';
  59. $value = 'value';
  60. $this->saveItem($key, $value);
  61. $item = $this->pool->getItem($key);
  62. $this->assertInstanceOf('Google\Auth\Cache\Item', $item);
  63. $this->assertEquals($value, $item->get());
  64. $this->assertTrue($item->isHit());
  65. }
  66. public function testGetsMultipleItems()
  67. {
  68. $keys = ['item1', 'item2'];
  69. $items = $this->pool->getItems($keys);
  70. $this->assertEquals($keys, array_keys($items));
  71. $this->assertContainsOnlyInstancesOf('Google\Auth\Cache\Item', $items);
  72. }
  73. public function testHasItem()
  74. {
  75. $existsKey = 'does-exist';
  76. $this->saveItem($existsKey, 'value');
  77. $this->assertTrue($this->pool->hasItem($existsKey));
  78. $this->assertFalse($this->pool->hasItem('does-not-exist'));
  79. }
  80. public function testClear()
  81. {
  82. $key = 'item';
  83. $this->saveItem($key, 'value');
  84. $this->assertTrue($this->pool->hasItem($key));
  85. $this->assertTrue($this->pool->clear());
  86. $this->assertFalse($this->pool->hasItem($key));
  87. }
  88. public function testDeletesItem()
  89. {
  90. $key = 'item';
  91. $this->saveItem($key, 'value');
  92. $this->assertTrue($this->pool->deleteItem($key));
  93. $this->assertFalse($this->pool->hasItem($key));
  94. }
  95. public function testDeletesItems()
  96. {
  97. $keys = ['item1', 'item2'];
  98. foreach ($keys as $key) {
  99. $this->saveItem($key, 'value');
  100. }
  101. $this->assertTrue($this->pool->deleteItems($keys));
  102. $this->assertFalse($this->pool->hasItem($keys[0]));
  103. $this->assertFalse($this->pool->hasItem($keys[1]));
  104. }
  105. public function testSavesItem()
  106. {
  107. $key = 'item';
  108. $this->saveItem($key, 'value');
  109. $this->assertTrue($this->pool->hasItem($key));
  110. }
  111. public function testSavesDeferredItem()
  112. {
  113. $item = $this->pool->getItem('item');
  114. $this->assertTrue($this->pool->saveDeferred($item));
  115. }
  116. public function testCommitsDeferredItems()
  117. {
  118. $keys = ['item1', 'item2'];
  119. foreach ($keys as $key) {
  120. $item = $this->pool->getItem($key);
  121. $item->set('value');
  122. $this->pool->saveDeferred($item);
  123. }
  124. $this->assertTrue($this->pool->commit());
  125. $this->assertTrue($this->pool->hasItem($keys[0]));
  126. $this->assertTrue($this->pool->hasItem($keys[1]));
  127. $this->assertEquals(
  128. $item->get(),
  129. $this->pool->getItem($keys[1])->get()
  130. );
  131. }
  132. }