Completed
Pull Request — master (#5823)
by Mikhail
13:41
created

DDC2660Test   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 29 3
A testIssueWithExtraColumn() 0 17 2
A testIssueWithoutExtraColumn() 0 17 2
1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\ORM\Query\ResultSetMappingBuilder;
6
7
/**
8
 * @group
9
 */
10
class DDC2660Test extends \Doctrine\Tests\OrmFunctionalTestCase
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
19
        try {
20
            $this->_schemaTool->createSchema(array(
21
                $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660Product'),
22
                $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660Customer'),
23
                $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder')
24
            ));
25
        } catch(\Exception $e) {
26
            return;
27
        }
28
29
        for ($i = 0; $i < 5; $i++) {
30
            $product = new DDC2660Product();
31
            $customer = new DDC2660Customer();
32
            $order = new DDC2660CustomerOrder($product, $customer, 'name' . $i);
33
34
            $this->_em->persist($product);
35
            $this->_em->persist($customer);
36
            $this->_em->flush();
37
38
            $this->_em->persist($order);
39
            $this->_em->flush();
40
        }
41
42
        $this->_em->clear();
43
    }
44
45
    public function testIssueWithExtraColumn()
46
    {
47
        $sql = "SELECT o.product_id, o.customer_id, o.name FROM ddc_2660_customer_order o";
48
49
        $rsm = new ResultSetMappingBuilder($this->_getEntityManager());
50
        $rsm->addRootEntityFromClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder', 'c');
51
52
        $query  = $this->_em->createNativeQuery($sql, $rsm);
53
        $result = $query->getResult();
54
55
        $this->assertCount(5, $result);
56
57
        foreach ($result as $order) {
58
            $this->assertNotNull($order);
59
            $this->assertInstanceOf(__NAMESPACE__ . '\\DDC2660CustomerOrder', $order);
60
        }
61
    }
62
63
    public function testIssueWithoutExtraColumn()
64
    {
65
        $sql = "SELECT o.product_id, o.customer_id FROM ddc_2660_customer_order o";
66
67
        $rsm = new ResultSetMappingBuilder($this->_getEntityManager());
68
        $rsm->addRootEntityFromClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder', 'c');
69
70
        $query  = $this->_em->createNativeQuery($sql, $rsm);
71
        $result = $query->getResult();
72
73
        $this->assertCount(5, $result);
74
75
        foreach ($result as $order) {
76
            $this->assertNotNull($order);
77
            $this->assertInstanceOf(__NAMESPACE__ . '\\DDC2660CustomerOrder', $order);
78
        }
79
    }
80
}
81
/**
82
 * @Entity @Table(name="ddc_2660_product")
83
 */
84
class DDC2660Product
85
{
86
    /** @Id @Column(type="integer") @GeneratedValue */
87
    public $id;
88
}
89
90
/** @Entity  @Table(name="ddc_2660_customer") */
91
class DDC2660Customer
92
{
93
    /** @Id @Column(type="integer") @GeneratedValue */
94
    public $id;
95
}
96
97
/** @Entity @Table(name="ddc_2660_customer_order") */
98
class DDC2660CustomerOrder
99
{
100
    /**
101
     * @Id @ManyToOne(targetEntity="DDC2660Product")
102
     */
103
    public $product;
104
105
    /**
106
     * @Id @ManyToOne(targetEntity="DDC2660Customer")
107
     */
108
    public $customer;
109
110
    /**
111
     * @Column(type="string")
112
     */
113
    public $name;
114
115
    public function __construct(DDC2660Product $product, DDC2660Customer $customer, $name)
116
    {
117
        $this->product  = $product;
118
        $this->customer = $customer;
119
        $this->name = $name;
120
    }
121
}
122