From 9bcee455cad4a13cb8b8011529d50dff850532c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= <lcobucci@gmail.com>
Date: Tue, 13 Sep 2016 12:56:20 +0000
Subject: [PATCH 1/3] Make child entity share the timestamp region with parent
 class

---
 lib/Doctrine/ORM/AbstractQuery.php            |  2 +-
 .../Entity/AbstractEntityPersister.php        |  2 +-
 ...condLevelCacheJoinTableInheritanceTest.php | 47 ++++++++++++++++++-
 ...ndLevelCacheSingleTableInheritanceTest.php | 43 ++++++++++++++++-
 4 files changed, 89 insertions(+), 5 deletions(-)

diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php
index 216fca542..7ac726f46 100644
--- a/lib/Doctrine/ORM/AbstractQuery.php
+++ b/lib/Doctrine/ORM/AbstractQuery.php
@@ -1038,7 +1038,7 @@ abstract class AbstractQuery
 
         $metadata = $this->_em->getClassMetadata($entityName);
 
-        return new Cache\TimestampCacheKey($metadata->getTableName());
+        return new Cache\TimestampCacheKey($metadata->rootEntityName);
     }
 
     /**
diff --git a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php
index a2071f42b..0a5aa1c65 100644
--- a/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php
+++ b/lib/Doctrine/ORM/Cache/Persister/Entity/AbstractEntityPersister.php
@@ -131,7 +131,7 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
         $this->cacheLogger      = $cacheConfig->getCacheLogger();
         $this->timestampRegion  = $cacheFactory->getTimestampRegion();
         $this->hydrator         = $cacheFactory->buildEntityHydrator($em, $class);
-        $this->timestampKey     = new TimestampCacheKey($this->class->getTableName());
+        $this->timestampKey     = new TimestampCacheKey($this->class->rootEntityName);
     }
 
     /**
diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
index e81b08b96..9864172fa 100644
--- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
+++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
@@ -3,8 +3,8 @@
 namespace Doctrine\Tests\ORM\Functional;
 
 use Doctrine\Tests\Models\Cache\Attraction;
-use Doctrine\Tests\Models\Cache\AttractionInfo;
 use Doctrine\Tests\Models\Cache\AttractionContactInfo;
+use Doctrine\Tests\Models\Cache\AttractionInfo;
 use Doctrine\Tests\Models\Cache\AttractionLocationInfo;
 
 /**
@@ -188,4 +188,47 @@ class SecondLevelCacheJoinTableInheritanceTest extends SecondLevelCacheAbstractT
         $this->assertInstanceOf(AttractionContactInfo::CLASSNAME, $entity->getInfos()->get(0));
         $this->assertEquals($this->attractionsInfo[0]->getFone(), $entity->getInfos()->get(0)->getFone());
     }
-}
\ No newline at end of file
+
+    public function testQueryCacheShouldBeEvictedOnTimestampUpdate()
+    {
+        $this->loadFixturesCountries();
+        $this->loadFixturesStates();
+        $this->loadFixturesCities();
+        $this->loadFixturesAttractions();
+        $this->loadFixturesAttractionsInfo();
+        $this->evictRegions();
+        $this->_em->clear();
+
+        $queryCount = $this->getCurrentQueryCount();
+        $dql        = 'SELECT attractionInfo FROM Doctrine\Tests\Models\Cache\AttractionInfo attractionInfo';
+
+        $result1    = $this->_em->createQuery($dql)
+            ->setCacheable(true)
+            ->getResult();
+
+        $this->assertCount(count($this->attractionsInfo), $result1);
+        $this->assertEquals($queryCount + 5, $this->getCurrentQueryCount());
+
+        $contact = new AttractionContactInfo(
+            '1234-1234',
+            $this->_em->find(Attraction::class, $this->attractions[5]->getId())
+        );
+
+        $this->_em->persist($contact);
+        $this->_em->flush();
+        $this->_em->clear();
+
+        $queryCount = $this->getCurrentQueryCount();
+
+        $result2 = $this->_em->createQuery($dql)
+            ->setCacheable(true)
+            ->getResult();
+
+        $this->assertCount(count($this->attractionsInfo) + 1, $result2);
+        $this->assertEquals($queryCount + 6, $this->getCurrentQueryCount());
+
+        foreach ($result2 as $entity) {
+            $this->assertInstanceOf(AttractionInfo::CLASSNAME, $entity);
+        }
+    }
+}
diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
index 3f8487d99..0ee1df176 100644
--- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
+++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
@@ -210,4 +210,45 @@ class SecondLevelCacheSingleTableInheritanceTest extends SecondLevelCacheAbstrac
         $this->assertEquals($this->attractions[0]->getName(), $entity->getAttractions()->get(0)->getName());
         $this->assertEquals($this->attractions[1]->getName(), $entity->getAttractions()->get(1)->getName());
     }
-}
\ No newline at end of file
+
+    public function testQueryCacheShouldBeEvictedOnTimestampUpdate()
+    {
+        $this->loadFixturesCountries();
+        $this->loadFixturesStates();
+        $this->loadFixturesCities();
+        $this->loadFixturesAttractions();
+        $this->_em->clear();
+
+        $queryCount = $this->getCurrentQueryCount();
+        $dql        = 'SELECT attraction FROM Doctrine\Tests\Models\Cache\Attraction attraction';
+
+        $result1    = $this->_em->createQuery($dql)
+            ->setCacheable(true)
+            ->getResult();
+
+        $this->assertCount(count($this->attractions), $result1);
+        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
+
+        $contact = new Beach(
+            'Botafogo',
+            $this->_em->find(City::class, $this->cities[1]->getId())
+        );
+
+        $this->_em->persist($contact);
+        $this->_em->flush();
+        $this->_em->clear();
+
+        $queryCount = $this->getCurrentQueryCount();
+
+        $result2 = $this->_em->createQuery($dql)
+            ->setCacheable(true)
+            ->getResult();
+
+        $this->assertCount(count($this->attractions) + 1, $result2);
+        $this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
+
+        foreach ($result2 as $entity) {
+            $this->assertInstanceOf(Attraction::CLASSNAME, $entity);
+        }
+    }
+}

From af99cba28cd292ecca8b95ccf15250804f6545c5 Mon Sep 17 00:00:00 2001
From: Marco Pivetta <ocramius@gmail.com>
Date: Wed, 23 Nov 2016 18:02:15 +0100
Subject: [PATCH 2/3] #6028 removed specific `::class` usage, since 2.5.x still
 supports PHP 5.4.x

---
 .../ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
index 9864172fa..5e70effee 100644
--- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
+++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheJoinTableInheritanceTest.php
@@ -211,7 +211,7 @@ class SecondLevelCacheJoinTableInheritanceTest extends SecondLevelCacheAbstractT
 
         $contact = new AttractionContactInfo(
             '1234-1234',
-            $this->_em->find(Attraction::class, $this->attractions[5]->getId())
+            $this->_em->find(Attraction::CLASSNAME, $this->attractions[5]->getId())
         );
 
         $this->_em->persist($contact);

From 2122297fdbeb5cca6d5da9059712924a80b7e8b3 Mon Sep 17 00:00:00 2001
From: Marco Pivetta <ocramius@gmail.com>
Date: Wed, 23 Nov 2016 18:06:14 +0100
Subject: [PATCH 3/3] #6028 removed specific `::class` usage, since 2.5.x still
 supports PHP 5.4.x

---
 .../Functional/SecondLevelCacheSingleTableInheritanceTest.php   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
index 0ee1df176..de5059928 100644
--- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
+++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheSingleTableInheritanceTest.php
@@ -231,7 +231,7 @@ class SecondLevelCacheSingleTableInheritanceTest extends SecondLevelCacheAbstrac
 
         $contact = new Beach(
             'Botafogo',
-            $this->_em->find(City::class, $this->cities[1]->getId())
+            $this->_em->find(City::CLASSNAME, $this->cities[1]->getId())
         );
 
         $this->_em->persist($contact);