Failed Conditions
Pull Request — master (#6735)
by Matthias
10:10
created

GH6443Test   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testIssueWithProxyClass() 0 21 1
A testIssue() 0 21 1
A testIssueWithCompositeIdentifier() 0 16 1
A setUp() 0 13 1
A tearDown() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\DBAL\Logging\DebugStack;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\ORM\Annotation as ORM;
10
use Doctrine\ORM\ORMInvalidArgumentException;
11
use Doctrine\Tests\DbalTypes\Rot13Type;
12
use Doctrine\Tests\OrmFunctionalTestCase;
13
use function count;
14
15
/**
16
 * @group 6443
17
 */
18
class GH6443Test extends OrmFunctionalTestCase
19
{
20
    /** @var Rot13Type */
21
    private $rot13Type;
22
23
    /** @var DebugStack */
24
    private $sqlLogger;
25
26
    /**
27
     * when having an entity, that has a non scalar identifier, the type will not be guessed / converted correctly
28
     */
29
    public function testIssue() : void
30
    {
31
        $entity     = new GH6443Post();
32
        $entity->id = 'Foo';
33
34
        $dql   = 'SELECT p FROM ' . GH6443Post::class . ' p WHERE p = ?1';
35
        $query = $this->em->createQuery($dql);
36
37
        // we do not know that the internal type is a rot13, so we can not add the type parameter here
38
        $query->setParameter(1, $entity);
39
40
        // we do not need the result, but we need to execute it to log the SQL-Statement
41
        $query->getResult();
42
43
        $lastSql = $this->sqlLogger->queries[count($this->sqlLogger->queries)];
44
45
        // the entity's identifier is of type "rot13" so the query parameter needs to be this type too
46
        $this->assertSame(
47
            $this->rot13Type->getName(),
48
            $lastSql['types'][0],
49
            "asserting that the entity's identifier type is correctly inferred"
50
        );
51
    }
52
53
    /**
54
     * when having an entity, that has a non scalar identifier, the type will not be guessed / converted correctly
55
     */
56
    public function testIssueWithProxyClass()
57
    {
58
        $metadata    = $this->em->getClassMetadata(GH6443Post::class);
59
        $entityProxy = $this->em->getProxyFactory()->getProxy($metadata, ['id' => 'Foo']);
60
61
        $dql   = 'SELECT p FROM ' . GH6443Post::class . ' p WHERE p = ?1';
62
        $query = $this->em->createQuery($dql);
63
64
        // we do not know that the internal type is a rot13, so we can not add the type parameter here
65
        $query->setParameter(1, $entityProxy);
66
67
        // we do not need the result, but we need to execute it to log the SQL-Statement
68
        $query->getResult();
69
70
        $lastSql = $this->sqlLogger->queries[count($this->sqlLogger->queries)];
71
72
        // the entity's identifier is of type "rot13" so the query parameter needs to be this type too
73
        $this->assertSame(
74
            $this->rot13Type->getName(),
75
            $lastSql['types'][0],
76
            "asserting that the entity's identifier type is correctly inferred"
77
        );
78
    }
79
80
    /**
81
     * when having an entity, that has a composite identifier, we throw an exception because this is not supported
82
     */
83
    public function testIssueWithCompositeIdentifier()
84
    {
85
        $this->expectException(ORMInvalidArgumentException::class);
86
87
        $entity           = new GH6443CombinedIdentityEntity();
88
        $entity->id       = 'Foo';
89
        $entity->secondId = 'Bar';
90
91
        $dql   = 'SELECT entity FROM ' . GH6443CombinedIdentityEntity::class . ' entity WHERE entity = ?1';
92
        $query = $this->em->createQuery($dql);
93
94
        // we set the entity as arameter
95
        $query->setParameter(1, $entity);
96
97
        // this is when the exception should be thrown
98
        $query->getResult();
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    protected function setUp() : void
105
    {
106
        parent::setUp();
107
108
        $this->sqlLogger = new DebugStack();
109
        $this->em->getConnection()->getConfiguration()->setSQLLogger($this->sqlLogger);
110
111
        $this->schemaTool->createSchema([
112
            $this->em->getClassMetadata(GH6443Post::class),
113
            $this->em->getClassMetadata(GH6443CombinedIdentityEntity::class),
114
        ]);
115
116
        $this->rot13Type = Type::getType('rot13');
117
    }
118
119
    protected function tearDown() : void
120
    {
121
        parent::tearDown();
122
123
        $this->schemaTool->dropSchema([
124
            $this->em->getClassMetadata(GH6443Post::class),
125
            $this->em->getClassMetadata(GH6443CombinedIdentityEntity::class),
126
        ]);
127
    }
128
}
129
130
/**
131
 * @ORM\Entity
132
 */
133
class GH6443Post
134
{
135
    /**
136
     * @ORM\Id
137
     * @ORM\Column(type="rot13")
138
     */
139
    public $id;
140
}
141
142
143
/**
144
 * @ORM\Entity
145
 */
146
class GH6443CombinedIdentityEntity
147
{
148
    /**
149
     * @ORM\Id
150
     * @ORM\Column(type="rot13")
151
     */
152
    public $id;
153
154
    /**
155
     * @ORM\Id
156
     * @ORM\Column(type="string")
157
     */
158
    public $secondId;
159
}
160