diff --git a/Adapters/DoctrineAdapter.php b/Adapters/DoctrineAdapter.php index 69d56a2..4f9909b 100644 --- a/Adapters/DoctrineAdapter.php +++ b/Adapters/DoctrineAdapter.php @@ -118,9 +118,9 @@ public function deleteAll() public function getStats() { $stats = array(); - $logger = $this->cacheService->getLogger(); - $stats['hits'] = $logger->getHits(); - $stats['misses'] = $logger->getCalls() - $stats['hits']; + $tracker = $this->cacheService->getTracker(); + $stats['hits'] = $tracker->getHits(); + $stats['misses'] = $tracker->getCalls() - $stats['hits']; return $stats; } diff --git a/Collector/CacheDataCollector.php b/Collector/CacheDataCollector.php index feea007..bd1e0ea 100644 --- a/Collector/CacheDataCollector.php +++ b/Collector/CacheDataCollector.php @@ -36,11 +36,11 @@ class CacheDataCollector extends DataCollector protected $cacheOptions; /** - * The logger classes for all cache services. + * The tracker classes for all cache services. * * @var array */ - protected $loggers = array(); + protected $trackers = array(); public function __construct($default, $caches, $options) { @@ -50,13 +50,13 @@ public function __construct($default, $caches, $options) } /** - * Inject the logger for a cache service. + * Inject the tracker for a cache service. * - * @param $logger + * @param $tracker */ - public function addLogger($logger) + public function addTracker($tracker) { - $this->loggers[] = $logger; + $this->trackers[] = $tracker; } /** @@ -65,23 +65,23 @@ public function addLogger($logger) public function collect(Request $request, Response $response, \Exception $exception = null) { $info = array('calls' => 0, 'hits' => 0); - foreach ($this->loggers as $logger) { - $name = $logger->getName(); - $calls = $logger->getCalls(); - $hits = $logger->getHits(); + foreach ($this->trackers as $tracker) { + $name = $tracker->getName(); + $calls = $tracker->getCalls(); + $hits = $tracker->getHits(); $info['calls'] += $calls; $info['hits'] += $hits; $info['caches'][$name]['options'] = $this->cacheOptions[$name]; - $info['caches'][$name]['queries'] = $logger->getQueries(); + $info['caches'][$name]['queries'] = $tracker->getQueries(); $info['caches'][$name]['calls'] = $calls; $info['caches'][$name]['hits'] = $hits; } - $handlers = Drivers::getDrivers(); - foreach ($handlers as $handler) { - $pieces = explode('\\', $handler); + $drivers = Drivers::getDrivers(); + foreach ($drivers as $driver) { + $pieces = explode('\\', $driver); $name = array_pop($pieces); if (!in_array($name, array('Ephemeral', 'Composite'))) { $info['availableDrivers'][] = $name; @@ -112,7 +112,7 @@ public function getHits() /** * Returns the list of available drivers. */ - public function gethandlers() + public function getDrivers() { return $this->data['availableDrivers']; } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8318acc..7c583b3 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -9,7 +9,7 @@ class Configuration implements ConfigurationInterface { - protected $handlerSettings = array( + protected $driverSettings = array( 'FileSystem' => array( 'dirSplit' => 2, 'path' => '%kernel.cache_dir%/stash', @@ -53,7 +53,7 @@ public function getConfigTreeBuilder() ->then(function ($v) { $cache = array(); foreach ($v as $key => $value) { - if (in_array($key, array('default_cache', 'logging'))) { + if (in_array($key, array('default_cache', 'tracking'))) { continue; } $cache[$key] = $v[$key]; @@ -67,7 +67,7 @@ public function getConfigTreeBuilder() ->end() ->children() ->scalarNode('default_cache')->end() - ->booleanNode('logging')->end() + ->booleanNode('tracking')->end() ->end() ->fixXmlConfig('cache') ->append($this->getCachesNode()) @@ -78,24 +78,24 @@ public function getConfigTreeBuilder() protected function getCachesNode() { - $handlers = array_keys(Drivers::getDrivers()); + $drivers = array_keys(Drivers::getDrivers()); $treeBuilder = new TreeBuilder(); $node = $treeBuilder->root('caches'); $childNode = $node - ->fixXmlConfig('handler') + ->fixXmlConfig('driver') ->requiresAtLeastOneElement() ->useAttributeAsKey('name') ->prototype('array') ->children() - ->arrayNode('handlers') + ->arrayNode('drivers') ->requiresAtLeastOneElement() ->defaultValue(array('FileSystem')) ->prototype('scalar') ->validate() - ->ifNotInArray($handlers) - ->thenInvalid('A handler of that name is not registered.') + ->ifNotInArray($drivers) + ->thenInvalid('A driver of that name is not registered.') ->end() ->end() ->end() @@ -104,9 +104,9 @@ protected function getCachesNode() ->booleanNode('inMemory')->defaultTrue()->end() ; - foreach ($handlers as $handler) { - if ($handler !== 'Composite') { - $this->addHandlerSettings($handler, $childNode); + foreach ($drivers as $driver) { + if ($driver !== 'Composite') { + $this->addDriverSettings($driver, $childNode); } } @@ -116,14 +116,14 @@ protected function getCachesNode() return $node; } - public function addHandlerSettings($handler, $rootNode) + public function addDriverSettings($driver, $rootNode) { - $handlerNode = $rootNode - ->arrayNode($handler) + $driverNode = $rootNode + ->arrayNode($driver) ->fixXmlConfig('server'); - if ($handler == 'Memcache') { - $finalNode = $handlerNode + if ($driver == 'Memcache') { + $finalNode = $driverNode ->info('All options except "servers" are Memcached options. See http://www.php.net/manual/en/memcached.constants.php') ->addDefaultsIfNotSet() ->children() @@ -161,8 +161,8 @@ public function addHandlerSettings($handler, $rootNode) ->end() ->end() ; - } elseif ($handler == 'Redis') { - $finalNode = $handlerNode + } elseif ($driver == 'Redis') { + $finalNode = $driverNode ->info("Accepts server info, password, and database.") ->addDefaultsIfNotSet() ->children() @@ -185,9 +185,9 @@ public function addHandlerSettings($handler, $rootNode) ->end() ; } else { - $defaults = isset($this->handlerSettings[$handler]) ? $this->handlerSettings[$handler] : array(); + $defaults = isset($this->driverSettings[$driver]) ? $this->driverSettings[$driver] : array(); - $node = $handlerNode + $node = $driverNode ->addDefaultsIfNotSet() ->children(); diff --git a/DependencyInjection/TedivmStashExtension.php b/DependencyInjection/TedivmStashExtension.php index 395a95c..d6959f3 100644 --- a/DependencyInjection/TedivmStashExtension.php +++ b/DependencyInjection/TedivmStashExtension.php @@ -14,7 +14,7 @@ /** * Bundle extension to handle configuration of the Stash bundle. Based on the specification provided * in the configuration file, this extension instantiates and dynamically injects the selected caching provider into - * the Stash service, passing it any handler-specific settings from the configuration. + * the Stash service, passing it any driver-specific settings from the configuration. * * @author Josh Hall-Bachner */ @@ -30,10 +30,10 @@ public function load(array $configs, ContainerBuilder $container) $container->setAlias('cache', sprintf('stash.%s_cache', $config['default_cache'])); - $lq = isset($config['logging']) - ? $config['logging'] + $lq = isset($config['tracking']) + ? $config['tracking'] : (in_array($container->getParameter('kernel.environment'), array('dev', 'test'))); - $container->setParameter('stash.logging', $lq); + $container->setParameter('stash.tracker', $lq); $caches = array(); $options = array(); @@ -50,13 +50,18 @@ public function load(array $configs, ContainerBuilder $container) protected function addCacheService($name, $cache, $container) { - $logqueries = $container->getParameter('stash.logging'); + $logqueries = $container->getParameter('stash.tracker'); - $handlers = $cache['handlers']; - unset($cache['handlers']); + if (isset($cache['drivers'])) { + $drivers = $cache['drivers']; + } else { + $drivers = $cache['drivers']; + } + + unset($cache['drivers']); if (isset($cache['inMemory']) && $cache['inMemory']) { - array_unshift($handlers, 'Ephemeral'); + array_unshift($drivers, 'Ephemeral'); } unset($cache['inMemory']); @@ -67,16 +72,16 @@ protected function addCacheService($name, $cache, $container) unset($cache['registerSessionHandler']); $container - ->setDefinition(sprintf('stash.handler.%s_cache', $name), new DefinitionDecorator('stash.handler')) + ->setDefinition(sprintf('stash.driver.%s_cache', $name), new DefinitionDecorator('stash.driver')) ->setArguments(array( - $handlers, + $drivers, $cache )) ->setAbstract(false) ; $container - ->setDefinition(sprintf('stash.logger.%s_cache', $name), new DefinitionDecorator('stash.logger')) + ->setDefinition(sprintf('stash.tracker.%s_cache', $name), new DefinitionDecorator('stash.tracker')) ->setArguments(array( $name )) @@ -88,8 +93,8 @@ protected function addCacheService($name, $cache, $container) ->setDefinition(sprintf('stash.%s_cache', $name), new DefinitionDecorator('stash.cache')) ->setArguments(array( $name, - new Reference(sprintf('stash.handler.%s_cache', $name)), - new Reference(sprintf('stash.logger.%s_cache', $name)) + new Reference(sprintf('stash.driver.%s_cache', $name)), + new Reference(sprintf('stash.tracker.%s_cache', $name)) )) ->setAbstract(false) ; @@ -116,8 +121,8 @@ protected function addCacheService($name, $cache, $container) $container ->getDefinition('data_collector.stash') - ->addMethodCall('addLogger', array( - new Reference(sprintf('stash.logger.%s_cache', $name)) + ->addMethodCall('addTracker', array( + new Reference(sprintf('stash.tracker.%s_cache', $name)) )) ; diff --git a/Factory/HandlerFactory.php b/Factory/DriverFactory.php similarity index 80% rename from Factory/HandlerFactory.php rename to Factory/DriverFactory.php index f04725d..1a7bbf5 100644 --- a/Factory/HandlerFactory.php +++ b/Factory/DriverFactory.php @@ -4,7 +4,7 @@ use Stash\Drivers, Stash\Interfaces\DriverInterface; -class HandlerFactory +class DriverFactory { /** * Given a list of cache types and options, creates a CompositeDrivers wrapping the specified drivers. @@ -13,14 +13,14 @@ class HandlerFactory * @param $options * @return DriverInterface */ - public static function createHandler($types, $options) + public static function createDriver($types, $options) { - $handlers = Drivers::getDrivers(); + $drivers = Drivers::getDrivers(); $h = array(); foreach ($types as $type) { - $class = $handlers[$type]; + $class = $drivers[$type]; if ($type === 'Memcache' && isset($options[$type])) { // Fix servers spec since underlying drivers expect plain arrays, not hashes. $servers = array(); @@ -43,9 +43,9 @@ public static function createHandler($types, $options) return reset($h); } - $class = $handlers['Composite']; - $handler = new $class(array('drivers' => $h)); + $class = $drivers['Composite']; + $driver = new $class(array('drivers' => $h)); - return $handler; + return $driver; } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index ff9acf3..7c03ca0 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -5,9 +5,9 @@ Tedivm\StashBundle\Service\CacheService - Tedivm\StashBundle\Service\CacheLogger - Stash\Handler\HandlerInterface - Tedivm\StashBundle\Factory\HandlerFactory + Tedivm\StashBundle\Service\CacheTracker + Stash\Interfaces\DriverInterface + Tedivm\StashBundle\Factory\DriverFactory Tedivm\StashBundle\Adapters\DoctrineAdapter Tedivm\StashBundle\Adapters\SessionHandlerAdapter Tedivm\StashBundle\Collector\CacheDataCollector @@ -15,12 +15,12 @@ - - + diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst new file mode 100644 index 0000000..e69de29 diff --git a/Resources/views/Profiler/layout.html.twig b/Resources/views/Profiler/layout.html.twig index 8e5d5f9..649ac01 100644 --- a/Resources/views/Profiler/layout.html.twig +++ b/Resources/views/Profiler/layout.html.twig @@ -13,7 +13,7 @@ Default Queue {{ collector.default }}
- Av. Handlers {{ collector.handlers|join(', ') }} + Av. Drivers {{ collector.drivers|join(', ') }}
{% for name, details in collector.caches %}
@@ -21,7 +21,7 @@ {{ name }} {{ details.calls }} / {{ details.hits }}
- Handlers {{ details.options.handlers|join(', ') }} + Drivers {{ details.options.drivers|join(', ') }}
{% endfor %} {% endset %} @@ -45,8 +45,8 @@ Default Cache {{ collector.default }} - Available Handlers - {{ collector.handlers|join(', ') }} + Available Drivers + {{ collector.drivers|join(', ') }} Total Requests @@ -63,8 +63,8 @@

Cache Service: {{ name }}

- - + + @@ -84,10 +84,10 @@
Handlers{{ details.options.handlers|join(', ') }}Drivers{{ details.options.drivers|join(', ') }}
Calls
-

Handler Options

+

Driver Options

{% for name, options in details.options %} - {% if name != 'handlers' and name in details.options.handlers %} + {% if name != 'drivers' and name in details.options.drivers %} diff --git a/Service/CacheItem.php b/Service/CacheItem.php new file mode 100644 index 0000000..8ea3a9d --- /dev/null +++ b/Service/CacheItem.php @@ -0,0 +1,38 @@ + + */ +class CacheItem extends Item +{ + + /** + * @var null|CacheTracker + */ + protected $cacheTracker; + + public function setCacheTracker(CacheTracker $tracker) + { + $this->cacheTracker = $tracker; + } + + public function get($invalidation = 0, $arg = null, $arg2 = null) + { + $result = parent::get($invalidation, $arg, $arg2); + + if (isset($this->cacheTracker)) { + $miss = $this->isMiss(); + $key = $this->getKey(); + $this->cacheTracker->trackRequest($key, !($miss), $result); + } + + return $result; + + } + +} diff --git a/Service/CacheResultObject.php b/Service/CacheResultObject.php deleted file mode 100644 index 6cfecc6..0000000 --- a/Service/CacheResultObject.php +++ /dev/null @@ -1,89 +0,0 @@ - - */ -class CacheResultObject -{ - /** - * @var \Stash\Item - */ - protected $cache; - - /** - * @var null|CacheLogger - */ - protected $logger; - - /** - * Constructs the CacheResultObject, wraps Cache object to perform logging. - * - * @param \Stash\Item $cache - * @param CacheLogger $logger - */ - public function __construct($cache, $logger = null) - { - $this->cache = $cache; - $this->logger = $logger; - } - - /** - * Decorates the Stash cache result object: - * - Adds logging to the 'get' method - * - Replaces the key for the 'getKey' method - * - Passes through all other methods directly - * - * @param $name - * @param $args - * @return mixed|string - */ - public function __call($name, $args) - { - if ($name === 'get') { - return $this->getAndLog($args); - } elseif ($name === 'getKey') { - return $this->getShortenedKey(); - } else { - return call_user_func_array(array($this->cache, $name), $args); - } - } - - /** - * Removes the cache-service namespace from the start of the key for public consumption. - * - * @return string - */ - protected function getShortenedKey() - { - $key = $this->cache->getKey(); - $parts = explode('/', $key); - if ( (strpos($parts[0], '@@_') === 0) && (strpos(strrev($parts[0]), '@@_') === 0) ) { - array_shift($parts); - } - - return join('/', $parts); - } - - /** - * Get the value from the cache object, then logs the query. - * - * @param $args - * @return mixed - */ - protected function getAndLog($args) - { - $result = call_user_func_array(array($this->cache, 'get'), $args); - $miss = $this->cache->isMiss(); - $key = $this->cache->getKey(); - - if (isset($this->logger)) { - $this->logger->logRequest($key, !($miss), $result); - } - - return $result; - } -} diff --git a/Service/CacheService.php b/Service/CacheService.php index f5f715f..6e6596b 100644 --- a/Service/CacheService.php +++ b/Service/CacheService.php @@ -1,11 +1,9 @@ name = $name; - $this->driver = $driver; - $this->key = '@@_' . $name . '_@@'; + $this->cacheTracker = $tracker; + $this->setNamespace($name); - $this->pool = new Pool($this->driver); + if (isset($this->driver)) { + $this->setDriver($driver); + } - $this->logger = $logger; + parent::__construct($driver); } /** * Returns a Stash caching object for the specified key. The key can be either a series of string arguments, * or an array. * - * @param string|array $key, $key, $key... - * @return \Stash\Item Note: Cache item is wrapped inside CacheResultObject which deals with logging + * @param mixed $key,... String Representing the key + * @return CacheItem */ public function getItem() { @@ -67,63 +54,36 @@ public function getItem() if(count($args) == 1 && is_array($args[0])) $args = $args[0]; - array_unshift($args, $this->key); - - $item = $this->pool->getItem($args); - - $stash = new CacheResultObject($item, $this->logger); - - return $stash; - } + /** @var CacheItem $item */ + $item = parent::getItem($args); - /** - * Returns a group of wrapped cache objects as an \Iterator. This duplicates the functionality of the - * Pool class getItemIterator method, but with wrapped, loggable cache items. - * - * @param array $keys - * @return \Iterator - */ - public function getItemIterator($keys) - { - $items = array(); - foreach ($keys as $key) { - $items[] = $this->getItem($key); + if (isset($this->tracker)) { + $item->setCacheTracker($this->cacheTracker); } - return new ArrayIterator($items); + return $item; } /** * Clears the cache for the key, or if none is specified clears the entire cache. The key can be either * a series of string arguments, or an array. * - * @param null|string|array $key, $key, $key... + * @param mixed $key,... String Representing the key */ public function clear() { $args = func_get_args(); if (count($args) === 0) { - return $this->pool->flush(); + return $this->flush(); } else { - $stash = call_user_func_array(array($this, 'getItem'), $args); + $item = $this->getItem($args); - return $stash->clear(); + return $item->clear(); } } /** - * Purges the cache of all stale or obsolete objects, as well as other maintenance tasks specified by the - * back end caching system. This operation has the potential to be very long running. - * - * @return bool - */ - public function purge() - { - return $this->pool->purge(); - } - - /** - * Returns the current list of handlers that the system is able to use. + * Returns the current list of drivers that the system is able to use. * * @return array */ @@ -133,12 +93,12 @@ public function getDrivers() } /** - * Returns the current logger + * Returns the current tracker * - * @return CacheLogger|null + * @return CacheTracker|false */ - public function getLogger() + public function getTracker() { - return $this->logger; + return isset($this->cacheTracker) ? $this->cacheTracker : false; } } diff --git a/Service/CacheLogger.php b/Service/CacheTracker.php similarity index 90% rename from Service/CacheLogger.php rename to Service/CacheTracker.php index 2caec58..f92ab5a 100644 --- a/Service/CacheLogger.php +++ b/Service/CacheTracker.php @@ -7,7 +7,7 @@ * * @author Josh Hall-Bachner */ -class CacheLogger +class CacheTracker { /** * The name of the cache being logged. @@ -66,7 +66,7 @@ public function enableQueryLogging($lq = true) * @param $hit * @param $value */ - public function logRequest($key, $hit, $value) + public function trackRequest($key, $hit, $value) { $this->calls++; if ($hit) { @@ -77,11 +77,6 @@ public function logRequest($key, $hit, $value) return; } - $leader = sprintf('@@_%s_@@/', $this->name); - if (strpos($key, $leader) === 0) { - $key = substr($key, strlen($leader)); - } - $hit = $hit ? 'true' : 'false'; $value = sprintf('(%s) %s', gettype($value), print_r($value, true)); diff --git a/Tests/DependencyInjection/TedivmStashExtensionTest.php b/Tests/DependencyInjection/TedivmStashExtensionTest.php index 10c2b93..e635a1c 100644 --- a/Tests/DependencyInjection/TedivmStashExtensionTest.php +++ b/Tests/DependencyInjection/TedivmStashExtensionTest.php @@ -10,7 +10,7 @@ class TedivmStashExtensionTest extends \PHPUnit_Framework_TestCase /** * @dataProvider configProvider */ - public function testLoadHandlerConfiguration($config) + public function testLoadDriverConfiguration($config) { $container = new ContainerBuilder(); $container->setParameter('kernel.environment', 'test'); @@ -36,10 +36,10 @@ public function testLoadHandlerConfiguration($config) $this->assertEquals($cacheoptions[$item], $value); } - foreach ($cache['handlers'] as $handler) { - $handleroptions = $cache[$handler]; - foreach ($handleroptions as $handleroptname => $handleroptvalue) { - $this->assertEquals($handleroptvalue, $cacheoptions[$handler][$handleroptname]); + foreach ($cache['drivers'] as $driver) { + $driveroptions = $cache[$driver]; + foreach ($driveroptions as $driveroptname => $driveroptvalue) { + $this->assertEquals($driveroptvalue, $cacheoptions[$driver][$driveroptname]); } } @@ -54,7 +54,7 @@ public function configProvider() 'default_cache' => 'first', 'caches' => array( 'first' => array( - 'handlers' => array('FileSystem'), + 'drivers' => array('FileSystem'), 'FileSystem' => array( 'dirSplit' => 2, 'path' => '%kernel.cache_dir%/stash', @@ -71,7 +71,7 @@ public function configProvider() 'default_cache' => 'default', 'caches' => array( 'default' => array( - 'handlers' => array('SQLite'), + 'drivers' => array('SQLite'), 'registerDoctrineAdapter' => true, 'registerSessionHandler' => false, 'inMemory' => true, @@ -82,7 +82,7 @@ public function configProvider() ), ), 'nondefault' => array( - 'handlers' => array('FileSystem', 'SQLite'), + 'drivers' => array('FileSystem', 'SQLite'), 'registerDoctrineAdapter' => true, 'registerSessionHandler' => true, 'inMemory' => true, diff --git a/Tests/Factory/HandlerFactoryTest.php b/Tests/Factory/DriverFactoryTest.php similarity index 69% rename from Tests/Factory/HandlerFactoryTest.php rename to Tests/Factory/DriverFactoryTest.php index 91771a3..7f79f76 100644 --- a/Tests/Factory/HandlerFactoryTest.php +++ b/Tests/Factory/DriverFactoryTest.php @@ -2,12 +2,12 @@ namespace Tedivm\StashBundle\Tests\Factory; -use Tedivm\StashBundle\Factory\HandlerFactory; +use Tedivm\StashBundle\Factory\DriverFactory; use Stash\Drivers; -class HandlerFactoryTest extends \PHPUnit_Framework_TestCase +class DriverFactoryTest extends \PHPUnit_Framework_TestCase { - protected $handlers = array(); + protected $drivers = array(); protected $defaultSettings = array( 'FileSystem' => array( @@ -21,7 +21,7 @@ class HandlerFactoryTest extends \PHPUnit_Framework_TestCase 'dirPermissions' => 0770, 'busyTimeout' => 500, 'nesting' => 0, - 'subhandler' => 'PDO', + 'subdriver' => 'PDO', 'version' => null, ), 'Apc' => array( @@ -32,30 +32,30 @@ class HandlerFactoryTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->handlers = Drivers::getDrivers(); + $this->drivers = Drivers::getDrivers(); } /** - * @dataProvider handlerProvider + * @dataProvider driverProvider */ - public function testManufactureHandlers($types, $options) + public function testManufactureDrivers($types, $options) { - $handler = HandlerFactory::createHandler($types, $options); + $driver = DriverFactory::createDriver($types, $options); if (count($types) > 1) { - $handlerclass = $this->handlers['Composite']; - $h = $this->getObjectAttribute($handler, 'drivers'); - $handlers = array_combine($types, $h); + $driverclass = $this->drivers['Composite']; + $h = $this->getObjectAttribute($driver, 'drivers'); + $drivers = array_combine($types, $h); } else { - $handlerclass = $this->handlers[$types[0]]; - $handlers = array($types[0] => $handler); + $driverclass = $this->drivers[$types[0]]; + $drivers = array($types[0] => $driver); } - $this->assertInstanceOf($handlerclass, $handler); + $this->assertInstanceOf($driverclass, $driver); - foreach ($handlers as $subtype => $subhandler) { - $subhandlerclass = $this->handlers[$subtype]; - $this->assertInstanceOf($subhandlerclass, $subhandler); + foreach ($drivers as $subtype => $subdriver) { + $subdriverclass = $this->drivers[$subtype]; + $this->assertInstanceOf($subdriverclass, $subdriver); } foreach ($types as $type) { @@ -63,13 +63,13 @@ public function testManufactureHandlers($types, $options) $options = array_merge($defaults, $options); /* foreach ($options as $optname => $optvalue) { - $this->assertAttributeEquals($optvalue, $optname, $handlers[$type]); + $this->assertAttributeEquals($optvalue, $optname, $drivers[$type]); } */ } } - public function handlerProvider() + public function driverProvider() { return array( array( diff --git a/Tests/Service/CacheItemTest.php b/Tests/Service/CacheItemTest.php new file mode 100644 index 0000000..e7e62b5 --- /dev/null +++ b/Tests/Service/CacheItemTest.php @@ -0,0 +1,9 @@ +handler = new Ephemeral(array()); - } + protected $itemClass = '\Tedivm\StashBundle\Service\CacheService'; protected function getCacheService($name) { - return new CacheService($name, $this->handler); + return new $this->itemClass($name); } - public function testCache() + public function testCacheService() { $service = $this->getCacheService('first'); + $service->setDriver(new Ephemeral()); $this->runCacheCycle($service, 'one', true); $this->runCacheCycle($service, 'two', true); @@ -30,12 +25,12 @@ public function testCache() $this->runCacheCycle($service, 'one', false); $this->runCacheCycle($service, 'two', false); - $service->clear('test', 'key', 'one'); + $this->assertTrue($service->clear('test', 'key', 'one')); $this->runCacheCycle($service, 'one', true); $this->runCacheCycle($service, 'two', false); - $service->clear(); + $this->assertTrue($service->clear()); $this->runCacheCycle($service, 'one', true); $this->runCacheCycle($service, 'two', true); @@ -43,8 +38,11 @@ public function testCache() public function testTwoServices() { + $driver = new Ephemeral(); $service1 = $this->getCacheService('first'); + $service1->setDriver($driver); $service2 = $this->getCacheService('second'); + $service2->setDriver($driver); $this->runCacheCycle($service1, 'one', true); $this->runCacheCycle($service2, 'two', true); @@ -59,12 +57,13 @@ public function testTwoServices() $this->runCacheCycle($service1, 'two', false); } - public function testGetItemIterator() + public function testServiceGetItemIterator() { $keys = array('test/key/one', 'test/key/two', 'test/key/three', 'test/key/four'); $values = array('uno', 'dos', 'tres', 'quattro'); $service = $this->getCacheService('first'); + $service->setDriver(new Ephemeral()); $iterator = $service->getItemIterator($keys); $setvalues = $values; @@ -88,16 +87,19 @@ public function testGetItemIterator() protected function runCacheCycle($service, $num, $ismiss) { - $cache = $service->getItem('test', 'key', $num); - $data = $cache->get(); - $this->assertEquals($ismiss, $cache->isMiss()); + $key = array('test', 'key', $num); + $testData = 'testkey' . $num; + + $item = $service->getItem($key); + $data = $item->get(); + $this->assertEquals($ismiss, $item->isMiss()); - $this->assertTrue($cache->set('testkey'.$num)); + $this->assertTrue($item->set($testData)); - $cache = $service->getItem('test', 'key', $num); - $data = $cache->get(); - $this->assertFalse($cache->isMiss()); - $this->assertEquals('testkey'.$num, $data); + $item = $service->getItem($key); + $data = $item->get(); + $this->assertFalse($item->isMiss()); + $this->assertEquals($testData, $data); } } diff --git a/Tests/Service/CacheTrackerTest.php b/Tests/Service/CacheTrackerTest.php new file mode 100644 index 0000000..2066852 --- /dev/null +++ b/Tests/Service/CacheTrackerTest.php @@ -0,0 +1,122 @@ +testClass($name); + $this->assertInstanceOf($this->testClass, $tracker); + + return $tracker; + } + + public function testTrackRequest() + { + + } + + public function testGetName() + { + $tracker = $this->testConstruct(); + $this->assertEquals('test', $tracker->getName(), 'Tracker knows it\'s name'); + + $tracker = $this->testConstruct('test2'); + $this->assertEquals('test2', $tracker->getName(), 'Tracker knows it\'s name'); + } + + public function testGetCalls() + { + $tracker = $this->getPopulatedTracker(); + + $this->assertEquals(6, $tracker->getCalls(), 'Tracker counts calls sent to it.'); + + $tracker->trackRequest('Key7', false, 'Value7'); + + $this->assertEquals(7, $tracker->getCalls(), 'Tracker counts calls sent to it.'); + + $tracker->trackRequest('Key7', false, 'Value7'); + + $this->assertEquals(8, $tracker->getCalls(), 'Tracker counts calls sent to it with duplicate keys.'); + + } + + public function testGetHits() + { + $tracker = $this->getPopulatedTracker(); + + $this->assertEquals(3, $tracker->getHits(), 'Tracker counts hits sent to it.'); + + $tracker->trackRequest('Key7', false, 'Value7'); + + $this->assertEquals(3, $tracker->getHits(), 'Tracker does not count misses as hits.'); + + $tracker->trackRequest('Key7', true, 'Value7'); + + $this->assertEquals(4, $tracker->getHits(), 'Tracker increments hits when sent them.'); + + } + + public function testGetQueries() + { + $tracker = $this->getPopulatedTracker(); + + $queries = $tracker->getQueries(); + $this->assertCount(6, $queries, 'Tracker returns queries passed to it.'); + + $tracker->trackRequest('Key6', false, 'Value6'); + $queries = $tracker->getQueries(); + $this->assertCount(7, $queries, 'Tracker returns queries with duplicate keys.'); + + $data = $this->getData(); + + foreach ($data as $index => $datum) { + $query = $queries[$index]; + $expectedTruth = $datum[1] ? 'true' : 'false'; + $this->assertEquals($datum[0], $query['key'], 'getQueries returns key for data example ' . $index); + $this->assertEquals($expectedTruth, $query['hit'], 'getQueries returns hit status as string for data example ' . $index); + $this->assertEquals($datum[3], $query['value'], 'getQueries returns value for data example ' . $index); + } + + + $tracker = $this->testConstruct('unpopulated'); + + $tracker->enableQueryLogging(false); + $tracker = $this->getPopulatedTracker($tracker); + $this->assertCount(0, $tracker->getQueries(), 'Tracker does not track queries when tracking is disabled'); + } + + protected function getPopulatedTracker($tracker = null) + { + if(is_null($tracker)) { + $tracker = $this->testConstruct('populated'); + } + + $data = $this->getData(); + foreach ($data as $datum) { + $tracker->trackRequest($datum[0], $datum[1], $datum[2]); + } + + return $tracker; + } + + + protected function getData() + { + return array( + array('Key1', true, 'Value1', '(string) Value1'), + array('Key2', false, 2, '(integer) 2'), + array('Key3', true, array(), "(array) Array\n(\n)\n"), + array('Key4', false, new \stdClass(), "(object) stdClass Object\n(\n)\n"), + array('Key5', true, 'Value5', '(string) Value5'), + array('Key6', false, 'Value6', '(string) Value6'), + ); + } +} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 7dd0fc9..4d1e1fe 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -26,3 +26,4 @@ $loader = require $filename; $loader->add('Tedivm\\StashBundle\\Test', __DIR__); +$loader->add('Stash\\Test', __DIR__ . '/../vendor/tedivm/stash/tests/'); diff --git a/composer.json b/composer.json index 8cff15a..680f5d8 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=5.3.0", - "tedivm/stash": "0.11.*", + "tedivm/stash": "dev-master", "symfony/framework-bundle": ">=2.1,<3.0", "symfony/yaml": ">=2.1,<3.0" },

{{ name }} settings