Completed
Pull Request — 2.6 (#7861)
by Ferran
08:28
created

GH7761Test::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Tests\OrmFunctionalTestCase;
9
10
final class GH7761Test extends OrmFunctionalTestCase
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15
    protected function setUp() : void
16
    {
17
        parent::setUp();
18
19
        $this->setUpEntitySchema([
20
            GH7761Entity::class,
21
            GH7761ChildEntity::class,
22
        ]);
23
24
        $parent = new GH7761Entity();
25
        $child  = new GH7761ChildEntity();
26
        $parent->children->add($child);
27
28
        $this->_em->persist($parent);
29
        $this->_em->persist($child);
30
        $this->_em->flush();
31
        $this->_em->clear();
32
    }
33
34
    protected function tearDown()
35
    {
36
        $this->_em->clear();
37
        parent::tearDown();
38
    }
39
40
    public function testCollectionClearDoesNotClearIfNotPersisted() : void
41
    {
42
        /** @var GH7761Entity $entity */
43
        $entity = $this->_em->find(GH7761Entity::class, 1);
44
        $entity->children->clear();
45
        $this->_em->persist(new GH7761Entity());
46
        $this->_em->flush();
47
48
        $this->_em->clear();
49
50
        $entity = $this->_em->find(GH7761Entity::class, 1);
51
        self::assertCount(1, $entity->children);
52
    }
53
54
    public function testCollectionClearDoesClearIfPersisted() : void
55
    {
56
        /** @var GH7761Entity $entity */
57
        $entity = $this->_em->find(GH7761Entity::class, 1);
58
        $entity->children->clear();
59
        $this->_em->persist($entity);
60
        $this->_em->flush();
61
62
        $this->_em->clear();
63
64
        $entity = $this->_em->find(GH7761Entity::class, 1);
65
        self::assertCount(0, $entity->children);
66
    }
67
}
68
69
/**
70
 * @Entity
71
 * @ChangeTrackingPolicy("DEFERRED_EXPLICIT")
72
 */
73
class GH7761Entity
74
{
75
    /**
76
     * @Id
77
     * @Column(type="integer")
78
     * @GeneratedValue
79
     */
80
    public $id;
81
82
    /**
83
     * @ManyToMany(targetEntity="Doctrine\Tests\ORM\Functional\Ticket\GH7761ChildEntity", cascade={"all"})
84
     * @JoinTable(name="gh7761_to_child",
85
     *     joinColumns={@JoinColumn(name="entity_id")},
86
     *     inverseJoinColumns={@JoinColumn(name="child_id")}
87
     * )
88
     */
89
    public $children;
90
91
    public function __construct()
92
    {
93
        $this->children = new ArrayCollection();
94
    }
95
}
96
97
/**
98
 * @Entity
99
 * @ChangeTrackingPolicy("DEFERRED_EXPLICIT")
100
 */
101
class GH7761ChildEntity
102
{
103
    /**
104
     * @Id
105
     * @Column(type="integer")
106
     * @GeneratedValue
107
     */
108
    public $id;
109
}
110