diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/GH2947Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH2947Test.php new file mode 100644 index 000000000..0b21b57f0 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/GH2947Test.php @@ -0,0 +1,95 @@ +resultCacheImpl = new ArrayCache(); + + parent::setUp(); + + $this->_schemaTool->createSchema([$this->_em->getClassMetadata(GH2947Car::class)]); + } + + public function testIssue() + { + $this->createData(); + $initialQueryCount = $this->getCurrentQueryCount(); + + $query = $this->createQuery(); + self::assertEquals('BMW', (string) $query->getSingleResult()); + self::assertEquals($initialQueryCount + 1, $this->getCurrentQueryCount()); + + $this->updateData(); + self::assertEquals('BMW', (string) $query->getSingleResult()); + self::assertEquals($initialQueryCount + 2, $this->getCurrentQueryCount()); + + $query->expireResultCache(true); + self::assertEquals('Dacia', (string) $query->getSingleResult()); + self::assertEquals($initialQueryCount + 3, $this->getCurrentQueryCount()); + + $query->expireResultCache(false); + self::assertEquals('Dacia', (string) $query->getSingleResult()); + self::assertEquals($initialQueryCount + 3, $this->getCurrentQueryCount()); + } + + private function createQuery() + { + return $this->_em->createQueryBuilder() + ->select('car') + ->from(GH2947Car::class, 'car') + ->getQuery() + ->useResultCache(true, 3600, 'foo-cache-id'); + } + + private function createData() + { + $this->_em->persist(new GH2947Car('BMW')); + $this->_em->flush(); + $this->_em->clear(); + } + + private function updateData() + { + $this->_em->createQueryBuilder() + ->update(GH2947Car::class, 'car') + ->set('car.brand', ':newBrand') + ->where('car.brand = :oldBrand') + ->setParameter('newBrand', 'Dacia') + ->setParameter('oldBrand', 'BMW') + ->getQuery() + ->execute(); + } +} + +/** + * @Entity + * @Table(name="GH2947_car") + */ +class GH2947Car +{ + /** + * @Id + * @Column(type="string", length=25) + * @GeneratedValue(strategy="NONE") + */ + public $brand; + + public function __construct(string $brand) + { + $this->brand = $brand; + } + + public function __toString(): string + { + return $this->brand; + } +} diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 2ff2c347c..5716f2a50 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -68,6 +68,13 @@ abstract class OrmFunctionalTestCase extends OrmTestCase */ protected $_usedModelSets = []; + /** + * To be configured by the test that uses result set cache + * + * @var \Doctrine\Common\Cache\Cache|null + */ + protected $resultCacheImpl; + /** * Whether the database schema has already been created. * @@ -699,6 +706,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies'); + if (null !== $this->resultCacheImpl) { + $config->setResultCacheImpl($this->resultCacheImpl); + } + $enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE'); if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) {