Completed
Pull Request — master (#6735)
by Matthias
11:52
created

GH6443Test::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
        self::expectException(ORMInvalidArgumentException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

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

85
        self::/** @scrutinizer ignore-call */ 
86
              expectException(ORMInvalidArgumentException::class);
Loading history...
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';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function setUp() : void
106
    {
107
        parent::setUp();
108
109
        $this->sqlLogger = new DebugStack();
110
        $this->em->getConnection()->getConfiguration()->setSQLLogger($this->sqlLogger);
111
112
        $this->schemaTool->createSchema([
113
            $this->em->getClassMetadata(GH6443Post::class),
114
            $this->em->getClassMetadata(GH6443CombinedIdentityEntity::class),
115
        ]);
116
117
        $this->rot13Type = Type::getType('rot13');
118
    }
119
120
    protected function tearDown() : void
121
    {
122
        parent::tearDown();
123
124
        $this->schemaTool->dropSchema([
125
                $this->em->getClassMetadata(GH6443Post::class),
126
                $this->em->getClassMetadata(GH6443CombinedIdentityEntity::class),
127
        ]);
128
    }
129
}
130
131
/**
132
 * @ORM\Entity
133
 */
134
class GH6443Post
135
{
136
    /**
137
     * @ORM\Id
138
     * @ORM\Column(type="rot13")
139
     */
140
    public $id;
141
}
142
143
144
/**
145
 * @ORM\Entity
146
 */
147
class GH6443CombinedIdentityEntity
148
{
149
    /**
150
     * @ORM\Id
151
     * @ORM\Column(type="rot13")
152
     */
153
    public $id;
154
155
    /**
156
     * @ORM\Id
157
     * @ORM\Column(type="string")
158
     */
159
    public $secondId;
160
}
161