Failed Conditions
Pull Request — 2.6 (#7149)
by
unknown
08:01
created

DDC6470Source::getTarget2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Doctrine\Tests\ORM\Functional\Ticket;
5
6
use Doctrine\DBAL\Schema\SchemaException;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Mapping\Cache;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\Tests\ORM\Functional\Ticket\Entity. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\OneToOne;
14
15
/**
16
 */
17
class DDC6470Test extends \Doctrine\Tests\OrmFunctionalTestCase
18
{
19
	public function setUp()
20
	{
21
		$this->enableSecondLevelCache();
22
		parent::setUp();
23
24
		$this->setUpEntitySchema([
25
			DDC6470Source::class,
26
			DDC6470Target1::class,
27
			DDC6470Target2::class,
28
			DDC6470SourceReverse::class,
29
			DDC6470Target2Reverse::class,
30
			DDC6470Target1Reverse::class,
31
		]);
32
	}
33
34
	/**
35
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
36
	 * @throws \Doctrine\ORM\ORMException
37
	 * @throws \Doctrine\ORM\OptimisticLockException
38
	 */
39
	public function testOneEntity()
40
	{
41
		$source = new DDC6470Source();
42
		$target = new DDC6470Target1();
43
		$source->setTarget1($target);
44
45
		$this->_em->persist($source);
46
		$this->_em->flush();
47
		$this->_em->clear();
48
49
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Source::class, ['id' => $source->getId()]));
50
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target1::class, ['id' => $target->getId()]));
51
52
		$queryCount = $this->getCurrentQueryCount();
53
54
		/** @var EntityRepository $er */
55
		$er = $this->_em->getRepository(DDC6470Source::class);
56
		$qb = $er->createQueryBuilder("n");
57
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region = time());
58
59
		$qb->getQuery()->getResult();
60
61
		$newQueryCount = $this->getCurrentQueryCount();
62
		$this->assertEquals($queryCount + 1, $newQueryCount, "+1 for query only. One more appears here @see UnitOfWork:2654. Acutally, it's not valid behaviour.");
63
64
		$this->_em->clear();
65
66
		/** @var EntityRepository $er */
67
		$er = $this->_em->getRepository(DDC6470Source::class);
68
		$qb = $er->createQueryBuilder("n");
69
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region);
70
		$qb->getQuery()->getResult();
71
		$this->assertEquals($newQueryCount, $this->getCurrentQueryCount(), "Assert everything get from cache. Because of prev assertion, check for sure");
72
	}
73
74
	/**
75
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
76
	 * @throws \Doctrine\ORM\ORMException
77
	 * @throws \Doctrine\ORM\OptimisticLockException
78
	 */
79
	public function testMultipleEntities()
80
	{
81
82
		$source1 = new DDC6470Source();
83
		$target1 = new DDC6470Target1();
84
		$source1->setTarget1($target1);
85
86
		$source2 = new DDC6470Source();
87
		$target2 = new DDC6470Target2();
88
		$source2->setTarget2($target2);
89
90
		$this->_em->persist($source1);
91
		$this->_em->persist($source2);
92
		$this->_em->flush();
93
		$this->_em->clear();
94
95
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Source::class, ['id' => $source1->getId()]));
96
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Source::class, ['id' => $source2->getId()]));
97
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target1::class, ['id' => $target1->getId()]));
98
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target2::class, ['id' => $target2->getId()]));
99
100
		$queryCount = $this->getCurrentQueryCount();
101
102
		/** @var EntityRepository $er */
103
		$er = $this->_em->getRepository(DDC6470Source::class);
104
		$qb = $er->createQueryBuilder("n");
105
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region = time());
106
107
		$qb->getQuery()->getResult();
108
109
		$newQueryCount = $this->getCurrentQueryCount();
110
		$this->assertEquals($queryCount + 1, $newQueryCount, "+1 for query only. One more appears here @see UnitOfWork:2654.");
111
112
		$this->_em->clear();
113
114
		/** @var EntityRepository $er */
115
		$er = $this->_em->getRepository(DDC6470Source::class);
116
		$qb = $er->createQueryBuilder("n");
117
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region);
118
		$qb->getQuery()->getResult();
119
		$this->assertEquals($newQueryCount, $this->getCurrentQueryCount(), "Assert everything get from cache. 
120
		This is the main problem. 
121
		The data hydrated in DefaultQueryCache::get() is not put into cache properly, some oneToOne relations are missed");
122
	}
