1
|
|
|
<?php |
2
|
|
|
namespace Netdudes\DataSourceryBundle\Tests\Query; |
3
|
|
|
|
4
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field; |
5
|
|
|
use Netdudes\DataSourceryBundle\DataType\DataTypeInterface; |
6
|
|
|
use Netdudes\DataSourceryBundle\Query\Filter; |
7
|
|
|
use Netdudes\DataSourceryBundle\Query\FilterCondition; |
8
|
|
|
use Netdudes\DataSourceryBundle\Query\SearchTextFilterConditionTransformer; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Prophecy\Argument; |
11
|
|
|
|
12
|
|
|
class SearchTextFilterConditionTransformerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testTransformCreatesFilterWithOrMethod() |
15
|
|
|
{ |
16
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
17
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
18
|
|
|
$result = $transformer->transform($filterCondition->reveal(), []); |
19
|
|
|
|
20
|
|
|
$this->assertEquals(Filter::CONDITION_TYPE_OR, $result->getConditionType()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testTransformWithOneField() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$searchPhrase = 'search phrase'; |
26
|
|
|
|
27
|
|
|
$dataType = $this->prophesize(DataTypeInterface::class); |
28
|
|
|
$dataType->supports(Argument::any())->willReturn(true); |
29
|
|
|
|
30
|
|
|
$field = $this->prophesize(Field::class); |
31
|
|
|
$field->getDataType()->willReturn($dataType->reveal()); |
32
|
|
|
$field->getDatabaseFilterQueryField()->willReturn('query_field'); |
33
|
|
|
$field->getDatabaseSelectAlias()->willReturn('alias'); |
34
|
|
|
$field->getUniqueName()->shouldBeCalled(); |
35
|
|
|
|
36
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
37
|
|
|
$filterCondition->getValue()->willReturn($searchPhrase); |
38
|
|
|
$filterCondition->getMethod()->shouldBeCalled(); |
|
|
|
|
39
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
40
|
|
|
$result = $transformer->transform($filterCondition->reveal(), [$field->reveal()]); |
41
|
|
|
|
42
|
|
|
$this->assertInstanceOf(Filter::class, $result); |
43
|
|
|
$this->assertCount(1, $result); |
44
|
|
|
$this->assertInstanceOf(FilterCondition::class, $result[0]); |
45
|
|
|
$this->assertEquals($searchPhrase, $result[0]->getValue()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testTransformWithOneFieldUsingLikeMethod() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$searchPhrase = 'search phrase'; |
51
|
|
|
|
52
|
|
|
$dataType = $this->prophesize(DataTypeInterface::class); |
53
|
|
|
$dataType->supports(Argument::any())->willReturn(true); |
54
|
|
|
|
55
|
|
|
$field = $this->prophesize(Field::class); |
56
|
|
|
$field->getDataType()->willReturn($dataType->reveal()); |
57
|
|
|
$field->getDatabaseFilterQueryField()->willReturn('query_field'); |
58
|
|
|
$field->getDatabaseSelectAlias()->willReturn('alias'); |
59
|
|
|
$field->getUniqueName()->shouldBeCalled(); |
60
|
|
|
|
61
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
62
|
|
|
$filterCondition->getValue()->willReturn($searchPhrase); |
63
|
|
|
$filterCondition->getMethod()->willReturn(FilterCondition::METHOD_STRING_LIKE); |
|
|
|
|
64
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
65
|
|
|
$result = $transformer->transform($filterCondition->reveal(), [$field->reveal()]); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf(Filter::class, $result); |
68
|
|
|
$this->assertCount(1, $result); |
69
|
|
|
$this->assertInstanceOf(FilterCondition::class, $result[0]); |
70
|
|
|
$this->assertEquals("%$searchPhrase%", $result[0]->getValue()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testTransformDoNotTransformUnsupportedMethod() |
74
|
|
|
{ |
75
|
|
|
$unsupportedMethod = 'unsupported_method'; |
76
|
|
|
|
77
|
|
|
$dataType = $this->prophesize(DataTypeInterface::class); |
78
|
|
|
$dataType->supports($unsupportedMethod)->willReturn(false); |
79
|
|
|
|
80
|
|
|
$field = $this->prophesize(Field::class); |
81
|
|
|
$field->getDataType()->willReturn($dataType->reveal()); |
82
|
|
|
|
83
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
84
|
|
|
$filterCondition->getValue()->shouldBeCalled(); |
85
|
|
|
$filterCondition->getMethod()->willReturn($unsupportedMethod); |
|
|
|
|
86
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
87
|
|
|
$result = $transformer->transform($filterCondition->reveal(), [$field->reveal()]); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf(Filter::class, $result); |
90
|
|
|
$this->assertCount(0, $result); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testTransformOnlyTransformQueryFields() |
94
|
|
|
{ |
95
|
|
|
$dataType = $this->prophesize(DataTypeInterface::class); |
96
|
|
|
$dataType->supports(Argument::any())->willReturn(true); |
97
|
|
|
|
98
|
|
|
$field = $this->prophesize(Field::class); |
99
|
|
|
$field->getDataType()->willReturn($dataType->reveal()); |
100
|
|
|
$field->getDatabaseFilterQueryField()->willReturn(null); |
101
|
|
|
|
102
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
103
|
|
|
$filterCondition->getValue()->shouldBeCalled(); |
104
|
|
|
$filterCondition->getMethod()->shouldBeCalled(); |
|
|
|
|
105
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
106
|
|
|
$result = $transformer->transform($filterCondition->reveal(), [$field->reveal()]); |
107
|
|
|
|
108
|
|
|
$this->assertInstanceOf(Filter::class, $result); |
109
|
|
|
$this->assertCount(0, $result); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testTransformOnlyTransformNonArraySelectAliases() |
113
|
|
|
{ |
114
|
|
|
$dataType = $this->prophesize(DataTypeInterface::class); |
115
|
|
|
$dataType->supports(Argument::any())->willReturn(true); |
116
|
|
|
|
117
|
|
|
$field = $this->prophesize(Field::class); |
118
|
|
|
$field->getDataType()->willReturn($dataType->reveal()); |
119
|
|
|
$field->getDatabaseFilterQueryField()->willReturn('query_field'); |
120
|
|
|
$field->getDatabaseSelectAlias()->willReturn(['a', 'b', 'c']); |
121
|
|
|
|
122
|
|
|
$filterCondition = $this->prophesize(FilterCondition::class); |
123
|
|
|
$filterCondition->getValue()->shouldBeCalled(); |
124
|
|
|
$filterCondition->getMethod()->shouldBeCalled(); |
|
|
|
|
125
|
|
|
$transformer = new SearchTextFilterConditionTransformer(); |
126
|
|
|
$result = $transformer->transform($filterCondition->reveal(), [$field->reveal()]); |
127
|
|
|
|
128
|
|
|
$this->assertInstanceOf(Filter::class, $result); |
129
|
|
|
$this->assertCount(0, $result); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.