File: DDC4003Test.php

package info (click to toggle)
doctrine 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,556 kB
  • sloc: php: 108,620; xml: 1,340; makefile: 35; sh: 14
file content (64 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\Cache\Bar;
use Doctrine\Tests\ORM\Functional\SecondLevelCacheFunctionalTestCase;

use function assert;
use function uniqid;

class DDC4003Test extends SecondLevelCacheFunctionalTestCase
{
    public function testReadsThroughRepositorySameDataThatItWroteInCache(): void
    {
        $this->loadFixturesCountries();
        $this->loadFixturesStates();
        $this->loadFixturesCities();
        $this->loadFixturesAttractions();

        // Get the id of the first bar
        $id = $this->attractions[0]->getId();

        $repository = $this->_em->getRepository(Bar::class);

        /**
         * This instance is fresh new, no QueryCache, so the full entity gets loaded from DB.
         * It will be saved in the WRONG KEY (notice the cache.bar at the end):
         * doctrine_tests_models_cache_attraction[doctrine_tests_models_cache_attraction_doctrine.tests.models.cache.bar_1][1]
         */
        $bar = $repository->findOneBy(['id' => $id]);
        assert($bar instanceof Bar);

        // Let's change it so that we can compare its state
        $bar->setName($newName = uniqid());
        $this->_em->persist($bar);
        $this->_em->flush();

        /**
         * Flush did 2 important things for us:
         * 1: saved the entity in its right key (notice the cache.attraction at the end):
         *     doctrine_tests_models_cache_attraction[doctrine_tests_models_cache_attraction_doctrine.tests.models.cache.attraction_1][1]
         * 2: Updated the TimestampRegion cache, so that the QueryCache is actually discarded.
         *
         * So, first findOneBy will hit DB, and its entity will not be loaded from cache.
         */
        $repository->findOneBy(['id' => $id]);

        // Lets clear EM so that we don't hit IdentityMap at all.
        $this->_em->clear();

        /**
         * Here's the failing step:
         * Right now QueryCache will HIT, as nothing changed between the last one and now.
         * QueryCache holds a reference to the WRONG KEY, as we saw was formed in line 24 of this test.
         * So this instance won't be updated and will have the original name ("Boteco São Bento"), and not the uniqid().
         */
        $cached = $repository->findOneBy(['id' => $id]);
        assert($cached instanceof Bar);

        self::assertEquals($newName, $cached->getName());
    }
}