Cache.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Memcache;
  8. abstract class Cache extends \Test\Cache\TestCache {
  9. /**
  10. * @var \OCP\IMemcache cache;
  11. */
  12. protected $instance;
  13. public function testExistsAfterSet() {
  14. $this->assertFalse($this->instance->hasKey('foo'));
  15. $this->instance->set('foo', 'bar');
  16. $this->assertTrue($this->instance->hasKey('foo'));
  17. }
  18. public function testGetAfterSet() {
  19. $this->assertNull($this->instance->get('foo'));
  20. $this->instance->set('foo', 'bar');
  21. $this->assertEquals('bar', $this->instance->get('foo'));
  22. }
  23. public function testGetArrayAfterSet() {
  24. $this->assertNull($this->instance->get('foo'));
  25. $this->instance->set('foo', ['bar']);
  26. $this->assertEquals(['bar'], $this->instance->get('foo'));
  27. }
  28. public function testDoesNotExistAfterRemove() {
  29. $this->instance->set('foo', 'bar');
  30. $this->instance->remove('foo');
  31. $this->assertFalse($this->instance->hasKey('foo'));
  32. }
  33. public function testRemoveNonExisting() {
  34. $this->instance->remove('foo');
  35. $this->assertFalse($this->instance->hasKey('foo'));
  36. }
  37. public function testArrayAccessSet() {
  38. $this->instance['foo'] = 'bar';
  39. $this->assertEquals('bar', $this->instance->get('foo'));
  40. }
  41. public function testArrayAccessGet() {
  42. $this->instance->set('foo', 'bar');
  43. $this->assertEquals('bar', $this->instance['foo']);
  44. }
  45. public function testArrayAccessExists() {
  46. $this->assertFalse(isset($this->instance['foo']));
  47. $this->instance->set('foo', 'bar');
  48. $this->assertTrue(isset($this->instance['foo']));
  49. }
  50. public function testArrayAccessUnset() {
  51. $this->instance->set('foo', 'bar');
  52. unset($this->instance['foo']);
  53. $this->assertFalse($this->instance->hasKey('foo'));
  54. }
  55. public function testAdd() {
  56. $this->assertTrue($this->instance->add('foo', 'bar'));
  57. $this->assertEquals('bar', $this->instance->get('foo'));
  58. $this->assertFalse($this->instance->add('foo', 'asd'));
  59. $this->assertEquals('bar', $this->instance->get('foo'));
  60. }
  61. public function testInc() {
  62. $this->assertEquals(1, $this->instance->inc('foo'));
  63. $this->assertEquals(1, $this->instance->get('foo'));
  64. $this->assertEquals(2, $this->instance->inc('foo'));
  65. $this->assertEquals(2, $this->instance->get('foo'));
  66. $this->assertEquals(12, $this->instance->inc('foo', 10));
  67. $this->assertEquals(12, $this->instance->get('foo'));
  68. $this->instance->set('foo', 'bar');
  69. $this->assertFalse($this->instance->inc('foo'));
  70. $this->assertEquals('bar', $this->instance->get('foo'));
  71. }
  72. public function testDec() {
  73. $this->assertFalse($this->instance->dec('foo'));
  74. $this->instance->set('foo', 20);
  75. $this->assertEquals(19, $this->instance->dec('foo'));
  76. $this->assertEquals(19, $this->instance->get('foo'));
  77. $this->assertEquals(9, $this->instance->dec('foo', 10));
  78. $this->instance->set('foo', 'bar');
  79. $this->assertFalse($this->instance->dec('foo'));
  80. $this->assertEquals('bar', $this->instance->get('foo'));
  81. }
  82. public function testCasNotChanged() {
  83. $this->instance->set('foo', 'bar');
  84. $this->assertTrue($this->instance->cas('foo', 'bar', 'asd'));
  85. $this->assertEquals('asd', $this->instance->get('foo'));
  86. }
  87. public function testCasChanged() {
  88. $this->instance->set('foo', 'bar1');
  89. $this->assertFalse($this->instance->cas('foo', 'bar', 'asd'));
  90. $this->assertEquals('bar1', $this->instance->get('foo'));
  91. }
  92. public function testCasNotSet() {
  93. $this->assertFalse($this->instance->cas('foo', 'bar', 'asd'));
  94. }
  95. public function testCadNotChanged() {
  96. $this->instance->set('foo', 'bar');
  97. $this->assertTrue($this->instance->cad('foo', 'bar'));
  98. $this->assertFalse($this->instance->hasKey('foo'));
  99. }
  100. public function testCadChanged() {
  101. $this->instance->set('foo', 'bar1');
  102. $this->assertFalse($this->instance->cad('foo', 'bar'));
  103. $this->assertTrue($this->instance->hasKey('foo'));
  104. }
  105. public function testCadNotSet() {
  106. $this->assertFalse($this->instance->cad('foo', 'bar'));
  107. }
  108. public function testNcadNotChanged() {
  109. $this->instance->set('foo', 'bar');
  110. $this->assertFalse($this->instance->ncad('foo', 'bar'));
  111. $this->assertTrue($this->instance->hasKey('foo'));
  112. }
  113. public function testNcadChanged() {
  114. $this->instance->set('foo', 'bar1');
  115. $this->assertTrue($this->instance->ncad('foo', 'bar'));
  116. $this->assertFalse($this->instance->hasKey('foo'));
  117. }
  118. public function testNcadNotSet() {
  119. $this->assertFalse($this->instance->ncad('foo', 'bar'));
  120. }
  121. protected function tearDown(): void {
  122. if ($this->instance) {
  123. $this->instance->clear();
  124. }
  125. parent::tearDown();
  126. }
  127. }