Failed Conditions
Pull Request — master (#6772)
by Grégoire
18:54
created

testOrphanRemovalIsPurelyOrnemental()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
c 0
b 0
f 0
rs 9.2
cc 1
eloc 13
nc 1
nop 0
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';
0 ignored issues
show
Bug introduced by
The property phonenumber does not seem to exist in Doctrine\Tests\Models\Or...phanRemoval\PhoneNumber.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $person defined by $this->_em->getReference...class, $this->personId) on line 57 can also be of type null; however, Doctrine\ORM\EntityManager::remove() does only seem to accept object, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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