Passed
Pull Request — master (#7641)
by
unknown
13:02
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 Exception;
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
        try {
34
            $query = $this->em->createQuery($dqlToBeTested);
35
36
            $query
37
                ->setHint(ORMQuery::HINT_FORCE_PARTIAL_LOAD, true)
38
                ->useQueryCache(false);
39
40
            $r      = new ReflectionObject($query);
41
            $method = $r->getMethod('getResultSetMapping');
42
            $method->setAccessible(true);
43
            /** @var ORMQuery\ResultSetMapping $mapping */
44
            $mapping = $method->invoke($query);
45
            foreach ($types as $key => $expectedType) {
46
                $alias = array_search($key, $mapping->scalarMappings);
47
                self::assertInstanceOf(
48
                    $expectedType,
49
                    $mapping->typeMappings[$alias],
50
                    sprintf('The field "%s" was expected as a $expectedType', $key)
51
                );
52
            }
53
            $query->free();
54
        } catch (Exception $e) {
55
            $this->fail($e->getMessage() . "\n" . $e->getTraceAsString());
56
        }
57
    }
58
59
    /**
60
     * @group DDC-2235
61
     */
62
    public function testTypeFromMathematicNodeFunction() : void
63
    {
64
        $this->assertScalarTypes(
65
            'SELECT p, 
66
          count(p.id) as count, 
67
          SUM(p.price) as sales, 
68
          AVG(p.price) as average, 
69
          ABS(p.price) as absolute,
70
          LENGTH(p.name) as length
71
          FROM Doctrine\Tests\Models\CMS\CmsProduct p',
72
            [
73
                'count' => IntegerType::class,
74
                'sales' => FloatType::class,
75
                'average' => FloatType::class,
76
                'absolute' => FloatType::class,
77
                'length' => IntegerType::class,
78
            ]
79
        );
80
    }
81
}
82