Beispiel #1
0
 /**
  * Validates whether we have a cache and whether it has the right capabilities
  *
  * @throws \Exception if no cache has been set or its capabilities meet the requirements
  */
 protected static function validateCache()
 {
     if (is_null(self::$cache)) {
         throw new \RuntimeException('No cache storage has been set');
     }
     $supportedDataTypes = self::$cache->getCapabilities()->getSupportedDatatypes();
     if (!array_key_exists('array', $supportedDataTypes) || !$supportedDataTypes['array']) {
         throw new \UnexpectedValueException('Cache storage does not support array datatype');
     }
     $supportedMetadata = self::$cache->getCapabilities()->getSupportedMetadata();
     if (!in_array('mtime', $supportedMetadata)) {
         throw new \UnexpectedValueException('Cache storage does not support mtime metadata');
     }
 }
Beispiel #2
0
 public function testTagable()
 {
     if (!$this->_storage instanceof TagableInterface) {
         $this->markTestSkipped("Storage doesn't implement TagableInterface");
     }
     $capabilities = $this->_storage->getCapabilities();
     $ttl = $capabilities->getTtlPrecision();
     $this->_options->setTtl($ttl);
     $this->assertSame(array(), $this->_storage->setItems(array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3')));
     $this->assertTrue($this->_storage->setTags('key1', array('tag1a', 'tag1b')));
     $this->assertTrue($this->_storage->setTags('key2', array('tag2a', 'tag2b')));
     $this->assertTrue($this->_storage->setTags('key3', array('tag3a', 'tag3b')));
     $this->assertFalse($this->_storage->setTags('missing', array('tag')));
     // return tags
     $tags = $this->_storage->getTags('key1');
     $this->assertInternalType('array', $tags);
     sort($tags);
     $this->assertSame(array('tag1a', 'tag1b'), $tags);
     // this should remove nothing
     $this->assertTrue($this->_storage->clearByTags(array('tag1a', 'tag2a')));
     $this->assertTrue($this->_storage->hasItem('key1'));
     $this->assertTrue($this->_storage->hasItem('key2'));
     $this->assertTrue($this->_storage->hasItem('key3'));
     // this should remove key1 and key2
     $this->assertTrue($this->_storage->clearByTags(array('tag1a', 'tag2b'), true));
     $this->assertFalse($this->_storage->hasItem('key1'));
     $this->assertFalse($this->_storage->hasItem('key2'));
     $this->assertTrue($this->_storage->hasItem('key3'));
     // this should remove key3
     $this->assertTrue($this->_storage->clearByTags(array('tag3a', 'tag3b'), true));
     $this->assertFalse($this->_storage->hasItem('key1'));
     $this->assertFalse($this->_storage->hasItem('key2'));
     $this->assertFalse($this->_storage->hasItem('key3'));
 }
Beispiel #3
0
 public function testClearExpired()
 {
     if (!$this->_storage instanceof ClearExpiredInterface) {
         $this->markTestSkipped("Storage doesn't implement ClearExpiredInterface");
     }
     $capabilities = $this->_storage->getCapabilities();
     $ttl = $capabilities->getTtlPrecision();
     $this->_options->setTtl($ttl);
     $this->assertTrue($this->_storage->setItem('key1', 'value1'));
     // wait until the first item expired
     $wait = $ttl + $capabilities->getTtlPrecision();
     usleep($wait * 2000000);
     $this->assertTrue($this->_storage->setItem('key2', 'value2'));
     $this->assertTrue($this->_storage->clearExpired());
     if ($capabilities->getUseRequestTime()) {
         $this->assertTrue($this->_storage->hasItem('key1'));
     } else {
         $this->assertFalse($this->_storage->hasItem('key1', array('ttl' => 0)));
     }
     $this->assertTrue($this->_storage->hasItem('key2'));
 }
 public function testSetAndGetItemOfDifferentTypes()
 {
     $capabilities = $this->_storage->getCapabilities();
     $types = array('NULL' => null, 'boolean' => true, 'integer' => 12345, 'double' => 123.45, 'string' => 'string', 'array' => array('one', 'tow' => 'two', 'three' => array('four' => 'four')), 'object' => new \stdClass(), 'resource' => fopen(__FILE__, 'r'));
     $types['object']->one = 'one';
     $types['object']->two = new \stdClass();
     $types['object']->two->three = 'three';
     foreach ($capabilities->getSupportedDatatypes() as $sourceType => $targetType) {
         if ($targetType === false) {
             continue;
         }
         $value = $types[$sourceType];
         $this->assertTrue($this->_storage->setItem('key', $value), "Failed to set type '{$sourceType}'");
         if ($targetType === true) {
             $this->assertSame($value, $this->_storage->getItem('key'));
         } elseif (is_string($targetType)) {
             settype($value, $targetType);
             $this->assertEquals($value, $this->_storage->getItem('key'));
         }
     }
 }
 /**
  * @test
  */
 public function delegatesGetCapabilities()
 {
     $this->storage->getCapabilities()->willReturn(true);
     $return = $this->cache->getCapabilities();
     $this->assertTrue($return);
 }
 public function getCapabilities()
 {
     return $this->storage->getCapabilities();
 }