Source of file ProxyCacheAdapterTest.php
Size: 5,206 Bytes - Last Modified: 2021-12-23T10:35:47+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Caching/ProxyCacheAdapterTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | <?php namespace SilverStripe\Versioned\Tests\Caching; use Psr\SimpleCache\CacheInterface; use SilverStripe\Dev\SapphireTest; class ProxyCacheAdapterTest extends SapphireTest { public function testItGetsFromPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('get') ->with($this->equalTo('test_get__fake')) ->willReturn('get__return'); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->get('test_get'); $this->assertEquals('get__return', $result); } public function testItSetsToPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('set') ->with( $this->equalTo('test_set__fake'), $this->equalTo('somevalue') ) ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->set('test_set', 'somevalue'); $this->assertTrue($result); } public function testItHasPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('has') ->with( $this->equalTo('test_has__fake') ) ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->has('test_has'); $this->assertTrue($result); } public function testItDeletesFromPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('delete') ->with( $this->equalTo('test_delete__fake') ) ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->delete('test_delete'); $this->assertTrue($result); } public function testItClearsPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('clear') ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->clear(); $this->assertTrue($result); } public function testItGetsMultipleFromPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('getMultiple') ->with( $this->equalTo(['getOne__fake', 'getTwo__fake', 'getThree__fake']) ) ->willReturn([ 'getOne__fake' => 'one', // getTwo deliberately omitted for fallback 'getThree__fake' => 'three', ]); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->getMultiple(['getOne', 'getTwo', 'getThree'], 'myDefault'); $this->assertArrayHasKey('getOne', $result); $this->assertEquals('one', $result['getOne']); $this->assertArrayHasKey('getTwo', $result); $this->assertEquals('myDefault', $result['getTwo']); $this->assertArrayHasKey('getThree', $result); $this->assertEquals('three', $result['getThree']); } public function testItSetsMultipleToPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('setMultiple') ->with( $this->equalTo([ 'setOne__fake' => 'one', 'setTwo__fake' => 'two', 'setThree__fake' => 'three', ]) ) ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->setMultiple([ 'setOne' => 'one', 'setTwo' => 'two', 'setThree' => 'three', ]); $this->assertTrue($result); } public function testItDeletesMultipleToPool() { $mock = $this->getMockCacheInterface(); /* @var CacheInterface $mock */ $mock ->expects($this->once()) ->method('deleteMultiple') ->with( $this->equalTo([ 'deleteOne__fake', 'deleteTwo__fake', 'deleteThree__fake', ]) ) ->willReturn(true); $cache = new ProxyCacheAdapterFake($mock); $result = $cache->deleteMultiple(['deleteOne', 'deleteTwo', 'deleteThree']); $this->assertTrue($result); } protected function getMockCacheInterface() { $methods = ['get', 'set', 'has', 'delete', 'getMultiple', 'setMultiple', 'clear', 'deleteMultiple']; $mock = $this->getMockBuilder(CacheInterface::class) ->setMethods($methods) ->getMock(); return $mock; } } |