CappedMemoryCacheTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Cache;
  8. /**
  9. * Class CappedMemoryCacheTest
  10. *
  11. * @package Test\Cache
  12. */
  13. class CappedMemoryCacheTest extends TestCache {
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->instance = new \OCP\Cache\CappedMemoryCache();
  17. }
  18. public function testSetOverCap(): void {
  19. $instance = new \OCP\Cache\CappedMemoryCache(3);
  20. $instance->set('1', 'a');
  21. $instance->set('2', 'b');
  22. $instance->set('3', 'c');
  23. $instance->set('4', 'd');
  24. $instance->set('5', 'e');
  25. $this->assertFalse($instance->hasKey('1'));
  26. $this->assertFalse($instance->hasKey('2'));
  27. $this->assertTrue($instance->hasKey('3'));
  28. $this->assertTrue($instance->hasKey('4'));
  29. $this->assertTrue($instance->hasKey('5'));
  30. }
  31. public function testClear(): void {
  32. $value = 'ipsum lorum';
  33. $this->instance->set('1_value1', $value);
  34. $this->instance->set('1_value2', $value);
  35. $this->instance->set('2_value1', $value);
  36. $this->instance->set('3_value1', $value);
  37. $this->assertTrue($this->instance->clear());
  38. $this->assertFalse($this->instance->hasKey('1_value1'));
  39. $this->assertFalse($this->instance->hasKey('1_value2'));
  40. $this->assertFalse($this->instance->hasKey('2_value1'));
  41. $this->assertFalse($this->instance->hasKey('3_value1'));
  42. }
  43. public function testIndirectSet(): void {
  44. $this->instance->set('array', []);
  45. $this->instance['array'][] = 'foo';
  46. $this->assertEquals(['foo'], $this->instance->get('array'));
  47. $this->instance['array']['bar'] = 'qwerty';
  48. $this->assertEquals(['foo', 'bar' => 'qwerty'], $this->instance->get('array'));
  49. }
  50. }