1
|
|
|
<?php |
2
|
|
|
namespace Netdudes\DataSourceryBundle\Tests\DataSource\Driver\Doctrine\QueryBuilder; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
5
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field; |
6
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\JoinGenerator; |
7
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\RequiredFieldsExtractor; |
8
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\SelectGenerator; |
9
|
|
|
use Netdudes\DataSourceryBundle\DataType\NumberDataType; |
10
|
|
|
use Netdudes\DataSourceryBundle\Query\Query; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class SelectGeneratorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testGetSelectFieldMap() |
16
|
|
|
{ |
17
|
|
|
$generator = $this->buildSelectGenerator(); |
18
|
|
|
$dummyQuery = new Query(); |
19
|
|
|
|
20
|
|
|
$map = $generator->getSelectFieldMap($dummyQuery); |
21
|
|
|
|
22
|
|
|
$this->assertCount(3, $map, "Unexpected number of entries in the map"); |
23
|
|
|
$this->assertArrayHasKey('FIELD_1', $map, "FIELD_1_ON_ENTITY is expected to be in the select field map"); |
24
|
|
|
$this->assertArrayHasKey('RELATION_1_FIELD_2', $map, "FIELD_2_ON_RELATION_1 is expected to be in the select field map"); |
25
|
|
|
$this->assertArrayHasKey('RELATION_2_RELATION_3_FIELD_3', $map, "FIELD_3_ON_RELATION_3 is expected to be in the select field map"); |
26
|
|
|
|
27
|
|
|
$this->assertEquals('ENTITY_ALIAS.FIELD_1', $map['FIELD_1'], 'Unexpected select path for alias'); |
28
|
|
|
$this->assertEquals('RELATION_1_ALIAS.FIELD_2', $map['RELATION_1_FIELD_2'], 'Unexpected select path for alias'); |
29
|
|
|
$this->assertEquals('RELATION_3_ALIAS.FIELD_3', $map['RELATION_2_RELATION_3_FIELD_3'], 'Unexpected select path for alias'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testGenerate() |
33
|
|
|
{ |
34
|
|
|
$generator = $this->buildSelectGenerator(); |
35
|
|
|
$dummyQuery = new Query(); |
36
|
|
|
|
37
|
|
|
$select = $generator->generate($dummyQuery); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(3, $select->count(), "The SELECT is expected to have three statements"); |
40
|
|
|
$this->assertContains('ENTITY_ALIAS.FIELD_1 FIELD_1', $select->getParts(), "Expected select statement is missing"); |
41
|
|
|
$this->assertContains('RELATION_1_ALIAS.FIELD_2 RELATION_1_FIELD_2', $select->getParts(), "Expected select statement is missing"); |
42
|
|
|
$this->assertContains('RELATION_3_ALIAS.FIELD_3 RELATION_2_RELATION_3_FIELD_3', $select->getParts(), "Expected select statement is missing"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return RequiredFieldsExtractor |
47
|
|
|
*/ |
48
|
|
|
private function buildRequiredFieldsExtractor() |
49
|
|
|
{ |
50
|
|
|
$extractor = $this->getMockBuilder('Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\RequiredFieldsExtractor') |
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
->setMethods(['extractRequiredFields']) |
53
|
|
|
->getMock(); |
54
|
|
|
|
55
|
|
|
$extractor |
56
|
|
|
->expects($this->any()) |
57
|
|
|
->method('extractRequiredFields') |
58
|
|
|
->will($this->returnValue(['FIELD_1_ON_ENTITY', 'FIELD_2_ON_RELATION_1', 'FIELD_3_ON_RELATION_3'])); |
59
|
|
|
|
60
|
|
|
return $extractor; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return JoinGenerator |
65
|
|
|
*/ |
66
|
|
|
private function buildJoinGenerator() |
67
|
|
|
{ |
68
|
|
|
$joins = [ |
69
|
|
|
'RELATION_1' => new Join(Join::LEFT_JOIN, 'ENTITY_ALIAS.RELATION_1', 'RELATION_1_ALIAS'), |
70
|
|
|
'RELATION_2' => new Join(Join::LEFT_JOIN, 'ENTITY_ALIAS.RELATION_2', 'RELATION_2_ALIAS'), |
71
|
|
|
'RELATION_2.RELATION_3' => new Join(Join::LEFT_JOIN, 'RELATION_2_ALIAS.RELATION_3', 'RELATION_3_ALIAS'), |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$generator = $this->getMockBuilder('Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\JoinGenerator') |
75
|
|
|
->disableOriginalConstructor() |
76
|
|
|
->setMethods(['generate']) |
77
|
|
|
->getMock(); |
78
|
|
|
|
79
|
|
|
$generator |
80
|
|
|
->expects($this->any()) |
81
|
|
|
->method('generate') |
82
|
|
|
->will($this->returnValue($joins)); |
83
|
|
|
|
84
|
|
|
return $generator; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
private function buildDataSourceFields() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
new Field('FIELD_1_ON_ENTITY', 'FIELD', '', new NumberDataType(), 'FIELD_1'), |
94
|
|
|
new Field('FIELD_2_ON_RELATION_1', 'FIELD', '', new NumberDataType(), 'RELATION_1.FIELD_2'), |
95
|
|
|
new Field('FIELD_3_ON_RELATION_3', 'FIELD', '', new NumberDataType(), 'RELATION_2.RELATION_3.FIELD_3'), |
96
|
|
|
new Field('FIELD_4', 'FIELD', '', new NumberDataType(), 'RELATION_2.FIELD_4'), |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return SelectGenerator |
102
|
|
|
*/ |
103
|
|
|
private function buildSelectGenerator() |
104
|
|
|
{ |
105
|
|
|
return new \Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\SelectGenerator( |
106
|
|
|
$this->buildDataSourceFields(), |
107
|
|
|
'ENTITY_ALIAS', |
108
|
|
|
$this->buildJoinGenerator(), |
109
|
|
|
$this->buildRequiredFieldsExtractor() |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|