1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
6
|
|
|
use Doctrine\DBAL\Driver\Connection; |
7
|
|
|
use Doctrine\ORM\Mapping\Driver\XmlDriver; |
8
|
|
|
use Doctrine\Tests\Models\OrnementalOrphanRemoval\PhoneNumber; |
9
|
|
|
use Doctrine\Tests\Models\OrnementalOrphanRemoval\Person; |
10
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests a unidirectional many-to-one association mapping with orphan removal. |
14
|
|
|
*/ |
15
|
|
|
class ManyToOneOrphanRemovalTest extends OrmFunctionalTestCase |
16
|
|
|
{ |
17
|
|
|
private $personId; |
18
|
|
|
|
19
|
|
|
protected static $_modelSets = [ |
20
|
|
|
'ornemental_orphan_removal' => [ |
21
|
|
|
Person::class, |
22
|
|
|
PhoneNumber::class, |
23
|
|
|
] |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
View Code Duplication |
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->useModelSet('ornemental_orphan_removal'); |
29
|
|
|
|
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$person = new Person; |
33
|
|
|
$person->id = 'ca41a293-799f-4d68-bf79-626c3ad223ec'; |
34
|
|
|
|
35
|
|
|
$phone1 = new PhoneNumber; |
36
|
|
|
$phone1->id = 'f4132478-c492-4dfe-aab5-a5b79ae129e7'; |
37
|
|
|
$phone1->phonenumber = '123456'; |
38
|
|
|
|
39
|
|
|
$phone2 = new PhoneNumber; |
40
|
|
|
$phone2->id = '7faa4cd3-a155-4fbf-bc42-aa4269a4454d'; |
41
|
|
|
$phone2->phonenumber = '234567'; |
42
|
|
|
|
43
|
|
|
$phone1->person = $person; |
44
|
|
|
$phone2->person = $person; |
45
|
|
|
|
46
|
|
|
$this->_em->persist($phone1); |
47
|
|
|
$this->_em->persist($phone2); |
48
|
|
|
$this->_em->persist($person); |
49
|
|
|
$this->_em->flush(); |
50
|
|
|
|
51
|
|
|
$this->personId = $person->id; |
52
|
|
|
$this->_em->clear(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testOrphanRemovalIsPurelyOrnemental() |
56
|
|
|
{ |
57
|
|
|
$person = $this->_em->getReference(Person::class, $this->personId); |
58
|
|
|
|
59
|
|
|
$this->_em->remove($person); |
|
|
|
|
60
|
|
|
$this->_em->flush(); |
61
|
|
|
$this->_em->clear(); |
62
|
|
|
|
63
|
|
|
$query = $this->_em->createQuery( |
64
|
|
|
'SELECT u FROM Doctrine\Tests\Models\OrnementalOrphanRemoval\Person u' |
65
|
|
|
); |
66
|
|
|
$result = $query->getResult(); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(0, count($result), 'Person should be removed by EntityManager'); |
69
|
|
|
|
70
|
|
|
$query = $this->_em->createQuery( |
71
|
|
|
'SELECT p FROM Doctrine\Tests\Models\OrnementalOrphanRemoval\PhoneNumber p' |
72
|
|
|
); |
73
|
|
|
$result = $query->getResult(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals(2, count($result), 'Orphan removal should not kick in'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function _getEntityManager( |
79
|
|
|
Connection $connection = null, |
80
|
|
|
MappingDriver $mappingDriver = null |
81
|
|
|
) { |
82
|
|
|
return parent::_getEntityManager($connection, new XmlDriver( |
83
|
|
|
__DIR__.DIRECTORY_SEPARATOR.'xml' |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.