Completed
Pull Request — master (#6735)
by Matthias
12:21
created

GH6443Test::testIssueWithProxyClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 22
rs 9.9
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\ORM\Annotation as ORM;
9
use Doctrine\DBAL\Types\Type;
0 ignored issues
show
Coding Style introduced by
Use statements should be sorted alphabetically. The first wrong one is Doctrine\DBAL\Types\Type.
Loading history...
10
use Doctrine\Tests\DbalTypes\Rot13Type;
11
use Doctrine\Tests\OrmFunctionalTestCase;
12
13
/**
14
 * @group 6443
15
 */
16
class GH6443Test extends OrmFunctionalTestCase
17
{
0 ignored issues
show
introduced by
There must be exactly 0 empty lines after class opening brace.
Loading history...
18
19
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \Doctrine\Tests\ORM\Functional\Ticket\GH6443Test::$rot13Type with single line content, use one-line comment instead.
Loading history...
20
     * @var Rot13Type
21
     */
22
    private $rot13Type;
23
24
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \Doctrine\Tests\ORM\Functional\Ticket\GH6443Test::$sqlLogger with single line content, use one-line comment instead.
Loading history...
25
     * @var DebugStack
26
     */
27
    private $sqlLogger;
28
29
    /**
30
     * when having an entity, that has a non scalar identifier, the type will not be guessed / converted correctly
31
     */
32
    public function testIssue()
33
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
34
35
        $entity = new GH6443Post();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
36
        $entity->id = 'Foo';
37
38
        $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...
39
        $query = $this->em->createQuery($dql);
40
41
        // we do not know that the internal type is a rot13, so we can not add the type parameter here
42
        $query->setParameter(1, $entity);
43
44
        // we do not need the result, but we need to execute it to log the SQL-Statement
45
        $query->getResult();
46
47
        $lastSql = $this->sqlLogger->queries[count($this->sqlLogger->queries)];
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
48
49
        // the entity's identifier is of type "rot13" so the query parameter needs to be this type too
50
        $this->assertSame(
51
            $this->rot13Type->getName(),
52
            $lastSql['types'][0],
53
            "asserting that the entity's identifier type is correctly inferred"
54
        );
55
    }
56
57
    /**
58
     * when having an entity, that has a non scalar identifier, the type will not be guessed / converted correctly
59
     */
60
    public function testIssueWithProxyClass()
61
    {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
62
63
        $metadata = $this->em->getClassMetadata(GH6443Post::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
64
        $entityProxy = $this->em->getProxyFactory()->getProxy($metadata, ['id' => 'Foo']);
65
66
        $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...
67
        $query = $this->em->createQuery($dql);
68
69
        // we do not know that the internal type is a rot13, so we can not add the type parameter here
70
        $query->setParameter(1, $entityProxy);
71
72
        // we do not need the result, but we need to execute it to log the SQL-Statement
73
        $query->getResult();
74
75
        $lastSql = $this->sqlLogger->queries[count($this->sqlLogger->queries)];
0 ignored issues
show
introduced by
Function count() should not be referenced via a fallback global name, but via a use statement.
Loading history...
76
77
        // the entity's identifier is of type "rot13" so the query parameter needs to be this type too
78
        $this->assertSame(
79
            $this->rot13Type->getName(),
80
            $lastSql['types'][0],
81
            "asserting that the entity's identifier type is correctly inferred"
82
        );
83
    }
84
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    protected function setUp(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
90
    {
91
        parent::setUp();
92
93
        $this->sqlLogger = new DebugStack();
94
        $this->em->getConnection()->getConfiguration()->setSQLLogger($this->sqlLogger);
95
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
96
97
        $this->schemaTool->createSchema([
98
            $this->em->getClassMetadata(GH6443Post::class),
99
        ]);
100
101
        $this->rot13Type = Type::getType('rot13');
102
    }
103
104
    protected function tearDown(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
105
    {
106
        parent::tearDown();
107
108
        $this->schemaTool->dropSchema([
109
                $this->em->getClassMetadata(GH6443Post::class),
110
        ]);
111
    }
112
113
}
0 ignored issues
show
introduced by
There must be exactly 0 empty lines before class closing brace.
Loading history...
114
115
/**
116
 * @ORM\Entity
117
 */
118
class GH6443Post
119
{
120
    /**
121
     * @ORM\Id
122
     * @ORM\Column(type="rot13")
123
     */
124
    public $id;
125
}
126