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

DDC6470Source::setTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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
		try {
25
			$this->setUpEntitySchema([
26
				DDC6470Source::class,
27
				DDC6470Target::class,
28
			]);
29
		} catch (SchemaException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
30
		}
31
	}
32
33
	/**
34
	 * @throws \Doctrine\Common\Persistence\Mapping\MappingException
35
	 * @throws \Doctrine\ORM\ORMException
36
	 * @throws \Doctrine\ORM\OptimisticLockException
37
	 */
38
	public function testOneToOne()
39
	{
40
		$source = new DDC6470Source();
41
		$target = new DDC6470Target();
42
		$source->setTarget($target);
43
44
		$this->_em->persist($source);
45
		$this->_em->flush();
46
		$this->_em->clear();
47
48
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Source::class, ['id' => $source->getId()]));
49
		$this->assertTrue($this->_em->getCache()->containsEntity(DDC6470Target::class, ['id' => $target->getId()]));
50
51
		$queryCount = $this->getCurrentQueryCount();
52
53
		/** @var EntityRepository $er */
54
		$er = $this->_em->getRepository(DDC6470Source::class);
55
		$qb = $er->createQueryBuilder("n");
56
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region = time());
57
58
		$result = $qb->getQuery()->getResult();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
59
60
		$newQueryCount = $this->getCurrentQueryCount();
61
		$this->assertEquals($queryCount + 1, $newQueryCount, "One for query only. One more appears here @see UnitOfWork:2654.");
62
63
		$this->_em->clear();
64
65
		/** @var EntityRepository $er */
66
		$er = $this->_em->getRepository(DDC6470Source::class);
67
		$qb = $er->createQueryBuilder("n");
68
		$qb->setCacheable(true)->setLifetime(3 * 60)->setCacheRegion($region);
69
		$qb->getQuery()->getResult();
70
		$this->assertEquals($newQueryCount, $this->getCurrentQueryCount(), "Assert everything get from cache");
71
	}
72
}
73
74
/**
75
 * @Entity()
76
 * @Cache(usage="NONSTRICT_READ_WRITE")
77
 */
78
class DDC6470Target
79
{
80
	/**
81
	 * @Id
82
	 * @GeneratedValue()
83
	 * @Column(type="integer")
84
	 * @var int
85
	 */
86
	protected $id;
87
	/**
88
	 * @Cache("NONSTRICT_READ_WRITE")
89
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Source", inversedBy="target")
90
	 * @var DDC6470Source
91
	 */
92
	protected $source;
93
94
	/**
95
	 * @return int
96
	 */
97
	public function getId(): ?int
98
	{
99
		return $this->id;
100
	}
101
102
	/**
103
	 * @param int $id
104
	 * @return DDC6470Target
105
	 */
106
	public function setId(?int $id): DDC6470Target
107
	{
108
		$this->id = $id;
109
110
		return $this;
111
	}
112
113
	/**
114
	 * @return DDC6470Source
115
	 */
116
	public function getSource(): ?DDC6470Source
117
	{
118
		return $this->source;
119
	}
120
121
	/**
122
	 * @param DDC6470Source $source
123
	 * @return DDC6470Target
124
	 */
125
	public function setSource(?DDC6470Source $source): DDC6470Target
126
	{
127
		$this->source = $source;
128
129
		return $this;
130
	}
131
132
}
133
134
/**
135
 * @Entity
136
 * @Cache(usage="NONSTRICT_READ_WRITE")
137
 */
138
class DDC6470Source
139
{
140
	/**
141
	 * @Id
142
	 * @GeneratedValue()
143
	 * @Column(type="integer")
144
	 * @var int
145
	 */
146
	protected $id;
147
	/**
148
	 * @Cache("NONSTRICT_READ_WRITE")
149
	 * @OneToOne(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\DDC6470Target", mappedBy="source", cascade={"persist"})
150
	 * @var DDC6470Target
151
	 */
152
	protected $target;
153
154
	/**
155
	 * @return int
156
	 */
157
	public function getId(): ?int
158
	{
159
		return $this->id;
160
	}
161
162
	/**
163
	 * @param int $id
164
	 * @return DDC6470Source
165
	 */
166
	public function setId(?int $id): DDC6470Source
167
	{
168
		$this->id = $id;
169
170
		return $this;
171
	}
172
173
	/**
174
	 * @return DDC6470Target
175
	 */
176
	public function getTarget(): ?DDC6470Target
177
	{
178
		return $this->target;
179
	}
180
181
	/**
182
	 * @param DDC6470Target $target
183
	 * @return DDC6470Source
184
	 */
185
	public function setTarget(?DDC6470Target $target): DDC6470Source
186
	{
187
		$this->target = $target;
188
189
		return $this;
190
	}
191
192
}
193