Completed
Pull Request — master (#6155)
by Guido
10:43
created

DDC3448Test   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
B testOrderedEagerAssociationShouldBeOrdered() 0 26 3
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Tests\OrmFunctionalTestCase;
6
7
class DDC3448Test extends OrmFunctionalTestCase
8
{
9
    protected function setUp()
10
    {
11
        parent::setUp();
12
13
        $this->_schemaTool->createSchema(array(
14
            $this->_em->getClassMetadata(DDC3448Client::class),
15
            $this->_em->getClassMetadata(DDC3448Target::class),
16
        ));
17
    }
18
19
    public function testOrderedEagerAssociationShouldBeOrdered()
20
    {
21
        $client = new DDC3448Client();
22
        $this->_em->persist($client);
23
24
        $positions = range(0, 99);
25
        shuffle($positions);
26
27
        foreach ($positions as $pos) {
28
            $target = new DDC3448Target($client, $pos);
29
            $this->_em->persist($target);
30
        }
31
32
        $this->_em->flush();
33
        $id = $client->id;
34
35
        $this->_em->clear();
36
37
        $dbClient = $this->_em->find(DDC3448Client::class, $id);
38
39
        $initial = -1;
40
        foreach ($dbClient->targets as $target) {
41
            $this->assertGreaterThan($initial, $target->position);
42
            $initial = $target->position;
43
        }
44
    }
45
}
46
47
/**
48
 * @Entity
49
 * @Table(name="ddc3448_clients")
50
 */
51
class DDC3448Client
52
{
53
    /**
54
     * @Id
55
     * @Column(name="id", type="integer")
56
     * @GeneratedValue(strategy="AUTO")
57
     */
58
    public $id;
59
60
    /**
61
     * @OneToMany(targetEntity="DDC3448Target", mappedBy="client", fetch="EAGER")
62
     * @OrderBy({"position" = "ASC"})
63
64
     *
65
     * @var DDC3448Target[]
66
     */
67
    public $targets;
68
}
69
70
/**
71
 * @Entity
72
 * @Table(name="ddc3448_targets")
73
 */
74
class DDC3448Target
75
{
76
    /**
77
     * @Id
78
     * @Column(name="id", type="integer")
79
     * @GeneratedValue(strategy="AUTO")
80
     */
81
    private $id;
82
83
    /**
84
     * @Column(name="position", type="integer")
85
     * @var int
86
     */
87
    public $position;
88
89
    /**
90
     * @ManyToOne(targetEntity="DDC3448Client", inversedBy="targets")
91
     *
92
     * @var DDC3448Client
93
     */
94
    private $client;
95
96
    /**
97
     * @param DDC3448Client $client
98
     * @param int $position
99
     */
100
    public function __construct(DDC3448Client $client, $position)
101
    {
102
        $this->position = $position;
103
        $this->client = $client;
104
    }
105
}
106