Failed Conditions
Pull Request — master (#6838)
by Luís
10:17
created

GH6776Test::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Tests\OrmFunctionalTestCase;
7
8
/**
9
 * @group 6776
10
 */
11
class GH6776Test extends OrmFunctionalTestCase
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function setUp() : void
17
    {
18
        parent::setUp();
19
20
        $this->setUpEntitySchema([GH6776WheelDimension::class, GH6776Vehicle::class]);
21
    }
22
23
    /**
24
     * Verifies that removing and then inserting an element in a collection using a unique constraint does not
25
     * make this constraint fail.
26
     */
27
    public function testIssue() : void
28
    {
29
        // create some vehicle with dimensions
30
        $vehicle       = new GH6776Vehicle();
31
        $vehicle->name = 'SuperCar';
32
33
        $dimension1               = new GH6776WheelDimension();
34
        $dimension1->width        = 6;
35
        $dimension1->diameter     = 17;
36
        $dimension1->offset       = 32;
37
        $dimension1->tirePressure = 2.75;
38
        $dimension1->vehicle      = $vehicle;
39
40
        $dimension1bis               = clone $dimension1;
41
        $dimension1bis->tirePressure = 3.10;
42
43
        $dimension2               = new GH6776WheelDimension();
44
        $dimension2->type         = 'rear';
45
        $dimension2->width        = 6;
46
        $dimension2->diameter     = 17;
47
        $dimension2->offset       = 39;
48
        $dimension2->tirePressure = 2.75;
49
        $dimension2->vehicle      = $vehicle;
50
51
        $vehicle->compatibleDimensions = new ArrayCollection();
52
        $vehicle->compatibleDimensions->add($dimension1);
53
        $vehicle->compatibleDimensions->add($dimension2);
54
55
        // persist and flush vehicle and the 2 original dimensions
56
        $this->_em->persist($vehicle);
57
        $this->_em->persist($dimension1);
58
        $this->_em->persist($dimension2);
59
        $this->_em->flush();
60
61
        self::assertCount(2, $vehicle->compatibleDimensions);
62
63
        // remove dimension1 and add its clone; when flushing it should crash because of unique constraint violation
64
        $vehicle->compatibleDimensions->removeElement($dimension1);
0 ignored issues
show
Bug introduced by
The method removeElement() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Doctrine\Common\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $vehicle->compatibleDimensions->/** @scrutinizer ignore-call */ 
65
                                        removeElement($dimension1);
Loading history...
65
        $this->_em->remove($dimension1);
66
67
        self::assertCount(1, $vehicle->compatibleDimensions);
68
69
        $this->_em->persist($dimension1bis);
70
        $vehicle->compatibleDimensions->add($dimension1bis);
0 ignored issues
show
Bug introduced by
The method add() does not exist on Countable. It seems like you code against a sub-type of Countable such as SplDoublyLinkedList or PharIo\Manifest\AuthorCollection or Doctrine\Common\Collections\Collection or PharIo\Manifest\BundledComponentCollection or Doctrine\DBAL\Query\Expression\CompositeExpression or PharIo\Manifest\RequirementCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $vehicle->compatibleDimensions->/** @scrutinizer ignore-call */ 
71
                                        add($dimension1bis);
Loading history...
Bug introduced by
The method add() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as PharIo\Manifest\AuthorCollection or Doctrine\Common\Collections\Collection or PharIo\Manifest\BundledComponentCollection or PharIo\Manifest\RequirementCollection or SplDoublyLinkedList. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $vehicle->compatibleDimensions->/** @scrutinizer ignore-call */ 
71
                                        add($dimension1bis);
Loading history...
71
72
        $this->_em->flush();
73
74
        // still the same count
75
        self::assertCount(2, $vehicle->compatibleDimensions);
76
    }
77
}
78
79
/**
80
 * @Entity
81
 */
82
class GH6776Vehicle
83
{
84
    /** @Id @Column(type="integer") @GeneratedValue */
85
    public $id;
86
87
    /** @Column(type="string") */
88
    public $name;
89
90
    /** @oneToMany(targetEntity="GH6776WheelDimension", mappedBy="vehicle") */
91
    public $compatibleDimensions;
92
}
93
94
/**
95
 * @Entity
96
 * @Table(uniqueConstraints={@UniqueConstraint(name="wheel_size", columns={"offset", "width", "diameter"})})
97
 */
98
class GH6776WheelDimension
99
{
100
    /** @Id @Column(type="integer") @GeneratedValue */
101
    public $id;
102
103
    /** @ManyToOne(targetEntity="GH6776Vehicle", inversedBy="compatibleDimensions") */
104
    public $vehicle;
105
106
    /** @Column(type="integer") */
107
    public $offset;
108
109
    /** @Column(type="integer") */
110
    public $width;
111
112
    /** @Column(type="integer") */
113
    public $diameter;
114
115
    /** @Column(type="float") */
116
    public $tirePressure;
117
118
    /** @Column(type="string") */
119
    public $type = 'front';
120
}
121