-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bogdan Rancichi
committed
Jan 7, 2017
1 parent
db875ca
commit 6196890
Showing
10 changed files
with
215 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |