Skip to content

Commit

Permalink
complete cache expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Rancichi committed Jan 7, 2017
1 parent db875ca commit 6196890
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/DependencyInjection/Compiler/CacheCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down Expand Up @@ -101,4 +101,9 @@ private function isFromIgnoredNamespace(ContainerBuilder $container, $className)
}
return false;
}

private function getExpressionLanguage()
{

}
}
11 changes: 8 additions & 3 deletions src/DependencyInjection/EmagCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'));
}
}
Expand All @@ -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'));
Expand Down
82 changes: 82 additions & 0 deletions tests/CacheExpressionDefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace CacheBundle\Tests;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Emag\CacheBundle\Annotation\CacheExpression;
use Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;

class CacheExpressionDefaultTest extends KernelTestCase
{
/**
* @var ContainerInterface
*/
protected $container;

public function setUp()
{
parent::setUp();

static::$class = null;
self::bootKernel(['environment' => '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;
}
}
13 changes: 10 additions & 3 deletions tests/CacheWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/Helpers/CacheableExpressionClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Emag\CacheBundle\Tests\Helpers;

use Emag\CacheBundle\Annotation as eMAG;

class CacheableExpressionClass
{
/** @var string */
private $prefix;

public function __construct($prefix)
{
$this->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);
}
}
6 changes: 3 additions & 3 deletions tests/IncorrectCachingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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)
Expand All @@ -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']);
}
}
47 changes: 47 additions & 0 deletions tests/IncorrectExpressiongLanguageServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Emag\CacheBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class IncorrectExpressionLanguageServiceTest extends KernelTestCase
{
protected static function getKernelClass()
{
return get_class(new class('test_incorrect_expr_lang_service', []) extends Kernel
{
public function registerBundles()
{
return [
new \Emag\CacheBundle\EmagCacheBundle()
];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->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']);
}
}
4 changes: 3 additions & 1 deletion tests/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
parameters:
cache.service: cache.service
max.value: 20

monolog:
Expand All @@ -9,6 +8,7 @@ monolog:
level: info
emag_cache:
provider: cache.service
expression_language: expr.lang.service

services:
cache_warmer:
Expand All @@ -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
13 changes: 13 additions & 0 deletions tests/config_default_expression.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/config_incorrect_expr_lang_service.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6196890

Please sign in to comment.