TestCache.php 3.0 KB

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