1
0

CasTraitTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /**
  24. * @group Memcache
  25. */
  26. class CasTraitTest extends TestCase {
  27. /**
  28. * @return \OC\Memcache\CasTrait
  29. */
  30. private function getCache() {
  31. $sourceCache = new \OC\Memcache\ArrayCache();
  32. $mock = $this->getMockForTrait('\OC\Memcache\CasTrait');
  33. $mock->expects($this->any())
  34. ->method('set')
  35. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  36. return $sourceCache->set($key, $value, $ttl);
  37. });
  38. $mock->expects($this->any())
  39. ->method('get')
  40. ->willReturnCallback(function ($key) use ($sourceCache) {
  41. return $sourceCache->get($key);
  42. });
  43. $mock->expects($this->any())
  44. ->method('add')
  45. ->willReturnCallback(function ($key, $value, $ttl) use ($sourceCache) {
  46. return $sourceCache->add($key, $value, $ttl);
  47. });
  48. $mock->expects($this->any())
  49. ->method('remove')
  50. ->willReturnCallback(function ($key) use ($sourceCache) {
  51. return $sourceCache->remove($key);
  52. });
  53. return $mock;
  54. }
  55. public function testCasNotChanged() {
  56. $cache = $this->getCache();
  57. $cache->set('foo', 'bar');
  58. $this->assertTrue($cache->cas('foo', 'bar', 'asd'));
  59. $this->assertEquals('asd', $cache->get('foo'));
  60. }
  61. public function testCasChanged() {
  62. $cache = $this->getCache();
  63. $cache->set('foo', 'bar1');
  64. $this->assertFalse($cache->cas('foo', 'bar', 'asd'));
  65. $this->assertEquals('bar1', $cache->get('foo'));
  66. }
  67. }