Cache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 testCadNotChanged() {
  93. $this->instance->set('foo', 'bar');
  94. $this->assertTrue($this->instance->cad('foo', 'bar'));
  95. $this->assertFalse($this->instance->hasKey('foo'));
  96. }
  97. public function testCadChanged() {
  98. $this->instance->set('foo', 'bar1');
  99. $this->assertFalse($this->instance->cad('foo', 'bar'));
  100. $this->assertTrue($this->instance->hasKey('foo'));
  101. }
  102. protected function tearDown(): void {
  103. if ($this->instance) {
  104. $this->instance->clear();
  105. }
  106. parent::tearDown();
  107. }
  108. }