TestCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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\Cache;
  8. abstract class TestCache extends \Test\TestCase {
  9. /**
  10. * @var \OCP\ICache cache;
  11. */
  12. protected $instance;
  13. protected function tearDown(): void {
  14. if ($this->instance) {
  15. $this->instance->clear();
  16. }
  17. parent::tearDown();
  18. }
  19. public function testSimple(): void {
  20. $this->assertNull($this->instance->get('value1'));
  21. $this->assertFalse($this->instance->hasKey('value1'));
  22. $value = 'foobar';
  23. $this->instance->set('value1', $value);
  24. $this->assertTrue($this->instance->hasKey('value1'));
  25. $received = $this->instance->get('value1');
  26. $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
  27. $value = 'ipsum lorum';
  28. $this->instance->set('value1', $value);
  29. $received = $this->instance->get('value1');
  30. $this->assertEquals($value, $received, 'Value not overwritten by second set');
  31. $value2 = 'foobar';
  32. $this->instance->set('value2', $value2);
  33. $received2 = $this->instance->get('value2');
  34. $this->assertTrue($this->instance->hasKey('value1'));
  35. $this->assertTrue($this->instance->hasKey('value2'));
  36. $this->assertEquals($value, $received, 'Value changed while setting other variable');
  37. $this->assertEquals($value2, $received2, 'Second value not equal to original');
  38. $this->assertFalse($this->instance->hasKey('not_set'));
  39. $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
  40. $this->assertTrue($this->instance->remove('value1'));
  41. $this->assertFalse($this->instance->hasKey('value1'));
  42. }
  43. public function testClear(): void {
  44. $value = 'ipsum lorum';
  45. $this->instance->set('1_value1', $value . '1');
  46. $this->instance->set('1_value2', $value . '2');
  47. $this->instance->set('2_value1', $value . '3');
  48. $this->instance->set('3_value1', $value . '4');
  49. $this->assertEquals([
  50. '1_value1' => 'ipsum lorum1',
  51. '1_value2' => 'ipsum lorum2',
  52. '2_value1' => 'ipsum lorum3',
  53. '3_value1' => 'ipsum lorum4',
  54. ], [
  55. '1_value1' => $this->instance->get('1_value1'),
  56. '1_value2' => $this->instance->get('1_value2'),
  57. '2_value1' => $this->instance->get('2_value1'),
  58. '3_value1' => $this->instance->get('3_value1'),
  59. ]);
  60. $this->assertTrue($this->instance->clear('1_'));
  61. $this->assertEquals([
  62. '1_value1' => null,
  63. '1_value2' => null,
  64. '2_value1' => 'ipsum lorum3',
  65. '3_value1' => 'ipsum lorum4',
  66. ], [
  67. '1_value1' => $this->instance->get('1_value1'),
  68. '1_value2' => $this->instance->get('1_value2'),
  69. '2_value1' => $this->instance->get('2_value1'),
  70. '3_value1' => $this->instance->get('3_value1'),
  71. ]);
  72. $this->assertTrue($this->instance->clear());
  73. $this->assertEquals([
  74. '1_value1' => null,
  75. '1_value2' => null,
  76. '2_value1' => null,
  77. '3_value1' => null,
  78. ], [
  79. '1_value1' => $this->instance->get('1_value1'),
  80. '1_value2' => $this->instance->get('1_value2'),
  81. '2_value1' => $this->instance->get('2_value1'),
  82. '3_value1' => $this->instance->get('3_value1'),
  83. ]);
  84. }
  85. }