SearchTextFilterConditionTransformerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 40 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 48
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testTransformCreatesFilterWithOrMethod() 0 8 1
B testTransformWithOneField() 24 24 1
B testTransformWithOneFieldUsingLikeMethod() 24 24 1
A testTransformDoNotTransformUnsupportedMethod() 0 19 1
A testTransformOnlyTransformQueryFields() 0 18 1
A testTransformOnlyTransformNonArraySelectAliases() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Prophecy\Prophecy\ObjectProphecy. Did you maybe mean getMethodProphecies()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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