Completed
Pull Request — master (#7709)
by
unknown
09:31
created

GH7708ArrayCacheMock   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 11
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A doFetch() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\ORM\Annotation as ORM;
9
use Doctrine\ORM\Cache\{DefaultCacheFactory, RegionsConfiguration};
0 ignored issues
show
Coding Style introduced by
Use statements should be sorted alphabetically. The first wrong one is DefaultCacheFactory.
Loading history...
introduced by
Group use declaration is disallowed, use single use for every import.
Loading history...
introduced by
Multiple used types per use statement are forbidden.
Loading history...
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Tools\{SchemaTool, Setup};
0 ignored issues
show
introduced by
Group use declaration is disallowed, use single use for every import.
Loading history...
introduced by
Multiple used types per use statement are forbidden.
Loading history...
12
use Doctrine\Tests\TestUtil;
13
14
use PHPUnit\Framework\TestCase;
0 ignored issues
show
introduced by
Expected 0 lines between same types of use statement, found 1.
Loading history...
15
16
class GH7708Test extends TestCase
17
{
18
    protected $entityManager;
19
20
    protected function getEntityManager()
21
    {
22
        if (!$this->entityManager) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
23
            $regionsConfig = new RegionsConfiguration();
24
25
            $cacheDriver = new GH7708ArrayCacheMock();
26
27
            $cacheFactory = new DefaultCacheFactory($regionsConfig, $cacheDriver);
28
29
            $conn = TestUtil::getConnection();
30
31
            $config = Setup::createAnnotationMetadataConfiguration([]);
32
33
            $config->setMetadataCacheImpl($cacheDriver);
34
            $config->setQueryCacheImpl($cacheDriver);
35
            $config->setResultCacheImpl($cacheDriver);
36
            $config->setHydrationCacheImpl($cacheDriver);
37
38
            $config->setSecondLevelCacheEnabled();
39
            $secondLevelCacheConfig = $config->getSecondLevelCacheConfiguration();
40
            $secondLevelCacheConfig->setCacheFactory($cacheFactory);
41
42
            $this->entityManager = EntityManager::create($conn, $config);
43
44
            // Creating a schema
45
46
            $schemaTool = new SchemaTool($this->entityManager);
47
            $schemaTool->createSchema([
48
                $this->entityManager->getClassMetadata(GH7708Car::class),
49
                $this->entityManager->getClassMetadata(GH7708Model::class),
50
                $this->entityManager->getClassMetadata(GH7708Drive::class)
0 ignored issues
show
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
51
            ]);
52
53
            // Populating with data
54
55
            $frontDrive = new GH7708Drive('Front');
56
            $rearDrive = new GH7708Drive('Rear');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
57
            $fullDrive = new GH7708Drive('Full');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
59
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A4'), $rearDrive));
60
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A5'), $rearDrive));
61
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A6'), $rearDrive));
62
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi A8'), $rearDrive));
63
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q3'), $fullDrive));
64
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q5'), $fullDrive));
65
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Audi Q7'), $fullDrive));
66
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 1-series'), $rearDrive));
67
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 3-series'), $rearDrive));
68
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 5-series'), $rearDrive));
69
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW 7-series'), $rearDrive));
70
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X1'), $fullDrive));
71
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X3'), $fullDrive));
72
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X5'), $fullDrive));
73
            $this->entityManager->persist(new GH7708Car(new GH7708Model('BMW X6'), $fullDrive));
74
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Golf'), $frontDrive));
75
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Jetta'), $frontDrive));
76
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Passat'), $frontDrive));
77
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Polo'), $frontDrive));
78
            $this->entityManager->persist(new GH7708Car(new GH7708Model('Volkswagen Tiguan'), $fullDrive));
79
80
            $this->entityManager->flush();
81
        }
82
        return $this->entityManager;
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
83
    }
84
85
    protected function executeQuery(): array
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
86
    {
87
        return $this->getEntityManager()->getRepository(GH7708Car::class)->createQueryBuilder('ca')
88
            ->addSelect('mo')
89
            ->addSelect('dr')
90
            ->innerJoin('ca.model', 'mo')
91
            ->innerJoin('ca.drive', 'dr')
92
            ->getQuery()
93
            ->setMaxResults(20)
94
            ->useResultCache(true, 3600, 'result')
95
            ->setCacheable(true)
96
            ->getResult();
97
    }
98
99
    public function testSecondLevelCacheCartesianProduct() : void
100
    {
101
        // Executing the query for the first time to populate the cache
102
        $this->executeQuery();
103
104
        // Reseting the mock array
105
        GH7708ArrayCacheMock::$fetchRequests = [];
106
107
        // The second query execution for retreiving data from cache
108
        $result = $this->executeQuery();
109
110
        // I expect that cache requests count is less than query result (20 rows).
111
        $resultObjectsCount = count($result);
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
112
        $fetchRequestsCount = count(GH7708ArrayCacheMock::$fetchRequests);
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
113
        $this->assertLessThan($resultObjectsCount, $fetchRequestsCount);
114
    }
115
}
116
117
class GH7708ArrayCacheMock extends ArrayCache
118
{
119
    public static $fetchRequests = [];
120
121
    protected function doFetch($id)
122
    {
123
        self::$fetchRequests[] = [
124
            'id' => $id,
125
            'cacheDriverObject' => spl_object_hash($this)
0 ignored issues
show
introduced by
Function spl_object_hash() should not be referenced via a fallback global name, but via a use statement.
Loading history...
introduced by
Multi-line arrays must have a trailing comma after the last element.
Loading history...
126
        ];
127
        return parent::doFetch($id);
0 ignored issues
show
introduced by
Expected 1 lines before "return", found 0.
Loading history...
128
    }
129
}
130
131
/**
132
 * @ORM\Entity
133
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
134
 **/
