|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +namespace CacheBundle\Tests; | 
|  | 4 | + | 
|  | 5 | +use Doctrine\Common\Annotations\AnnotationReader; | 
|  | 6 | +use Doctrine\Common\Annotations\AnnotationRegistry; | 
|  | 7 | +use Emag\CacheBundle\Annotation\CacheExpression; | 
|  | 8 | +use Emag\CacheBundle\Tests\Helpers\CacheableExpressionClass; | 
|  | 9 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | 
|  | 10 | +use Symfony\Component\Config\Loader\LoaderInterface; | 
|  | 11 | +use Symfony\Component\DependencyInjection\ContainerInterface; | 
|  | 12 | +use Symfony\Component\HttpKernel\Kernel; | 
|  | 13 | + | 
|  | 14 | +class CacheExpressionDefaultTest extends KernelTestCase | 
|  | 15 | +{ | 
|  | 16 | +    /** | 
|  | 17 | +     * @var ContainerInterface | 
|  | 18 | +     */ | 
|  | 19 | +    protected $container; | 
|  | 20 | + | 
|  | 21 | +    public function setUp() | 
|  | 22 | +    { | 
|  | 23 | +        parent::setUp(); | 
|  | 24 | + | 
|  | 25 | +        static::$class = null; | 
|  | 26 | +        self::bootKernel(['environment' => 'test_expr_lang_default']); | 
|  | 27 | +        $this->container = self::$kernel->getContainer(); | 
|  | 28 | +    } | 
|  | 29 | + | 
|  | 30 | +    protected static function getKernelClass() | 
|  | 31 | +    { | 
|  | 32 | +        return get_class(new class('test_expr_lang_default', []) extends Kernel | 
|  | 33 | +        { | 
|  | 34 | +            public function registerBundles() | 
|  | 35 | +            { | 
|  | 36 | +                return [ | 
|  | 37 | +                    new \Emag\CacheBundle\EmagCacheBundle() | 
|  | 38 | +                ]; | 
|  | 39 | +            } | 
|  | 40 | + | 
|  | 41 | +            public function registerContainerConfiguration(LoaderInterface $loader) | 
|  | 42 | +            { | 
|  | 43 | +                $loader->load(__DIR__ . '/config_default_expression.yml'); | 
|  | 44 | +            } | 
|  | 45 | + | 
|  | 46 | +            public function __construct($environment, $debug) | 
|  | 47 | +            { | 
|  | 48 | +                parent::__construct($environment, $debug); | 
|  | 49 | + | 
|  | 50 | +                $loader = require __DIR__ . '/../vendor/autoload.php'; | 
|  | 51 | + | 
|  | 52 | +                AnnotationRegistry::registerLoader(array($loader, 'loadClass')); | 
|  | 53 | +                $this->rootDir = __DIR__ . '/app/'; | 
|  | 54 | +            } | 
|  | 55 | +        }); | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    public function testDefaultExpressionLanguage() | 
|  | 59 | +    { | 
|  | 60 | +        /** @var CacheableExpressionClass $object */ | 
|  | 61 | +        $object = $this->container->get('cache.expr.test.service'); | 
|  | 62 | +        $methodName = 'getIntenseResult'; | 
|  | 63 | +        $objectReflectionClass = new \ReflectionClass($object); | 
|  | 64 | +        $annotationReader = $this->container->get('annotation_reader'); | 
|  | 65 | +        /** @var CacheExpression $cacheExpressionAnnotation */ | 
|  | 66 | +        $cacheExpressionAnnotation = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), $methodName), CacheExpression::class); | 
|  | 67 | +        $cacheExpressionAnnotation | 
|  | 68 | +            ->setExpressionLanguage($this->container->get('emag.cache.expression.language')) | 
|  | 69 | +            ->setContext($object) | 
|  | 70 | +        ; | 
|  | 71 | + | 
|  | 72 | +        $result = $object->$methodName(); | 
|  | 73 | +        $this->assertContains($object->buildCachePrefix(), $cacheExpressionAnnotation->getCache()); | 
|  | 74 | +        $this->assertEquals(0, strpos($cacheExpressionAnnotation->getCache(), $object->buildCachePrefix())); | 
|  | 75 | +        $this->assertEquals($result, $object->$methodName()); | 
|  | 76 | +    } | 
|  | 77 | + | 
|  | 78 | +    public function tearDown() | 
|  | 79 | +    { | 
|  | 80 | +        static::$class = null; | 
|  | 81 | +    } | 
|  | 82 | +} | 
0 commit comments