Passed
Pull Request — master (#7641)
by
unknown
09:30
created

SelectSqlMappingTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 ReflectionException;
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
     * @throws ReflectionException
29
     */
30
    public function assertScalarTypes(string $dqlToBeTested, array $types)
31
    {
32
        $query = $this->em->createQuery($dqlToBeTested);
33
34
        $query
35
            ->setHint(ORMQuery::HINT_FORCE_PARTIAL_LOAD, true)
36
            ->useQueryCache(false);
37
38
        $r      = new ReflectionObject($query);
39
        $method = $r->getMethod('getResultSetMapping');
40
        $method->setAccessible(true);
41
        /** @var ORMQuery\ResultSetMapping $mapping */
42
        $mapping = $method->invoke($query);
43
        foreach ($types as $key => $expectedType) {
44
            $alias = array_search($key, $mapping->scalarMappings);
45
            self::assertInstanceOf(
46
                $expectedType,
47
                $mapping->typeMappings[$alias],
48
                sprintf('The field "%s" was expected as a %s', $key, $expectedType)
49
            );
50
        }
51
        $query->free();
52
    }
53
54
    /**
55
     * @group DDC-2235
56
     */
57
    public function testTypeFromMathematicNodeFunction() : void
58
    {
59
        $this->assertScalarTypes(
60
            'SELECT p, 
61
          count(p.id) as count, 
62
          SUM(p.price) as sales, 
63
          AVG(p.price) as average, 
64
          ABS(p.price) as absolute,
65
          LENGTH(p.name) as length
66
          FROM Doctrine\Tests\Models\CMS\CmsProduct p',
67
            [
68
                'count' => IntegerType::class,
69
                'sales' => FloatType::class,
70
                'average' => FloatType::class,
71
                'absolute' => FloatType::class,
72
                'length' => IntegerType::class,
73
            ]
74
        );
75
    }
76
}
77