1
0

CasTraitTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Memcache;
  22. use Test\TestCase;
  23. class CasTraitTest extends TestCase {
  24. /**
  25. * @return \OC\Memcache\CasTrait
  26. */
  27. private function getCache() {
  28. $sourceCache = new \OC\Memcache\ArrayCache();
  29. $mock = $this->getMockForTrait('\OC\Memcache\CasTrait');
  30. $mock->expects($this->any())
  31. ->method('set')
  32. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  33. return $sourceCache->set($key, $value, $ttl);
  34. });
  35. $mock->expects($this->any())
  36. ->method('get')
  37. ->willReturnCallback(function ($key) use ($sourceCache) {
  38. return $sourceCache->get($key);
  39. });
  40. $mock->expects($this->any())
  41. ->method('add')
  42. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  43. return $sourceCache->add($key, $value, $ttl);
  44. });
  45. $mock->expects($this->any())
  46. ->method('remove')
  47. ->willReturnCallback(function ($key) use ($sourceCache) {
  48. return $sourceCache->remove($key);
  49. });
  50. return $mock;
  51. }
  52. public function testCasNotChanged() {
  53. $cache = $this->getCache();
  54. $cache->set('foo', 'bar');
  55. $this->assertTrue($cache->cas('foo', 'bar', 'asd'));
  56. $this->assertEquals('asd', $cache->get('foo'));
  57. }
  58. public function testCasChanged() {
  59. $cache = $this->getCache();
  60. $cache->set('foo', 'bar1');
  61. $this->assertFalse($cache->cas('foo', 'bar', 'asd'));
  62. $this->assertEquals('bar1', $cache->get('foo'));
  63. }
  64. }