|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * To change this license header, choose License Headers in Project Properties. |
| 5 | + * To change this template file, choose Tools | Templates |
| 6 | + * and open the template in the editor. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Cache\Prefixed\Tests; |
| 10 | + |
| 11 | +use Psr\SimpleCache\CacheInterface; |
| 12 | +use Cache\Prefixed\PrefixedSimpleCache; |
| 13 | + |
| 14 | +/** |
| 15 | + * Description of PrefixedSimpleCacheTest |
| 16 | + * |
| 17 | + * @author ndobromirov |
| 18 | + */ |
| 19 | +class PrefixedSimpleCacheTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param string $method Method name to mock. |
| 23 | + * @param array $arguments List of expected arguments. |
| 24 | + * @param type $result |
| 25 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + private function getCacheStub($method, $arguments, $result) |
| 28 | + { |
| 29 | + $stub = $this->getMockBuilder(CacheInterface::class) |
| 30 | + ->setMethods(['get', 'set', 'delete', 'clear', 'getMultiple', 'setMultiple', 'deleteMultiple', 'has']) |
| 31 | + ->getMock() |
| 32 | + ->expects($this->once()) |
| 33 | + ->method($method) |
| 34 | + ->willReturn($result); |
| 35 | + |
| 36 | + return call_user_func_array([$stub, 'with'], $arguments); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public function testGet() |
| 41 | + { |
| 42 | + $prefix = 'ns'; |
| 43 | + $key = 'key'; |
| 44 | + $returnValue = true; |
| 45 | + |
| 46 | + $stub = $this->getCacheStub('get', [$prefix . $key], $returnValue); |
| 47 | + $pool = (new PrefixedSimpleCache($stub, $prefix)); |
| 48 | + |
| 49 | + $this->assertEquals($returnValue, $pool->get($key)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testSet() |
| 53 | + { |
| 54 | + $prefix = 'ns'; |
| 55 | + $key = 'key'; |
| 56 | + $returnValue = true; |
| 57 | + $value = 'value'; |
| 58 | + |
| 59 | + $stub = $this->getCacheStub('set', [$prefix . $key, $value], $returnValue); |
| 60 | + $pool = (new PrefixedSimpleCache($stub, $prefix)); |
| 61 | + |
| 62 | + $this->assertEquals($returnValue, $pool->set($key, $value)); |
| 63 | + } |
| 64 | + |
| 65 | + public function testDelete() |
| 66 | + { |
| 67 | + $prefix = 'ns'; |
| 68 | + $key = 'key'; |
| 69 | + $returnValue = true; |
| 70 | + |
| 71 | + $stub = $this->getCacheStub('delete', [[$prefix . $key]], $returnValue); |
| 72 | + $pool = (new PrefixedSimpleCache($stub, $prefix)); |
| 73 | + |
| 74 | + $this->assertEquals($returnValue, $pool->delete($key)); |
| 75 | + } |
| 76 | + |
| 77 | + public function testClear() |
| 78 | + { |
| 79 | + $prefix = 'ns'; |
| 80 | + $returnValue = true; |
| 81 | + |
| 82 | + $stub = $this->getCacheStub('clear', [], $returnValue); |
| 83 | + $pool = (new PrefixedSimpleCache($stub, $prefix)); |
| 84 | + |
| 85 | + $this->assertEquals($returnValue, $pool->clear()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testGetMultiple() |
| 89 | + { |
| 90 | + $prefix = 'ns'; |
| 91 | + list ($key1, $value1) = ['key1', 1]; |
| 92 | + list ($key2, $value2) = ['key2', 2]; |
| 93 | + |
| 94 | + $stub = $this->getCacheStub('getMultiple', [[$prefix . $key1, $prefix . $key2]], [ |
| 95 | + $prefix . $key1 => $value1, |
| 96 | + $prefix . $key2 => $value2, |
| 97 | + ]); |
| 98 | + $pool = new PrefixedSimpleCache($stub, $prefix); |
| 99 | + |
| 100 | + $this->assertEquals([$key1 => $value1, $key2 => $value2], $pool->getMultiple([$key1, $key2])); |
| 101 | + } |
| 102 | + |
| 103 | + public function testSetMultiple() |
| 104 | + { |
| 105 | + $prefix = 'ns'; |
| 106 | + list ($key1, $value1) = ['key1', 1]; |
| 107 | + list ($key2, $value2) = ['key2', 2]; |
| 108 | + $result = true; |
| 109 | + |
| 110 | + $stub = $this->getCacheStub('setMultiple', [[$prefix . $key1 => $value1, $prefix . $key2 => $value2]], $result); |
| 111 | + $pool = new PrefixedSimpleCache($stub, $prefix); |
| 112 | + |
| 113 | + $this->assertEquals($result, $pool->setMultiple([$key1 => $value1, $key2 => $value2])); |
| 114 | + } |
| 115 | + |
| 116 | + public function testDeleteMultiple() |
| 117 | + { |
| 118 | + $prefix = 'ns'; |
| 119 | + list ($key1, $key2) = ['key1', 'key2']; |
| 120 | + $result = true; |
| 121 | + |
| 122 | + $stub = $this->getCacheStub('deleteMultiple', [[$prefix . $key1, $prefix . $key2]], $result); |
| 123 | + $pool = new PrefixedSimpleCache($stub, $prefix); |
| 124 | + |
| 125 | + $this->assertEquals($result, $pool->deleteMultiple([$key1, $key2])); |
| 126 | + } |
| 127 | + |
| 128 | + public function testHas() |
| 129 | + { |
| 130 | + $prefix = 'ns'; |
| 131 | + $key = 'key'; |
| 132 | + $result = true; |
| 133 | + |
| 134 | + $stub = $this->getCacheStub('has', [[$prefix . $key]], $result); |
| 135 | + $pool = new PrefixedSimpleCache($stub, $prefix); |
| 136 | + |
| 137 | + $this->assertEquals($result, $pool->has($key)); |
| 138 | + } |
| 139 | +} |
0 commit comments