1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Doctrine\Tests\ORM\Query; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Types\FloatType; |
7
|
|
|
use Doctrine\DBAL\Types\IntegerType; |
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 |
|
|
|
|
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); |
|
|
|
|
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 |
|
|
|
|
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' => FloatType::class, |
67
|
|
|
'average' => FloatType::class, |
68
|
|
|
'absolute' => FloatType::class, |
69
|
|
|
'length' => IntegerType::class, |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|