diff --git a/src/DependencyInjection/Compiler/CacheCompilerPass.php b/src/DependencyInjection/Compiler/CacheCompilerPass.php index 41fab1a..35847ea 100644 --- a/src/DependencyInjection/Compiler/CacheCompilerPass.php +++ b/src/DependencyInjection/Compiler/CacheCompilerPass.php @@ -43,7 +43,7 @@ protected function analyzeServicesTobeCached(ContainerBuilder $container) $proxyWarmup = $container->getDefinition('emag.cache.warmup'); $cacheProxyFactory = new Reference('emag.cache.proxy.factory'); $cacheServiceReference = new Reference($container->getParameter('emag.cache.service')); - $expressionLanguage = $container->hasParameter('emag.cache.expression.language') ? new Reference($container->getParameter('emag.cache.expression.language')) : null; + $expressionLanguage = $container->hasDefinition('emag.cache.expression.language') || $container->hasAlias('emag.cache.expression.language') ? new Reference('emag.cache.expression.language') : null; foreach ($container->getDefinitions() as $serviceId => $definition) { if (!class_exists($definition->getClass()) || $this->isFromIgnoredNamespace($container, $definition->getClass())) { @@ -101,4 +101,9 @@ private function isFromIgnoredNamespace(ContainerBuilder $container, $className) } return false; } + + private function getExpressionLanguage() + { + + } } diff --git a/src/DependencyInjection/EmagCacheExtension.php b/src/DependencyInjection/EmagCacheExtension.php index b0f3d4c..a7e15ac 100644 --- a/src/DependencyInjection/EmagCacheExtension.php +++ b/src/DependencyInjection/EmagCacheExtension.php @@ -7,8 +7,10 @@ use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -43,7 +45,7 @@ public function prepend(ContainerBuilder $container) } $expressionLanguage = new \ReflectionClass($container->getDefinition($config['expression_language'])->getClass()); - if (!$expressionLanguage->isSubclassOf(ExpressionLanguage::class) || !($expressionLanguage instanceof ExpressionLanguage::class)) { + if ($expressionLanguage->getName() !== ExpressionLanguage::class) { throw new CacheException(sprintf('You must provide a valid Expression Language service')); } } @@ -59,9 +61,12 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('emag.cache.service', $config['provider']); $container->setParameter('emag.cache.ignore.namespaces', $config['ignore_namespaces']); if (!$config['expression_language'] && class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { - $container->setParameter('emag.cache.expression.language', new ExpressionLanguage(new FilesystemAdapter('expr_cache'))); + $container->addDefinitions([ + 'emag.cache.filesystem.adapter' => (new Definition(FilesystemAdapter::class))->addArgument('expr_cache'), + 'emag.cache.expression.language'=> (new Definition(ExpressionLanguage::class))->addArgument(new Reference('emag.cache.filesystem.adapter')), + ]); } elseif ($config['expression_language']) { - $container->setParameter('emag.cache.expression.language', $config['expression_language']); + $container->setAlias('emag.cache.expression.language', $config['expression_language']); } $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); diff --git a/tests/CacheExpressionDefaultTest.php b/tests/CacheExpressionDefaultTest.php new file mode 100644 index 0000000..c75f643 --- /dev/null +++ b/tests/CacheExpressionDefaultTest.php @@ -0,0 +1,82 @@ + 'test_expr_lang_default']); + $this->container = self::$kernel->getContainer(); + } + + protected static function getKernelClass() + { + return get_class(new class('test_expr_lang_default', []) extends Kernel + { + public function registerBundles() + { + return [ + new \Emag\CacheBundle\EmagCacheBundle() + ]; + } + + public function registerContainerConfiguration(LoaderInterface $loader) + { + $loader->load(__DIR__ . '/config_default_expression.yml'); + } + + public function __construct($environment, $debug) + { + parent::__construct($environment, $debug); + + $loader = require __DIR__ . '/../vendor/autoload.php'; + + AnnotationRegistry::registerLoader(array($loader, 'loadClass')); + $this->rootDir = __DIR__ . '/app/'; + } + }); + } + + public function testDefaultExpressionLanguage() + { + /** @var CacheableExpressionClass $object */ + $object = $this->container->get('cache.expr.test.service'); + $methodName = 'getIntenseResult'; + $objectReflectionClass = new \ReflectionClass($object); + $annotationReader = $this->container->get('annotation_reader'); + /** @var CacheExpression $cacheExpressionAnnotation */ + $cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class); + $cacheExpressionAnnotation + ->setExpressionLanguage($this->container->get('emag.cache.expression.language')) + ->setContext($object) + ; + + $result = $object->$methodName(); + $this->assertContains($object->buildCachePrefix(), $cacheExpressionAnnotation->getCache()); + $this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->buildCachePrefix())); + $this->assertEquals($result, $object->$methodName()); + } + + public function tearDown() + { + static::$class = null; + } +} diff --git a/tests/CacheWrapperTest.php b/tests/CacheWrapperTest.php index d351978..7b251cc 100644 --- a/tests/CacheWrapperTest.php +++ b/tests/CacheWrapperTest.php @@ -155,14 +155,21 @@ public function testCachePrefixExpressions() { /** @var CacheableClass $object */ $object = $this->container->get('cache.testservice'); + $methodName = 'getCachePrefixFromExpression'; $objectReflectionClass = new \ReflectionClass($object); - $annotationReader = new AnnotationReader(); + $annotationReader = $this->container->get('annotation_reader'); /** @var CacheExpression $cacheExpressionAnnotation */ - $cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getCachePrefixFromExpression'), CacheExpression::class); - $cacheExpressionAnnotation->setContext($object); + $cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class); + $cacheExpressionAnnotation + ->setExpressionLanguage($this->container->get('emag.cache.expression.language')) + ->setContext($object) + ; + $result = $object->$methodName(); $this->assertContains($object->calculateCachePrefix(), $cacheExpressionAnnotation->getCache()); $this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->calculateCachePrefix())); + sleep(1); + $this->assertEquals($result, $object->$methodName()); } /** diff --git a/tests/Helpers/CacheableExpressionClass.php b/tests/Helpers/CacheableExpressionClass.php new file mode 100644 index 0000000..2c84665 --- /dev/null +++ b/tests/Helpers/CacheableExpressionClass.php @@ -0,0 +1,34 @@ +prefix = $prefix; + } + + /** + * @eMAG\CacheExpression(cache="this.buildCachePrefix()") + * + * @return int + */ + public function getIntenseResult() : int + { + return rand(); + } + + /** + * @return string + */ + public function buildCachePrefix() : string + { + return sprintf('_expr[%s]', $this->prefix); + } +} diff --git a/tests/IncorrectCachingServiceTest.php b/tests/IncorrectCachingServiceTest.php index 67ba766..54954a3 100644 --- a/tests/IncorrectCachingServiceTest.php +++ b/tests/IncorrectCachingServiceTest.php @@ -10,7 +10,7 @@ class IncorrectCachingServiceTest extends KernelTestCase { protected static function getKernelClass() { - return get_class(new class('test_incorrect_service', []) extends Kernel + return get_class(new class('test_incorrect_cache_service', []) extends Kernel { public function registerBundles() { @@ -21,7 +21,7 @@ public function registerBundles() public function registerContainerConfiguration(LoaderInterface $loader) { - $loader->load(__DIR__ . '/config_incorrect_service.yml'); + $loader->load(__DIR__ . '/config_incorrect_cache_service.yml'); } public function __construct($environment, $debug) @@ -42,6 +42,6 @@ public function __construct($environment, $debug) public function testIncorrectService() { static::$class = null; - self::bootKernel(['environment' => 'test_incorrect_service']); + self::bootKernel(['environment' => 'test_incorrect_cache_service']); } } \ No newline at end of file diff --git a/tests/IncorrectExpressiongLanguageServiceTest.php b/tests/IncorrectExpressiongLanguageServiceTest.php new file mode 100644 index 0000000..da290fe --- /dev/null +++ b/tests/IncorrectExpressiongLanguageServiceTest.php @@ -0,0 +1,47 @@ +load(__DIR__ . '/config_incorrect_expr_lang_service.yml'); + } + + public function __construct($environment, $debug) + { + require __DIR__ . '/../vendor/autoload.php'; + + parent::__construct($environment, $debug); + + $this->rootDir = __DIR__ . '/app/'; + } + }); + } + + /** + * @expectedException \Emag\CacheBundle\Exception\CacheException + * @expectedExceptionMessage You must provide a valid Expression Language service + */ + public function testIncorrectService() + { + static::$class = null; + self::bootKernel(['environment' => 'test_incorrect_expr_lang_service']); + } +} \ No newline at end of file diff --git a/tests/config.yml b/tests/config.yml index 499ea5e..889a916 100644 --- a/tests/config.yml +++ b/tests/config.yml @@ -1,5 +1,4 @@ parameters: - cache.service: cache.service max.value: 20 monolog: @@ -9,6 +8,7 @@ monolog: level: info emag_cache: provider: cache.service + expression_language: expr.lang.service services: cache_warmer: @@ -23,3 +23,5 @@ services: class: Emag\CacheBundle\Tests\Helpers\ExtendedCacheableClass annotation_reader: class: Doctrine\Common\Annotations\AnnotationReader + expr.lang.service: + class: Symfony\Component\ExpressionLanguage\ExpressionLanguage diff --git a/tests/config_default_expression.yml b/tests/config_default_expression.yml new file mode 100644 index 0000000..54ca2ad --- /dev/null +++ b/tests/config_default_expression.yml @@ -0,0 +1,13 @@ +emag_cache: + provider: cache.service + +services: + cache_warmer: + class: Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate + cache.service: + class: Symfony\Component\Cache\Adapter\ArrayAdapter + cache.expr.test.service: + class: Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass + arguments: ["prefix_cache"] + annotation_reader: + class: Doctrine\Common\Annotations\AnnotationReader diff --git a/tests/config_incorrect_expr_lang_service.yml b/tests/config_incorrect_expr_lang_service.yml new file mode 100644 index 0000000..e19fc4a --- /dev/null +++ b/tests/config_incorrect_expr_lang_service.yml @@ -0,0 +1,9 @@ +emag_cache: + provider: cache.service + expression_language: expr.lang.fake + +services: + expr.lang.fake: + class: Emag\CacheBundle\Tests\Helpers\CacheableClass + cache.service: + class: Symfony\Component\Cache\Adapter\ArrayAdapter