1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\GBProd\ElasticaSpecification; |
4
|
|
|
|
5
|
|
|
use Elastica\QueryBuilder; |
6
|
|
|
use Elastica\Query\AbstractQuery; |
7
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\AndXBuilder; |
8
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; |
9
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder; |
10
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\OrXBuilder; |
11
|
|
|
use GBProd\ElasticaSpecification\Handler; |
12
|
|
|
use GBProd\ElasticaSpecification\Registry; |
13
|
|
|
use GBProd\Specification\AndX; |
14
|
|
|
use GBProd\Specification\Not; |
15
|
|
|
use GBProd\Specification\OrX; |
16
|
|
|
use GBProd\Specification\Specification; |
17
|
|
|
|
18
|
|
|
class HandlerTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Registry |
22
|
|
|
*/ |
23
|
|
|
private $registry; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Handler |
27
|
|
|
*/ |
28
|
|
|
private $handler; |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->registry = new Registry(); |
33
|
|
|
$this->handler = new Handler($this->registry); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testConstructWillRegisterBaseBuilders() |
37
|
|
|
{ |
38
|
|
|
$spec1 = $this->createMock(Specification::class); |
39
|
|
|
$spec2 = $this->createMock(Specification::class); |
40
|
|
|
|
41
|
|
|
$this->assertInstanceOf( |
42
|
|
|
AndXBuilder::class, |
43
|
|
|
$this->registry->getBuilder(new AndX($spec1, $spec2)) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf( |
47
|
|
|
OrXBuilder::class, |
48
|
|
|
$this->registry->getBuilder(new OrX($spec1, $spec2)) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf( |
52
|
|
|
NotBuilder::class, |
53
|
|
|
$this->registry->getBuilder(new Not($spec1)) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testRegisterBuilderAddBuilderInRegistry() |
58
|
|
|
{ |
59
|
|
|
$builder = $this->createMock(Builder::class); |
60
|
|
|
$spec = $this->createMock(Specification::class); |
61
|
|
|
|
62
|
|
|
$this->handler->registerBuilder(get_class($spec), $builder); |
63
|
|
|
|
64
|
|
|
$this->assertEquals( |
65
|
|
|
$builder, |
66
|
|
|
$this->registry->getBuilder($spec) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testHandle() |
71
|
|
|
{ |
72
|
|
|
$this->handler = new Handler(new Registry()); |
73
|
|
|
|
74
|
|
|
$builder = $this->prophesize(Builder::class); |
75
|
|
|
|
76
|
|
|
$spec = $this->createMock(Specification::class); |
77
|
|
|
$this->handler->registerBuilder(get_class($spec), $builder->reveal()); |
78
|
|
|
|
79
|
|
|
$builtQuery = $this->getMockForAbstractClass(AbstractQuery::class); |
80
|
|
|
$qb = new QueryBuilder(); |
81
|
|
|
|
82
|
|
|
$builder |
83
|
|
|
->build($spec, $qb) |
84
|
|
|
->willReturn($builtQuery) |
85
|
|
|
// ->shouldBeCalled() |
86
|
|
|
; |
87
|
|
|
|
88
|
|
|
$this->handler->handle($spec, $qb); |
89
|
|
|
// $this->assertEquals( |
90
|
|
|
// $builtQuery, |
91
|
|
|
// ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// private function createQueryWithResult($result) |
|
|
|
|
95
|
|
|
// { |
96
|
|
|
// $query = $this |
97
|
|
|
// ->getMockBuilder(AbstractQuery::class) |
|
|
|
|
98
|
|
|
// ->disableOriginalConstructor() |
99
|
|
|
// ->getMock() |
100
|
|
|
// ; |
101
|
|
|
|
102
|
|
|
// $query |
103
|
|
|
// ->expects($this->once()) |
|
|
|
|
104
|
|
|
// ->method('getResult') |
|
|
|
|
105
|
|
|
// ->willReturn($result) |
|
|
|
|
106
|
|
|
// ; |
107
|
|
|
|
108
|
|
|
// return $query; |
109
|
|
|
// } |
110
|
|
|
|
111
|
|
|
// private function createQueryBuilderBuildingQuery($query, $buildedExpr, $spec) |
|
|
|
|
112
|
|
|
// { |
113
|
|
|
// $qb = $this |
114
|
|
|
// ->getMockBuilder(QueryBuilder::class) |
|
|
|
|
115
|
|
|
// ->disableOriginalConstructor() |
116
|
|
|
// ->getMock() |
117
|
|
|
// ; |
118
|
|
|
|
119
|
|
|
// $qb |
120
|
|
|
// ->expects($this->at(0)) |
|
|
|
|
121
|
|
|
// ->method('where') |
|
|
|
|
122
|
|
|
// ->with($buildedExpr) |
|
|
|
|
123
|
|
|
// ; |
124
|
|
|
|
125
|
|
|
// $qb |
126
|
|
|
// ->expects($this->at(1)) |
|
|
|
|
127
|
|
|
// ->method('getQuery') |
|
|
|
|
128
|
|
|
// ->willReturn($query) |
|
|
|
|
129
|
|
|
// ; |
130
|
|
|
|
131
|
|
|
// return $qb; |
132
|
|
|
// } |
133
|
|
|
} |
134
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.