123
124
	/**
125
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
126
	 * @throws \Doctrine\ORM\ORMException
127
	 * @throws \Doctrine\ORM\OptimisticLockException
128
	 */
129
	public function testMultipleEntitiesReverse()
130
	{
131
132
		$target1 = new DDC6470Target1Reverse();
133
		$source1 = new DDC6470SourceReverse();
134
		$target1->setSource($source1);
135
136
		$target2 = new DDC6470Target2Reverse();
137
		$source2 = new DDC6470SourceReverse();
138
		$target2->setSource($source2);
139
140
		$this->_em->persist($target1);
141
		$this->_em->persist($target2);
142
		$this->_em->flush();
143
		$this->_em->clear();
144
145
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470SourceReverse::class, ['id' => $source1->getId()]));
146
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470SourceReverse::class, ['id' => $source2->getId()]));
147
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target1Reverse::class, ['id' => $target1->getId()]));
148
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target2Reverse::class, ['id' => $target2->getId()]));
149
150
		$queryCount = $this->getCurrentQueryCount();
151
152
		/** @var EntityRepository $er */
153
		$er = $this->_em->getRepository(DDC6470Target1Reverse::class);
154
		$qb = $er->createQueryBuilder("n");
155
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region1 = time());
156
		$qb->getQuery()->getResult();
157
158
		$newQueryCount = $this->getCurrentQueryCount();
159
		$this->assertEquals($queryCount + 1, $newQueryCount, "+1 for query only. One more appears here @see UnitOfWork:2654.");
160
		$this->_em->clear();
161
162
		$queryCount = $newQueryCount;
163
		/** @var EntityRepository $er */
164
		$er = $this->_em->getRepository(DDC6470Target2Reverse::class);
165
		$qb = $er->createQueryBuilder("n");
166
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region2 = time());
167
		$qb->getQuery()->getResult();
168
169
		$newQueryCount = $this->getCurrentQueryCount();
170
		$this->assertEquals($queryCount + 1, $newQueryCount, "+1 for query only. One more appears here @see UnitOfWork:2654.");
171
		$this->_em->clear();
172
173
		$queryCount = $newQueryCount;
174
		/** @var EntityRepository $er */
175
		$er = $this->_em->getRepository(DDC6470Target1Reverse::class);
176
		$qb = $er->createQueryBuilder("n");
177
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region1);
178
		$qb->getQuery()->getResult();
179
180
		$newQueryCount = $this->getCurrentQueryCount();
181
		$this->assertEquals($queryCount, $newQueryCount, "Assert everything get from cache.");
182
		$this->_em->clear();
183
184
		$queryCount = $newQueryCount;
185
		/** @var EntityRepository $er */
186
		$er = $this->_em->getRepository(DDC6470Target2Reverse::class);
187
		$qb = $er->createQueryBuilder("n");
188
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region2);
189
		$qb->getQuery()->getResult();
190
191
		$newQueryCount = $this->getCurrentQueryCount();
192
		$this->assertEquals($queryCount, $newQueryCount, "Assert everything get from cache.");
193
		$this->_em->clear();
194
	}
195
}
196
197
/**
198
 * @Entity
199
 * @Cache(usage="NONSTRICT_READ_WRITE")
200
 */
201
class DDC6470Source
202
{
203
	/**
204
	 * @Id
205
	 * @GeneratedValue()
206
	 * @Column(type="integer")
207
	 * @var int
208
	 */
209
	protected $id;
210
	/**
211
	 * @Cache("NONSTRICT_READ_WRITE")
212
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Target1", mappedBy="source", cascade={"persist"})
213
	 * @var DDC6470Target1
214
	 */
215
	protected $target1;
216
	/**
217
	 * @Cache("NONSTRICT_READ_WRITE")
218
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Target2", mappedBy="source", cascade={"persist"})
219
	 * @var DDC6470Target2
220
	 */
221
	protected $target2;
222
223
	/**
224
	 * @return int
225
	 */
226
	public function getId(): ?int
227
	{
228
		return $this->id;
229
	}
230
231
	/**
232
	 * @param int $id
233
	 * @return DDC6470Source
234
	 */
235
	public function setId(?int $id): DDC6470Source
236
	{
237
		$this->id = $id;
238
239
		return $this;
240
	}
241
242
	/**
243
	 * @return DDC6470Target1
244
	 */
245
	public function getTarget1(): ?DDC6470Target1
246
	{
247
		return $this->target1;
248
	}
249
250
	/**
251
	 * @param DDC6470Target1 $target
252
	 * @return DDC6470Source
253
	 */
254
	public function setTarget1(?DDC6470Target1 $target): DDC6470Source
255
	{
256
		$this->target1 = $target;
257
258
		return $this;
259
	}
260
261
	/**
262
	 * @return DDC6470Target2
263
	 */
264
	public function getTarget2(): ?DDC6470Target2
265
	{
266
		return $this->target2;
267
	}
268
269
	/**
270
	 * @param DDC6470Target2 $target2
271
	 * @return DDC6470Source
272
	 */
273
	public function setTarget2(?DDC6470Target2 $target2): DDC6470Source
274
	{
275
		$this->target2 = $target2;
276
277
		return $this;
278
	}
279
280
}
281
282
/**
283
 * @Entity()
284
 * @Cache(usage="NONSTRICT_READ_WRITE")
285
 */
