Completed
Pull Request — master (#7641)
by
unknown
07:29
created

SelectSqlMappingTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

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