diff --git a/Module.php b/Module.php index 9ad0510..6c9363b 100644 --- a/Module.php +++ b/Module.php @@ -70,7 +70,8 @@ public function onBootstrap(EventInterface $e) $em = $app->getEventManager(); $sm = $app->getServiceManager(); - $listener = new Listener\Cache($sm); - $listener->attach($em); + $listener = $sm->get('CacheListener'); + + $em->attach($listener); } } \ No newline at end of file diff --git a/config/module.config.php b/config/module.config.php index 8d02e50..0bcd211 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -1,9 +1,18 @@ array( 'cache' => null, 'routes' => array( ), ), + + 'service_manager' => array( + 'factories' => array( + 'CacheService' => 'SlmCache\Factory\ServiceFactory', + 'CacheListener' => 'SlmCache\Factory\ListenerFactory', + ), + ), ); \ No newline at end of file diff --git a/src/SlmCache/Factory/ListenerFactory.php b/src/SlmCache/Factory/ListenerFactory.php new file mode 100644 index 0000000..b00a4ca --- /dev/null +++ b/src/SlmCache/Factory/ListenerFactory.php @@ -0,0 +1,23 @@ +get('CacheService')); + } + +} \ No newline at end of file diff --git a/src/SlmCache/Factory/ServiceFactory.php b/src/SlmCache/Factory/ServiceFactory.php new file mode 100644 index 0000000..07a83db --- /dev/null +++ b/src/SlmCache/Factory/ServiceFactory.php @@ -0,0 +1,23 @@ + + * @copyright 2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://juriansluiman.nl + */ + +namespace SlmCache\Listener; + +use SlmCache\Service\CacheService; + +use Zend\EventManager\AbstractListenerAggregate; +use Zend\EventManager\EventManagerInterface; +use Zend\Mvc\MvcEvent; +use Zend\ServiceManager\ServiceLocatorInterface; + +class CacheListener extends AbstractListenerAggregate +{ + protected $cacheService; + + public function __construct(CacheService $cacheService) + { + $this->cacheService = $cacheService; + } + + /** + * {@inheritDoc} + */ + public function attach(EventManagerInterface $events) + { + $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRoute'), -100); + $events->attach(MvcEvent::EVENT_FINISH, array($this, 'onFinish'), 100); + } + + public function onRoute(MvcEvent $e) + { + $this->cacheService->matchRoute($e); + } + + public function onFinish(MvcEvent $e) + { + $this->cacheService->saveRoute($e); + } + +} diff --git a/src/SlmCache/Listener/Cache.php b/src/SlmCache/Service/CacheService.php similarity index 85% rename from src/SlmCache/Listener/Cache.php rename to src/SlmCache/Service/CacheService.php index 031e25a..31688e0 100644 --- a/src/SlmCache/Listener/Cache.php +++ b/src/SlmCache/Service/CacheService.php @@ -38,42 +38,42 @@ * @link http://juriansluiman.nl */ -namespace SlmCache\Listener; +namespace SlmCache\Service; -use Zend\EventManager\AbstractListenerAggregate; -use Zend\EventManager\EventManagerInterface; +use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\Mvc\MvcEvent; use Zend\Mvc\Router\RouteMatch; -use Zend\Http\Response; -use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Cache\StorageFactory; use Zend\Cache\Storage\StorageInterface; -class Cache extends AbstractListenerAggregate +class CacheService implements ServiceLocatorAwareInterface { - protected $cache_prefix = 'slm_cache_'; - protected $match; protected $serviceLocator; - public function __construct(ServiceLocatorInterface $sl) - { - $this->serviceLocator = $sl; + protected $config = array(); - $config = $sl->get('Config'); + protected $match; - if (isset($config['slm_cache']['cache_prefix'])) { - $this->cache_prefix = $config['slm_cache']['cache_prefix']; - } + public function __construct(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + + $this->configSetup(); } - /** - * {@inheritDoc} - */ - public function attach(EventManagerInterface $events) + protected function configSetup() { - $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'matchRoute')); - $events->attach(MvcEvent::EVENT_FINISH, array($this, 'saveRoute')); + $config = $this->serviceLocator->get('Config'); + + if (!isset($config['slm_cache']['cache_prefix'])) { + $config['slm_cache']['cache_prefix'] = 'slm_cache_'; + } + + // TODO: implement config validation?! + + $this->config = $config; } public function matchRoute(MvcEvent $e) @@ -201,4 +201,24 @@ protected function getCache(MvcEvent $e) return $cache; } -} + /** + * Set service locator + * + * @param ServiceLocatorInterface $serviceLocator + */ + public function setServiceLocator(ServiceLocatorInterface $serviceLocator) + { + $this->serviceLocator = $serviceLocator; + } + + /** + * Get service locator + * + * @return ServiceLocatorInterface + */ + public function getServiceLocator() + { + return $this->serviceLocator; + } + +} \ No newline at end of file diff --git a/tests/SlmCacheTest/Factory/ListenerFactoryTest.php b/tests/SlmCacheTest/Factory/ListenerFactoryTest.php new file mode 100644 index 0000000..859487f --- /dev/null +++ b/tests/SlmCacheTest/Factory/ListenerFactoryTest.php @@ -0,0 +1,30 @@ +setService('Config', array()); + + $cacheService = new CacheService($sl); + + $mock = $this->getMock('SlmCache\Factory\ListenerFactory', array('createService')); + $mock->expects($this->once()) + ->method('createService') + ->with($this->equalTo($sl)) + ->will($this->returnValue(new CacheListener($cacheService))); + + $this->assertInstanceOf('SlmCache\Listener\CacheListener', $mock->createService($sl)); + } +} \ No newline at end of file diff --git a/tests/SlmCacheTest/Factory/ServiceFactoryTest.php b/tests/SlmCacheTest/Factory/ServiceFactoryTest.php new file mode 100644 index 0000000..a41e057 --- /dev/null +++ b/tests/SlmCacheTest/Factory/ServiceFactoryTest.php @@ -0,0 +1,27 @@ +setService('Config', array()); + + $mock = $this->getMock('SlmCache\Factory\ServiceFactory', array('createService')); + $mock->expects($this->once()) + ->method('createService') + ->with($this->equalTo($sl)) + ->will($this->returnValue(new CacheService($sl))); + + $this->assertInstanceOf('SlmCache\Service\CacheService', $mock->createService($sl)); + } +} \ No newline at end of file diff --git a/tests/SlmCacheTest/Listener/CacheTest.php b/tests/SlmCacheTest/Listener/CacheListenerTest.php similarity index 50% rename from tests/SlmCacheTest/Listener/CacheTest.php rename to tests/SlmCacheTest/Listener/CacheListenerTest.php index 70cbd37..8bb0597 100644 --- a/tests/SlmCacheTest/Listener/CacheTest.php +++ b/tests/SlmCacheTest/Listener/CacheListenerTest.php @@ -40,25 +40,27 @@ namespace SlmCacheTest\Listener; use PHPUnit_Framework_TestCase as TestCase; -use PHPUnit_Framework_Assert; -use SlmCache\Listener\Cache as CacheListener; +use SlmCache\Listener\CacheListener; +use SlmCache\Service\CacheService; use Zend\EventManager\EventManager; use Zend\ServiceManager\ServiceManager; use Zend\Mvc\MvcEvent; use Zend\Mvc\Router\RouteMatch; -class CacheTest extends TestCase +class CacheListenerTest extends TestCase { public function testMatchRouteIsTriggered() { $sl = new ServiceManager; $sl->setService('Config', array()); - $mock = $this->getMock('SlmCache\Listener\Cache', array('matchRoute'), array($sl)); + $cacheService = new CacheService($sl); + + $mock = $this->getMock('SlmCache\Listener\CacheListener', array('onRoute'), array($cacheService)); $mock->expects($this->once()) - ->method('matchRoute'); + ->method('onRoute'); $em = new EventManager; $mock->attach($em); @@ -73,9 +75,11 @@ public function testSaveRouteIsTriggered() $sl = new ServiceManager; $sl->setService('Config', array()); - $mock = $this->getMock('SlmCache\Listener\Cache', array('saveRoute'), array($sl)); + $cacheService = new CacheService($sl); + + $mock = $this->getMock('SlmCache\Listener\CacheListener', array('onFinish'), array($cacheService)); $mock->expects($this->once()) - ->method('saveRoute'); + ->method('onFinish'); $em = new EventManager; $mock->attach($em); @@ -84,96 +88,4 @@ public function testSaveRouteIsTriggered() $event->setName(MvcEvent::EVENT_FINISH); $em->trigger($event); } - - public function testMatchReturnsNullForMissingRouteMatch() - { - $sl = new ServiceManager; - $sl->setService('Config', array()); - - $listener = new CacheListener($sl); - - $event = new MvcEvent; - $result = $listener->matchRoute($event); - - $this->assertNull($result); - } - - public function testMatchReturnsNullForMissingKey() - { - $sl = new ServiceManager; - $sl->setService('Config', array( - 'slm_cache' => array( - 'routes' => array() - ), - )); - $listener = new CacheListener($sl); - - $event = new MvcEvent; - $event->setRouteMatch(new RouteMatch(array())); - $result = $listener->matchRoute($event); - - $this->assertNull($result); - } - - public function testSuccessfulMatchInvokesCacheRetrieval() - { - $sl = new ServiceManager; - $sl->setService('Config', array( - 'slm_cache' => array( - 'routes' => array( - 'home' => array() - ), - ), - )); - - $mock = $this->getMock('SlmCache\Listener\Cache', array('fromCache'), array($sl)); - $mock->expects($this->once()) - ->method('fromCache'); - - $match = new RouteMatch(array()); - $match->setMatchedRouteName('home'); - - $event = new MvcEvent; - $event->setRouteMatch($match); - - $mock->matchRoute($event); - } - - public function testCachePrefixIsUserDefined() - { - $config = array( - 'slm_cache' => array( - 'cache_prefix' => 'my_cache_prefix' - ) - ); - - $sl = new ServiceManager; - $sl->setService('Config', $config); - - $listener = new CacheListener($sl); - - $this->assertEquals( - $config['slm_cache']['cache_prefix'], - PHPUnit_Framework_Assert::readAttribute($listener, 'cache_prefix') - ); - } - - public function testCachePrefixIsDefault() - { - $config = array( - 'slm_cache' => array( - 'cache' => array(), - ) - ); - - $sl = new ServiceManager; - $sl->setService('Config', $config); - - $listener = new CacheListener($sl); - - $this->assertEquals( - 'slm_cache_', - PHPUnit_Framework_Assert::readAttribute($listener, 'cache_prefix') - ); - } } \ No newline at end of file diff --git a/tests/SlmCacheTest/Service/CacheServiceTest.php b/tests/SlmCacheTest/Service/CacheServiceTest.php new file mode 100644 index 0000000..fe1ca1d --- /dev/null +++ b/tests/SlmCacheTest/Service/CacheServiceTest.php @@ -0,0 +1,105 @@ + array()); + + $sl = new ServiceManager; + $sl->setService('Config', $config); + + $cacheService = new CacheService($sl); + + $generatedConfig = Assert::readAttribute($cacheService, 'config'); + + $this->assertArrayHasKey('cache_prefix', $generatedConfig['slm_cache']); + $this->assertEquals('slm_cache_', $generatedConfig['slm_cache']['cache_prefix']); + } + + public function testUserDefinedCachePrefix() + { + $config = array( + 'slm_cache' => array( + 'cache_prefix' => 'my_cache_prefix_' + ), + ); + + $sl = new ServiceManager; + $sl->setService('Config', $config); + + $cacheService = new CacheService($sl); + + $generatedConfig = Assert::readAttribute($cacheService, 'config'); + + $this->assertArrayHasKey('cache_prefix', $generatedConfig['slm_cache']); + $this->assertEquals($config['slm_cache']['cache_prefix'], $generatedConfig['slm_cache']['cache_prefix']); + } + + public function testMatchReturnsNullForMissingRouteMatch() + { + $sl = new ServiceManager; + $sl->setService('Config', array()); + + $cacheService = new CacheService($sl); + + $event = new MvcEvent; + $result = $cacheService->matchRoute($event); + + $this->assertNull($result); + } + + public function testMatchReturnsNullForMissingKey() + { + $sl = new ServiceManager; + $sl->setService('Config', array( + 'slm_cache' => array( + 'routes' => array() + ), + )); + $cacheService = new CacheService($sl); + + $event = new MvcEvent; + $event->setRouteMatch(new RouteMatch(array())); + $result = $cacheService->matchRoute($event); + + $this->assertNull($result); + } + + public function testSuccessfulMatchInvokesCacheRetrieval() + { + $sl = new ServiceManager; + $sl->setService('Config', array( + 'slm_cache' => array( + 'routes' => array( + 'home' => array() + ), + ), + )); + + $mock = $this->getMock('SlmCache\Service\CacheService', array('fromCache'), array($sl)); + $mock->expects($this->once()) + ->method('fromCache'); + + $match = new RouteMatch(array()); + $match->setMatchedRouteName('home'); + + $event = new MvcEvent; + $event->setRouteMatch($match); + + $mock->matchRoute($event); + } + +} \ No newline at end of file