286
class DDC6470Target1
287
{
288
	/**
289
	 * @Id
290
	 * @GeneratedValue()
291
	 * @Column(type="integer")
292
	 * @var int
293
	 */
294
	protected $id;
295
	/**
296
	 * @Cache("NONSTRICT_READ_WRITE")
297
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Source", inversedBy="target1")
298
	 * @var DDC6470Source
299
	 */
300
	protected $source;
301
302
	/**
303
	 * @return int
304
	 */
305
	public function getId(): ?int
306
	{
307
		return $this->id;
308
	}
309
310
	/**
311
	 * @param int $id
312
	 * @return DDC6470Target1
313
	 */
314
	public function setId(?int $id): DDC6470Target1
315
	{
316
		$this->id = $id;
317
318
		return $this;
319
	}
320
321
	/**
322
	 * @return DDC6470Source
323
	 */
324
	public function getSource(): ?DDC6470Source
325
	{
326
		return $this->source;
327
	}
328
329
	/**
330
	 * @param DDC6470Source $source
331
	 * @return DDC6470Target1
332
	 */
333
	public function setSource(?DDC6470Source $source): DDC6470Target1
334
	{
335
		$this->source = $source;
336
337
		return $this;
338
	}
339
340
}
341
342
/**
343
 * @Entity()
344
 * @Cache(usage="NONSTRICT_READ_WRITE")
345
 */
346
class DDC6470Target2
347
{
348
	/**
349
	 * @Id
350
	 * @GeneratedValue()
351
	 * @Column(type="integer")
352
	 * @var int
353
	 */
354
	protected $id;
355
	/**
356
	 * @Cache("NONSTRICT_READ_WRITE")
357
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Source", inversedBy="target2")
358
	 * @var DDC6470Source
359
	 */
360
	protected $source;
361
362
	/**
363
	 * @return int
364
	 */
365
	public function getId(): ?int
366
	{
367
		return $this->id;
368
	}
369
370
	/**
371
	 * @param int $id
372
	 * @return DDC6470Target2
373
	 */
374
	public function setId(?int $id): DDC6470Target2
375
	{
376
		$this->id = $id;
377
378
		return $this;
379
	}
380
381
	/**
382
	 * @return DDC6470Source
383
	 */
384
	public function getSource(): ?DDC6470Source
385
	{
386
		return $this->source;
387
	}
388
389
	/**
390
	 * @param DDC6470Source $source
391
	 * @return DDC6470Target2
392
	 */
393
	public function setSource(?DDC6470Source $source): DDC6470Target2
394
	{
395
		$this->source = $source;
396
397
		return $this;
398
	}
399
400
}
401
402
/**
403
 * @Entity
404
 * @Cache(usage="NONSTRICT_READ_WRITE")
405
 */
