1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElasticaSpecification\QueryFactory; |
4
|
|
|
|
5
|
|
|
use Elastica\QueryBuilder; |
6
|
|
|
use Elastica\Query\AbstractQuery; |
7
|
|
|
use Elastica\Query\BoolQuery; |
8
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\Factory; |
9
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\NotFactory; |
10
|
|
|
use GBProd\ElasticaSpecification\Registry; |
11
|
|
|
use GBProd\Specification\Not; |
12
|
|
|
use GBProd\Specification\Specification; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class NotFactoryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testConstruct() |
18
|
|
|
{ |
19
|
|
|
$factory = new NotFactory(new Registry()); |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf(NotFactory::class, $factory); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testCreateReturnsNotQuery() |
25
|
|
|
{ |
26
|
|
|
$not = $this->createNot(); |
27
|
|
|
$registry = $this->createRegistry($not); |
28
|
|
|
|
29
|
|
|
$factory = new NotFactory($registry); |
30
|
|
|
|
31
|
|
|
$query = $factory->create($not, new QueryBuilder()); |
32
|
|
|
|
33
|
|
|
$this->assertInstanceOf(BoolQuery::class, $query); |
34
|
|
|
|
35
|
|
|
$this->assertArrayHasKey('bool', $query->toArray()); |
36
|
|
|
$this->assertArrayHasKey('must_not', $query->toArray()['bool']); |
37
|
|
|
$this->assertCount(1, $query->toArray()['bool']['must_not']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Not |
42
|
|
|
*/ |
43
|
|
|
private function createNot() |
44
|
|
|
{ |
45
|
|
|
return new Not( |
46
|
|
|
$this->createMock(Specification::class) |
|
|
|
|
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Not $not |
52
|
|
|
* |
53
|
|
|
* @return Registry |
54
|
|
|
*/ |
55
|
|
|
private function createRegistry($not) |
56
|
|
|
{ |
57
|
|
|
$factory = $this->createMock(Factory::class); |
58
|
|
|
$factory |
59
|
|
|
->expects($this->any()) |
60
|
|
|
->method('create') |
61
|
|
|
->willReturn($this->createMock(AbstractQuery::class)) |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$registry = new Registry(); |
65
|
|
|
|
66
|
|
|
$registry->register(get_class($not->getWrappedSpecification()), $factory); |
|
|
|
|
67
|
|
|
|
68
|
|
|
return $registry; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function testCreateThrowExceptionIfNotNotSpecification() |
73
|
|
|
{ |
74
|
|
|
$spec = $this->createMock(Specification::class); |
75
|
|
|
$registry = new Registry(); |
76
|
|
|
$factory = new NotFactory($registry); |
77
|
|
|
|
78
|
|
|
$this->expectException(\InvalidArgumentException::class); |
79
|
|
|
|
80
|
|
|
$factory->create($spec, new QueryBuilder()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: