Passed
Pull Request — master (#7641)
by
unknown
08:43
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 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
20
    {
21
        $this->em = $this->getTestEntityManager();
22
    }
23
24
    /**
25
     * Assert a valid SQL generation.
26
     *
27
     * @param string $dqlToBeTested
0 ignored issues
show
introduced by
Incorrect annotations group.
Loading history...
28
     * @param array $types
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
29
     * @throws \ReflectionException
0 ignored issues
show
introduced by
Class \ReflectionException should not be referenced via a fully qualified name, but via a use statement.
Loading history...
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