Completed
Pull Request — master (#7941)
by
unknown
64:07
created

SelectSqlMappingTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 30
c 2
b 2
f 0
dl 0
loc 55
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A assertScalarTypes() 0 20 2
A testTypeFromMathematicNodeFunction() 0 16 1
1
<?php
2
declare(strict_types=1);
0 ignored issues
show
introduced by
Expected 2 newlines between PHP open tag and declare statement, found 1.
Loading history...
3
4
namespace Doctrine\Tests\ORM\Query;
5
6
use Doctrine\DBAL\Types\IntegerType;
7
use Doctrine\DBAL\Types\StringType;
8
use Doctrine\ORM\Query as ORMQuery;
9
use Doctrine\Tests\OrmTestCase;
10
use ReflectionException;
11
use ReflectionObject;
12
use function array_search;
13
use function sprintf;
14
15
class SelectSqlMappingTest extends OrmTestCase
16
{
17
    private $em;
18
19
    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...
20
    {
21
        $this->em = $this->getTestEntityManager();
22
    }
23
24
    /**
25
     * Assert a valid SQL generation.
26
     *
27
     * @throws ReflectionException
28
     */
29
    public function assertScalarTypes(string $dqlToBeTested, array $types)
30
    {
31
        $query = $this->em->createQuery($dqlToBeTested);
32
        $query
33
            ->setHint(ORMQuery::HINT_FORCE_PARTIAL_LOAD, true)
34
            ->useQueryCache(false);
35
        $r = new ReflectionObject($query);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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
        $method = $r->getMethod('getResultSetMapping');
37
        $method->setAccessible(true);
38
        /** @var ORMQuery\ResultSetMapping $mapping */
39
        $mapping = $method->invoke($query);
40
        foreach ($types as $key => $expectedType) {
41
            $alias = array_search($key, $mapping->scalarMappings);
42
            self::assertInstanceOf(
43
                $expectedType,
44
                $mapping->typeMappings[$alias],
45
                sprintf('The field "%s" was expected as a %s', $key, $expectedType)
46
            );
47
        }
48
        $query->free();
49
    }
50
51
    /**
52
     * @group DDC-2235
53
     */
54
    public function testTypeFromMathematicNodeFunction(): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
55
    {
56
        $this->assertScalarTypes(
57
            'SELECT p, 
58
          count(p.id) as count, 
59
          SUM(p.price) as sales, 
60
          AVG(p.price) as average, 
61
          ABS(p.price) as absolute,
62
          LENGTH(p.name) as length
63
          FROM Doctrine\Tests\Models\CMS\CmsProduct p',
64
            [
65
                'count'    => IntegerType::class,
66
                'sales'    => StringType::class,
67
                'average'  => StringType::class,
68
                'absolute' => StringType::class,
69
                'length'   => IntegerType::class,
70
            ]
71
        );
72
    }
73
}
74