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\RequiredFieldsExtractor; |
6
|
|
|
use Netdudes\DataSourceryBundle\DataType\NumberDataType; |
7
|
|
|
use Netdudes\DataSourceryBundle\Query\Filter; |
8
|
|
|
use Netdudes\DataSourceryBundle\Query\FilterCondition; |
9
|
|
|
use Netdudes\DataSourceryBundle\Query\Query; |
10
|
|
|
use Netdudes\DataSourceryBundle\Query\Sort; |
11
|
|
|
use Netdudes\DataSourceryBundle\Query\SortCondition; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class RequiredFieldsExtractorTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testExtractRequiredFields() |
17
|
|
|
{ |
18
|
|
|
$extractor = $this->buildExtractor(); |
19
|
|
|
$query = $this->buildQuery(); |
20
|
|
|
|
21
|
|
|
$requiredFields = $extractor->extractRequiredFields($query); |
22
|
|
|
|
23
|
|
|
$this->assertCount(8, $requiredFields); |
24
|
|
|
$this->assertContains('TEST_FIELD_1_REQUIRED_BY_COMPLEX', $requiredFields); |
25
|
|
|
$this->assertContains('TEST_FIELD_2_REQUIRED_BY_COMPLEX', $requiredFields); |
26
|
|
|
$this->assertContains('TEST_FIELD_3_THAT_IS_COMPLEX', $requiredFields); |
27
|
|
|
$this->assertContains('TEST_FIELD_4_REQUIRED_BY_SELECT', $requiredFields); |
28
|
|
|
$this->assertContains('TEST_FIELD_5_REQUIRED_BY_TRANSFORMER', $requiredFields); |
29
|
|
|
$this->assertContains('TEST_FIELD_6_REQUIRED_BY_FILTER', $requiredFields); |
30
|
|
|
$this->assertContains('TEST_FIELD_7_REQUIRED_BY_SORT', $requiredFields); |
31
|
|
|
$this->assertNotContains('TEST_FIELD_8_NOT_REQUIRED', $requiredFields); |
32
|
|
|
$this->assertContains('UNSUPPORTED_FIELD', $requiredFields); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return RequiredFieldsExtractor |
37
|
|
|
*/ |
38
|
|
|
protected function buildExtractor() |
39
|
|
|
{ |
40
|
|
|
$queryBuilderFields = [ |
41
|
|
|
new Field('TEST_FIELD_1_REQUIRED_BY_COMPLEX', 'TEST_FIELD', '', new NumberDataType(), 'testField1'), |
42
|
|
|
new Field('TEST_FIELD_2_REQUIRED_BY_COMPLEX', 'TEST_FIELD', '', new NumberDataType(), 'testField2'), |
43
|
|
|
new Field('TEST_FIELD_3_THAT_IS_COMPLEX', 'TEST_FIELD', '', new NumberDataType(), null, [ |
44
|
|
|
'SUB_FIELD_1' => 'TEST_FIELD_1_REQUIRED_BY_COMPLEX', |
45
|
|
|
'SUB_FIELD_2' => 'TEST_FIELD_2_REQUIRED_BY_COMPLEX', |
46
|
|
|
]), |
47
|
|
|
new Field('TEST_FIELD_4_REQUIRED_BY_SELECT', 'TEST_FIELD', '', new NumberDataType(), 'testField4'), |
48
|
|
|
new Field('TEST_FIELD_5_REQUIRED_BY_TRANSFORMER', 'TEST_FIELD', '', new NumberDataType(), 'testField5'), |
49
|
|
|
new Field('TEST_FIELD_6_REQUIRED_BY_FILTER', 'TEST_FIELD', '', new NumberDataType(), 'testField6'), |
50
|
|
|
new Field('TEST_FIELD_7_REQUIRED_BY_SORT', 'TEST_FIELD', '', new NumberDataType(), 'testField7'), |
51
|
|
|
new Field('TEST_FIELD_8_NOT_REQUIRED', 'TEST_FIELD', '', new NumberDataType(), 'testField8'), |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$transformer = $this |
55
|
|
|
->getMockBuilder('Netdudes\DataSourceryBundle\Transformers\TransformerInterface') |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->setMethods(['getRequiredFieldNames', 'transform']) |
58
|
|
|
->getMock(); |
59
|
|
|
$transformer->expects($this->any()) |
60
|
|
|
->method('getRequiredFieldNames') |
61
|
|
|
->will($this->returnValue(['TEST_FIELD_5_REQUIRED_BY_TRANSFORMER'])); |
62
|
|
|
|
63
|
|
|
return new RequiredFieldsExtractor($queryBuilderFields, [$transformer]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Query |
68
|
|
|
*/ |
69
|
|
|
protected function buildQuery() |
70
|
|
|
{ |
71
|
|
|
$filter = new Filter([new FilterCondition('TEST_FIELD_6_REQUIRED_BY_FILTER', FilterCondition::METHOD_STRING_EQ, 'TEST_VALUE', 'TEST_VALUE')]); |
72
|
|
|
$sort = new Sort([new SortCondition('TEST_FIELD_7_REQUIRED_BY_SORT', null, SortCondition::ASC)]); |
73
|
|
|
|
74
|
|
|
$query = new Query(); |
75
|
|
|
$query->setFilter($filter); |
76
|
|
|
$query->setSort($sort); |
77
|
|
|
$query->setSelect(['TEST_FIELD_4_REQUIRED_BY_SELECT', 'TEST_FIELD_3_THAT_IS_COMPLEX', 'UNSUPPORTED_FIELD']); |
78
|
|
|
|
79
|
|
|
return $query; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|