From d5d4836a74cbbec2c1234a695ff79819d9a74813 Mon Sep 17 00:00:00 2001 From: Bogdan Rancichi Date: Thu, 15 Dec 2016 13:22:44 +0200 Subject: [PATCH] add test for cache key collision --- tests/CacheWrapperTest.php | 27 ++++++++++++++++++++++++++- tests/Helpers/CacheableClass.php | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/CacheWrapperTest.php b/tests/CacheWrapperTest.php index a4676ca..f986ad7 100644 --- a/tests/CacheWrapperTest.php +++ b/tests/CacheWrapperTest.php @@ -1,6 +1,9 @@ assertEquals($dataWithParam, $object->getCachedTime(300)); } + public function testWithSamePrefix() + { + /** @var CacheableClass $object */ + $object = $this->container->get('cache.testservice'); + + $objectReflectionClass = new \ReflectionClass($object); + $annotationReader = new AnnotationReader(); + + /** @var Cache $methodOne */ + $methodOne = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getComputationOneWithoutParametersSamePrefix'), Cache::class); + /** @var Cache $methodTwo */ + $methodTwo = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getComputationTwoWithoutParametersSamePrefix'), Cache::class); + + $resultOne = $object->getComputationOneWithoutParametersSamePrefix(); + $resultTwo = $object->getComputationTwoWithoutParametersSamePrefix(); + + $this->assertEquals($methodOne->getCache(), $methodTwo->getCache()); + $this->assertNotEquals($resultOne, $resultTwo); + sleep(1); + $this->assertNotEquals($object->getComputationOneWithoutParametersSamePrefix(), $object->getComputationTwoWithoutParametersSamePrefix()); + } + public function testWithParamsExtendedClass() { $object = $this->container->get('cache.testservice.extended'); @@ -105,7 +130,7 @@ public function testWithMultiParams() /** * @expectedExceptionMessage Missing param3 - * @expectedException CacheBundle\Exception\CacheException + * @expectedException \CacheBundle\Exception\CacheException */ public function testWithWrongParamNames() { diff --git a/tests/Helpers/CacheableClass.php b/tests/Helpers/CacheableClass.php index b09c2cd..eacfe37 100644 --- a/tests/Helpers/CacheableClass.php +++ b/tests/Helpers/CacheableClass.php @@ -110,4 +110,24 @@ protected function protectedMethod() { return time(); } + + /** + * @Cache(cache="__key") + * + * @return int + */ + public function getComputationOneWithoutParametersSamePrefix() : int + { + return 10; + } + + /** + * @Cache(cache="__key") + * + * @return int + */ + public function getComputationTwoWithoutParametersSamePrefix() : int + { + return 20; + } } \ No newline at end of file