Source of file CacheIncludeTest.php
Size: 4,519 Bytes - Last Modified: 2021-12-24T06:50:57+00:00
/var/www/docs.ssmods.com/process/src/tests/CacheIncludeTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | <?php namespace Heyday\CacheInclude; use Heyday\CacheInclude\Configs\ArrayConfig; use Heyday\CacheInclude\KeyCreators\KeyCreatorInterface; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\SapphireTest; use Symfony\Component\Cache\Simple\ArrayCache; class CacheIncludeTest extends SapphireTest { /** * @var \Heyday\CacheInclude\CacheInclude */ protected $cacheinclude; /** * @var \Heyday\CacheInclude\KeyCreators\KeyCreatorInterface */ protected $keyCreatorMock; protected function setUp() { Injector::inst()->registerService(new ArrayCache(), CacheInterface::class . '.CacheInclude'); $this->keyCreatorMock = $this->getMockBuilder(KeyCreatorInterface::class) ->getMock(); $this->cacheinclude = new CacheInclude( new ArrayConfig(array( 'test' => array( 'expires' => '+1 week' ) )) ); parent::setUp(); } public function testEnabled() { $this->assertTrue($this->cacheinclude->getEnabled()); $this->cacheinclude->setEnabled(false); $this->assertFalse($this->cacheinclude->getEnabled()); } public function testSetDefaultConfig() { $config = [ 'context' => 'no', 'member' => true, 'expires' => false ]; $this->cacheinclude->setDefaultConfig($config); $this->assertEquals($config, $this->cacheinclude->getDefaultConfig()); } public function testCombinedConfig() { $this->assertEquals( [ 'context' => 'no', 'member' => false, 'expires' => '+1 week' ], $this->cacheinclude->getCombinedConfig('test') ); } /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The argument $processor must be an instance of ProcessorInterface or a callable */ public function testProcessException() { $this->cacheinclude->process('test', [], $this->keyCreatorMock); } public function testProcessDisabled() { $this->cacheinclude->setEnabled(false); $this->assertEquals( 'hello', $this->cacheinclude->process( 'test', function () { return 'hello'; }, $this->keyCreatorMock ) ); } public function testProcess() { $this->keyCreatorMock ->expects($this->any()) ->method('getKey') ->will($this->returnValue(['testkey'])); $i = 0; $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(1, $i); $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(1, $i); $this->cacheinclude->setForceExpire(true); $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(2, $i); $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(3, $i); $this->cacheinclude->setForceExpire(false); $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(4, $i); $i = $this->cacheinclude->process( 'test', function ($name) use ($i) { $i++; return $i; }, $this->keyCreatorMock ); $this->assertEquals(4, $i); } public function testForceExpire() { $this->cacheinclude->setForceExpire(true); $this->assertTrue($this->cacheinclude->getForceExpire()); } } |