406
class DDC6470SourceReverse
407
{
408
	/**
409
	 * @Id
410
	 * @GeneratedValue()
411
	 * @Column(type="integer")
412
	 * @var int
413
	 */
414
	protected $id;
415
	/**
416
	 * @Cache("NONSTRICT_READ_WRITE")
417
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Target1Reverse", inversedBy="source")
418
	 * @var DDC6470Target1Reverse
419
	 */
420
	protected $target1;
421
	/**
422
	 * @Cache("NONSTRICT_READ_WRITE")
423
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Target2Reverse", inversedBy="source")
424
	 * @var DDC6470Target2Reverse
425
	 */
426
	protected $target2;
427
428
	/**
429
	 * @return int
430
	 */
431
	public function getId(): ?int
432
	{
433
		return $this->id;
434
	}
435
436
	/**
437
	 * @param int $id
438
	 * @return DDC6470SourceReverse
439
	 */
440
	public function setId(?int $id): DDC6470SourceReverse
441
	{
442
		$this->id = $id;
443
444
		return $this;
445
	}
446
447
	/**
448
	 * @return DDC6470Target1Reverse
449
	 */
450
	public function getTarget1(): ?DDC6470Target1Reverse
451
	{
452
		return $this->target1;
453
	}
454
455
	/**
456
	 * @param DDC6470Target1Reverse $target1
457
	 * @return DDC6470SourceReverse
458
	 */
459
	public function setTarget1(?DDC6470Target1Reverse $target1): DDC6470SourceReverse
460
	{
461
		$this->target1 = $target1;
462
463
		return $this;
464
	}
465
466
	/**
467
	 * @return DDC6470Target2Reverse
468
	 */
469
	public function getTarget2(): ?DDC6470Target2Reverse
470
	{
471
		return $this->target2;
472
	}
473
474
	/**
475
	 * @param DDC6470Target2Reverse $target2
476
	 * @return DDC6470SourceReverse
477
	 */
478
	public function setTarget2(?DDC6470Target2Reverse $target2): DDC6470SourceReverse
479
	{
480
		$this->target2 = $target2;
481
482
		return $this;
483
	}
484
485
}
486
487
/**
488
 * @Entity()
489
 * @Cache(usage="NONSTRICT_READ_WRITE")
490
 */
491
class DDC6470Target1Reverse
492
{
493
	/**
494
	 * @Id
495
	 * @GeneratedValue()
496
	 * @Column(type="integer")
497
	 * @var int
498
	 */
499
	protected $id;
500
	/**
501
	 * @Cache("NONSTRICT_READ_WRITE")
502
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470SourceReverse", mappedBy="target1", cascade={"persist"})
503
	 * @var DDC6470SourceReverse
504
	 */
505
	protected $source;
506
507
	/**
508
	 * @return int
509
	 */
510
	public function getId(): ?int
511
	{
512
		return $this->id;
513
	}
514
515
	/**
516
	 * @param int $id
517
	 * @return DDC6470Target1Reverse
518
	 */
519
	public function setId(?int $id): DDC6470Target1Reverse
520
	{
521
		$this->id = $id;
522
523
		return $this;
524
	}
525
526
	/**
527
	 * @return DDC6470SourceReverse
528
	 */
529
	public function getSource(): ?DDC6470SourceReverse
530
	{
531
		return $this->source;
532
	}
533
534
	/**
535
	 * @param DDC6470SourceReverse $source
536
	 * @return DDC6470Target1Reverse
537
	 */
538
	public function setSource(?DDC6470SourceReverse $source): DDC6470Target1Reverse
539
	{
540
		$this->source = $source;
541
542
		return $this;
543
	}
544
545
}
546
547
/**
548
 * @Entity()
549
 * @Cache(usage="NONSTRICT_READ_WRITE")
550
 */
551
class DDC6470Target2Reverse
552
{
553
	/**
554
	 * @Id
555
	 * @GeneratedValue()
556
	 * @Column(type="integer")
557
	 * @var int
558
	 */
559
	protected $id;
560
	/**
561
	 * @Cache("NONSTRICT_READ_WRITE")
562
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470SourceReverse", mappedBy="target2", cascade={"persist"})
563
	 * @var DDC6470SourceReverse
564
	 */
565
	protected $source;
566
567
	/**
568
	 * @return int
569
	 */
570
	public function getId(): ?int
571
	{
572
		return $this->id;
573
	}
574
575
	/**
576
	 * @param int $id
577
	 * @return DDC6470Target2Reverse
578
	 */
579
	public function setId(?int $id): DDC6470Target2Reverse
580
	{
581
		$this->id = $id;
582
583
		return $this;
584
	}
585
586
	/**
587
	 * @return DDC6470SourceReverse
588
	 */
589
	public function getSource(): ?DDC6470SourceReverse
590
	{
591
		return $this->source;
592
	}
593
594
	/**
595
	 * @param DDC6470SourceReverse $source
596
	 * @return DDC6470Target2Reverse
597
	 */
598
	public function setSource(?DDC6470SourceReverse $source): DDC6470Target2Reverse
599
	{
600
		$this->source = $source;
601
602
		return $this;
603
	}
604
}
605