|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\GBProd\ElasticaSpecification; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\QueryBuilder; |
|
6
|
|
|
use Elastica\Query\AbstractQuery; |
|
7
|
|
|
use Elastica\Query\BoolQuery; |
|
8
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\AndXBuilder; |
|
9
|
|
|
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder; |
|
10
|
|
|
use GBProd\ElasticaSpecification\Registry; |
|
11
|
|
|
use GBProd\Specification\AndX; |
|
12
|
|
|
use GBProd\Specification\Specification; |
|
13
|
|
|
|
|
14
|
|
|
class AndXBuilderTest extends \PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testConstruct() |
|
17
|
|
|
{ |
|
18
|
|
|
$builder = new AndXBuilder(new Registry()); |
|
19
|
|
|
|
|
20
|
|
|
$this->assertInstanceOf(AndXBuilder::class, $builder); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testBuildReturnsAndxExpression() |
|
24
|
|
|
{ |
|
25
|
|
|
$andx = $this->createAndX(); |
|
26
|
|
|
$registry = $this->createRegistry($andx); |
|
27
|
|
|
|
|
28
|
|
|
$builder = new AndXBuilder($registry); |
|
29
|
|
|
|
|
30
|
|
|
$query = $builder->build($andx, new QueryBuilder()); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertInstanceOf(BoolQuery::class, $query); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertArrayHasKey('bool', $query->toArray()); |
|
35
|
|
|
$this->assertArrayHasKey('must', $query->toArray()['bool']); |
|
36
|
|
|
$this->assertCount(2, $query->toArray()['bool']['must']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return AndX |
|
41
|
|
|
*/ |
|
42
|
|
|
private function createAndX() |
|
43
|
|
|
{ |
|
44
|
|
|
return new AndX( |
|
45
|
|
|
$this->createMock(Specification::class), |
|
46
|
|
|
$this->createMock(Specification::class) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param AndX $andx |
|
52
|
|
|
* |
|
53
|
|
|
* @return Registry |
|
54
|
|
|
*/ |
|
55
|
|
|
private function createRegistry($andx) |
|
56
|
|
|
{ |
|
57
|
|
|
$builder = $this->createMock(Builder::class); |
|
58
|
|
|
$builder |
|
59
|
|
|
->expects($this->any()) |
|
60
|
|
|
->method('build') |
|
61
|
|
|
->willReturn($this->createMock(AbstractQuery::class)) |
|
62
|
|
|
; |
|
63
|
|
|
|
|
64
|
|
|
$registry = new Registry(); |
|
65
|
|
|
|
|
66
|
|
|
$registry->register(get_class($andx->getFirstPart()), $builder); |
|
67
|
|
|
$registry->register(get_class($andx->getSecondPart()), $builder); |
|
68
|
|
|
|
|
69
|
|
|
return $registry; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public function testBuildThrowExceptionIfNotAndXSpecification() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$spec = $this->createMock(Specification::class); |
|
76
|
|
|
$registry = new Registry(); |
|
77
|
|
|
$builder = new AndXBuilder($registry); |
|
78
|
|
|
|
|
79
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
80
|
|
|
|
|
81
|
|
|
$expr = $builder->build($spec, new QueryBuilder()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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.