Cache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Memcache;
  9. abstract class Cache extends \Test\Cache\TestCache {
  10. /**
  11. * @var \OCP\IMemcache cache;
  12. */
  13. protected $instance;
  14. public function testExistsAfterSet() {
  15. $this->assertFalse($this->instance->hasKey('foo'));
  16. $this->instance->set('foo', 'bar');
  17. $this->assertTrue($this->instance->hasKey('foo'));
  18. }
  19. public function testGetAfterSet() {
  20. $this->assertNull($this->instance->get('foo'));
  21. $this->instance->set('foo', 'bar');
  22. $this->assertEquals('bar', $this->instance->get('foo'));
  23. }
  24. public function testGetArrayAfterSet() {
  25. $this->assertNull($this->instance->get('foo'));
  26. $this->instance->set('foo', ['bar']);
  27. $this->assertEquals(['bar'], $this->instance->get('foo'));
  28. }
  29. public function testDoesNotExistAfterRemove() {
  30. $this->instance->set('foo', 'bar');
  31. $this->instance->remove('foo');
  32. $this->assertFalse($this->instance->hasKey('foo'));
  33. }
  34. public function testRemoveNonExisting() {
  35. $this->instance->remove('foo');
  36. $this->assertFalse($this->instance->hasKey('foo'));
  37. }
  38. public function testArrayAccessSet() {
  39. $this->instance['foo'] = 'bar';
  40. $this->assertEquals('bar', $this->instance->get('foo'));
  41. }
  42. public function testArrayAccessGet() {
  43. $this->instance->set('foo', 'bar');
  44. $this->assertEquals('bar', $this->instance['foo']);
  45. }
  46. public function testArrayAccessExists() {
  47. $this->assertFalse(isset($this->instance['foo']));
  48. $this->instance->set('foo', 'bar');
  49. $this->assertTrue(isset($this->instance['foo']));
  50. }
  51. public function testArrayAccessUnset() {
  52. $this->instance->set('foo', 'bar');
  53. unset($this->instance['foo']);
  54. $this->assertFalse($this->instance->hasKey('foo'));
  55. }
  56. public function testAdd() {
  57. $this->assertTrue($this->instance->add('foo', 'bar'));
  58. $this->assertEquals('bar', $this->instance->get('foo'));
  59. $this->assertFalse($this->instance->add('foo', 'asd'));
  60. $this->assertEquals('bar', $this->instance->get('foo'));
  61. }
  62. public function testInc() {
  63. $this->assertEquals(1, $this->instance->inc('foo'));
  64. $this->assertEquals(1, $this->instance->get('foo'));
  65. $this->assertEquals(2, $this->instance->inc('foo'));
  66. $this->assertEquals(2, $this->instance->get('foo'));
  67. $this->assertEquals(12, $this->instance->inc('foo', 10));
  68. $this->assertEquals(12, $this->instance->get('foo'));
  69. $this->instance->set('foo', 'bar');
  70. $this->assertFalse($this->instance->inc('foo'));
  71. $this->assertEquals('bar', $this->instance->get('foo'));
  72. }
  73. public function testDec() {
  74. $this->assertFalse($this->instance->dec('foo'));
  75. $this->instance->set('foo', 20);
  76. $this->assertEquals(19, $this->instance->dec('foo'));
  77. $this->assertEquals(19, $this->instance->get('foo'));
  78. $this->assertEquals(9, $this->instance->dec('foo', 10));
  79. $this->instance->set('foo', 'bar');
  80. $this->assertFalse($this->instance->dec('foo'));
  81. $this->assertEquals('bar', $this->instance->get('foo'));
  82. }
  83. public function testCasNotChanged() {
  84. $this->instance->set('foo', 'bar');
  85. $this->assertTrue($this->instance->cas('foo', 'bar', 'asd'));
  86. $this->assertEquals('asd', $this->instance->get('foo'));
  87. }
  88. public function testCasChanged() {
  89. $this->instance->set('foo', 'bar1');
  90. $this->assertFalse($this->instance->cas('foo', 'bar', 'asd'));
  91. $this->assertEquals('bar1', $this->instance->get('foo'));
  92. }
  93. public function testCadNotChanged() {
  94. $this->instance->set('foo', 'bar');
  95. $this->assertTrue($this->instance->cad('foo', 'bar'));
  96. $this->assertFalse($this->instance->hasKey('foo'));
  97. }
  98. public function testCadChanged() {
  99. $this->instance->set('foo', 'bar1');
  100. $this->assertFalse($this->instance->cad('foo', 'bar'));
  101. $this->assertTrue($this->instance->hasKey('foo'));
  102. }
  103. protected function tearDown(): void {
  104. if ($this->instance) {
  105. $this->instance->clear();
  106. }
  107. parent::tearDown();
  108. }
  109. }