Failed Conditions
Push — master ( 3ca65e...6e095f )
by Luís
20s
created

GH6029Test   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 51.47 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 35
loc 68
rs 10
c 1
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testManyToManyAssociation() 17 17 1
A setUp() 0 11 1
A testOneToManyAssociation() 17 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\ORMInvalidArgumentException;
7
use Doctrine\Tests\OrmFunctionalTestCase;
8
9
final class GH6029Test extends OrmFunctionalTestCase
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14
    protected function setUp()
15
    {
16
        parent::setUp();
17
18
        $this->setUpEntitySchema(
19
            [
20
                GH6029User::class,
21
                GH6029Group::class,
22
                GH6029Group2::class,
23
                GH6029Product::class,
24
                GH6029Feature::class,
25
            ]
26
        );
27
    }
28
29
    /**
30
     * Verifies that when wrong entity is persisted via relationship field, the error message does not correctly state
31
     * the expected class name.
32
     *
33
     * @group 6029
34
     */
35 View Code Duplication
    public function testManyToManyAssociation() : void
36
    {
37
        $user = new GH6029User();
38
        $user->groups->add(new GH6029Group2());
39
40
        $this->expectException(ORMInvalidArgumentException::class);
41
        $this->expectExceptionMessage(
42
            sprintf(
43
                'Expected value of type "%s" for association field "%s#$groups", got "%s" instead.',
44
                GH6029Group::class,
45
                GH6029User::class,
46
                GH6029Group2::class
47
            )
48
        );
49
50
        $this->_em->persist($user);
51
        $this->_em->flush();
52
    }
53
54
    /**
55
     * Verifies that when wrong entity is persisted via relationship field, the error message does not correctly state
56
     * the expected class name.
57
     *
58
     * @group 6029
59
     */
60 View Code Duplication
    public function testOneToManyAssociation() : void
61
    {
62
        $product = new GH6029Product();
63
        $product->features->add(new GH6029Group2());
64
65
        $this->expectException(ORMInvalidArgumentException::class);
66
        $this->expectExceptionMessage(
67
            sprintf(
68
                'Expected value of type "%s" for association field "%s#$features", got "%s" instead.',
69
                GH6029Feature::class,
70
                GH6029Product::class,
71
                GH6029Group2::class
72
            )
73
        );
74
75
        $this->_em->persist($product);
76
        $this->_em->flush();
77
    }
78
}
79
80
/** @Entity */
81
class GH6029User
82
{
83
    /** @Id @Column(type="integer") @GeneratedValue */
84
    public $id;
85
86
    /** @ManyToMany(targetEntity=GH6029Group::class, cascade={"all"}) */
87
    public $groups;
88
89
    public function __construct()
90
    {
91
        $this->groups = new ArrayCollection();
92
    }
93
}
94
95
/** @Entity */
96
class GH6029Group
97
{
98
    /** @Id @Column(type="integer") @GeneratedValue */
99
    public $id;
100
}
101
102
/** @Entity */
103
class GH6029Group2
104
{
105
    /** @Id @Column(type="integer") @GeneratedValue */
106
    public $id;
107
}
108
109
/** @Entity */
110
class GH6029Product
111
{
112
    /** @Id @Column(type="integer") @GeneratedValue */
113
    public $id;
114
115
    /**
116
     * @OneToMany(targetEntity=GH6029Feature::class, mappedBy="product", cascade={"all"})
117
     */
118
    public $features;
119
120
    public function __construct()
121
    {
122
        $this->features = new ArrayCollection();
123
    }
124
}
125
126
/** @Entity */
127
class GH6029Feature
128
{
129
    /** @Id @Column(type="integer") @GeneratedValue */
130
    public $id;
131
132
    /**
133
     * @ManyToOne(targetEntity=GH6029Product::class, inversedBy="features")
134
     * @JoinColumn(name="product_id", referencedColumnName="id")
135
     */
136
    public $product;
137
}
138