CasTraitTest.php 1.6 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-only
  6. */
  7. namespace Test\Memcache;
  8. use Test\TestCase;
  9. /**
  10. * @group Memcache
  11. */
  12. class CasTraitTest extends TestCase {
  13. /**
  14. * @return \OC\Memcache\CasTrait
  15. */
  16. private function getCache() {
  17. $sourceCache = new \OC\Memcache\ArrayCache();
  18. $mock = $this->getMockForTrait('\OC\Memcache\CasTrait');
  19. $mock->expects($this->any())
  20. ->method('set')
  21. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  22. return $sourceCache->set($key, $value, $ttl);
  23. });
  24. $mock->expects($this->any())
  25. ->method('get')
  26. ->willReturnCallback(function ($key) use ($sourceCache) {
  27. return $sourceCache->get($key);
  28. });
  29. $mock->expects($this->any())
  30. ->method('add')
  31. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  32. return $sourceCache->add($key, $value, $ttl);
  33. });
  34. $mock->expects($this->any())
  35. ->method('remove')
  36. ->willReturnCallback(function ($key) use ($sourceCache) {
  37. return $sourceCache->remove($key);
  38. });
  39. return $mock;
  40. }
  41. public function testCasNotChanged(): void {
  42. $cache = $this->getCache();
  43. $cache->set('foo', 'bar');
  44. $this->assertTrue($cache->cas('foo', 'bar', 'asd'));
  45. $this->assertEquals('asd', $cache->get('foo'));
  46. }
  47. public function testCasChanged(): void {
  48. $cache = $this->getCache();
  49. $cache->set('foo', 'bar1');
  50. $this->assertFalse($cache->cas('foo', 'bar', 'asd'));
  51. $this->assertEquals('bar1', $cache->get('foo'));
  52. }
  53. }