135
class GH7708Car
136
{
137
    /**
138
     * @ORM\Id
139
     * @ORM\Column(type="integer")
140
     * @ORM\GeneratedValue
141
     **/
142
    protected $id;
143
144
    /**
145
     * @ORM\ManyToOne(targetEntity=GH7708Model::class, cascade={"persist"})
146
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
147
     **/
148
    protected $model;
149
150
    /**
151
     * @ORM\ManyToOne(targetEntity=GH7708Drive::class, cascade={"persist"})
152
     * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
153
     **/
154
    protected $drive;
155
156
    public function __construct(GH7708Model $model, GH7708Drive $drive)
157
    {
158
        $this->model = $model;
159
        $this->drive = $drive;
160
    }
161
}
162
163
/**
164
 * @ORM\Entity
165
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
166
 **/
167
class GH7708Model
168
{
169
    /**
170
     * @ORM\Id
171
     * @ORM\Column(type="integer")
172
     * @ORM\GeneratedValue
173
     **/
174
    protected $id;
175
176
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \Doctrine\Tests\ORM\Functional\Ticket\GH7708Model::$title with single line content, use one-line comment instead.
Loading history...
177
     * @ORM\Column(type="string", length=64)
178
     **/
179
    protected $title;
180
181
    public function __construct(string $title)
182
    {
183
        $this->title = $title;
184
    }
185
}
186
187
/**
188
 * @ORM\Entity
189
 * @ORM\Cache(usage="NONSTRICT_READ_WRITE")
190
 **/
191
class GH7708Drive
192
{
193
    /**
194
     * @ORM\Id
195
     * @ORM\Column(type="integer")
196
     * @ORM\GeneratedValue
197
     **/
198
    protected $id;
199
200
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \Doctrine\Tests\ORM\Functional\Ticket\GH7708Drive::$title with single line content, use one-line comment instead.
Loading history...
201
     * @ORM\Column(type="string", length=64)
202
     **/
203
    protected $title;
204
205
    public function __construct(string $title)
206
    {
207
        $this->title = $title;
208
    }
209
}
210