|
1
|
|
|
<?php |
|
2
|
|
|
namespace Netdudes\DataSourceryBundle\Tests\DataSource\Driver\Doctrine\QueryBuilder; |
|
3
|
|
|
|
|
4
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field; |
|
5
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\JoinGenerator; |
|
6
|
|
|
use Netdudes\DataSourceryBundle\DataType\NumberDataType; |
|
7
|
|
|
use Netdudes\DataSourceryBundle\Query\Query; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
class JoinGeneratorTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testGenerate() |
|
13
|
|
|
{ |
|
14
|
|
|
$generator = $this->buildJoinGenerator(); |
|
15
|
|
|
|
|
16
|
|
|
$dummyQuery = new Query(); |
|
17
|
|
|
|
|
18
|
|
|
$joins = $generator->generate($dummyQuery); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertCount(5, $joins, "Incorrect number of joins returned by the generator."); |
|
21
|
|
|
|
|
22
|
|
|
// List of simple (ENTITY.RELATION) join strings, indexed by the automatically generated alias |
|
23
|
|
|
$joinStringsByAlias = []; |
|
24
|
|
|
foreach ($joins as $join) { |
|
25
|
|
|
$joinStringsByAlias[$join->getAlias()] = $join->getJoin(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// First-level joins expected |
|
29
|
|
|
$this->assertContains('TEST_FROM_ALIAS.testRelation1', $joinStringsByAlias, "A join to testRelation1 is expected"); |
|
30
|
|
|
$this->assertContains('TEST_FROM_ALIAS.testRelation2', $joinStringsByAlias, "A join to testRelation2 is expected"); |
|
31
|
|
|
|
|
32
|
|
|
$firstRelationAlias = array_search('TEST_FROM_ALIAS.testRelation1', $joinStringsByAlias, true); |
|
33
|
|
|
$secondRelationAlias = array_search('TEST_FROM_ALIAS.testRelation2', $joinStringsByAlias, true); |
|
34
|
|
|
|
|
35
|
|
|
// Second-level joins expected |
|
36
|
|
|
$this->assertContains("$firstRelationAlias.testRelation3", $joinStringsByAlias, "A join to testRelation3 through testRelation1 is expected"); |
|
37
|
|
|
$this->assertContains("$secondRelationAlias.testRelation4", $joinStringsByAlias, "A join to testRelation4 through testRelation2 is expected"); |
|
38
|
|
|
|
|
39
|
|
|
$fourthRelationAlias = array_search("$secondRelationAlias.testRelation4", $joinStringsByAlias, true); |
|
40
|
|
|
|
|
41
|
|
|
// Third-level joins expected |
|
42
|
|
|
$this->assertContains("$fourthRelationAlias.testRelation5", $joinStringsByAlias, "A join to testRelation5 through testRelation4 through testRelation2 is expected"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return JoinGenerator |
|
47
|
|
|
*/ |
|
48
|
|
|
private function buildJoinGenerator() |
|
49
|
|
|
{ |
|
50
|
|
|
$queryBuilderDataSourceFields = [ |
|
51
|
|
|
new Field('TEST_FIELD_1', 'TEST_FIELD', '', new NumberDataType(), 'testField1'), |
|
52
|
|
|
new Field('TEST_FIELD_2', 'TEST_FIELD', '', new NumberDataType(), 'testRelation1.testField2'), |
|
53
|
|
|
new Field('TEST_FIELD_3', 'TEST_FIELD', '', new NumberDataType(), 'testRelation2.testField3'), |
|
54
|
|
|
new Field('TEST_FIELD_4', 'TEST_FIELD', '', new NumberDataType(), 'testRelation1.testRelation3.testField4'), |
|
55
|
|
|
new Field('TEST_FIELD_5', 'TEST_FIELD', '', new NumberDataType(), 'testRelation2.testRelation4.testRelation5.testField5'), |
|
56
|
|
|
new Field('TEST_FIELD_6', 'TEST_FIELD', '', new NumberDataType(), 'testField6'), |
|
57
|
|
|
new Field('TEST_FIELD_7', 'TEST_FIELD', '', new NumberDataType(), 'testRelation6.testField7'), |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$fromAlias = 'TEST_FROM_ALIAS'; |
|
61
|
|
|
$requiredFieldsExtractor = $this |
|
62
|
|
|
->getMockBuilder('Netdudes\DataSourceryBundle\DataSource\Driver\Doctrine\QueryBuilder\RequiredFieldsExtractor') |
|
63
|
|
|
->disableOriginalConstructor() |
|
64
|
|
|
->setMethods(['extractRequiredFields']) |
|
65
|
|
|
->getMock(); |
|
66
|
|
|
$requiredFieldsExtractor->expects($this->any()) |
|
67
|
|
|
->method('extractRequiredFields') |
|
68
|
|
|
->will($this->returnValue(['TEST_FIELD_1', 'TEST_FIELD_2', 'TEST_FIELD_3', 'TEST_FIELD_4', 'TEST_FIELD_5'])); |
|
69
|
|
|
|
|
70
|
|
|
return new JoinGenerator($queryBuilderDataSourceFields, $fromAlias, $requiredFieldsExtractor); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|