1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\GBProd\ElasticaSpecification; |
4
|
|
|
|
5
|
|
|
use Elastica\QueryBuilder; |
6
|
|
|
use Elastica\Query\AbstractQuery; |
7
|
|
|
use GBProd\ElasticaSpecification\Handler; |
8
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\AndXFactory; |
9
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\Factory; |
10
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\NotFactory; |
11
|
|
|
use GBProd\ElasticaSpecification\QueryFactory\OrXFactory; |
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
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
class HandlerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Registry |
23
|
|
|
*/ |
24
|
|
|
private $registry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var QueryBuilder |
28
|
|
|
*/ |
29
|
|
|
private $qb; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Handler |
33
|
|
|
*/ |
34
|
|
|
private $handler; |
35
|
|
|
|
36
|
|
|
protected function setUp() |
37
|
|
|
{ |
38
|
|
|
$this->registry = new Registry(); |
39
|
|
|
$this->qb = new QueryBuilder(); |
40
|
|
|
$this->handler = new Handler($this->registry, $this->qb); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testConstructWillRegisterBaseFactoriess() |
44
|
|
|
{ |
45
|
|
|
$spec1 = $this->createMock(Specification::class); |
46
|
|
|
$spec2 = $this->createMock(Specification::class); |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf( |
49
|
|
|
AndXFactory::class, |
50
|
|
|
$this->registry->getFactory(new AndX($spec1, $spec2)) |
|
|
|
|
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf( |
54
|
|
|
OrXFactory::class, |
55
|
|
|
$this->registry->getFactory(new OrX($spec1, $spec2)) |
|
|
|
|
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->assertInstanceOf( |
59
|
|
|
NotFactory::class, |
60
|
|
|
$this->registry->getFactory(new Not($spec1)) |
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testRegisterFactoryAddFactoryInRegistry() |
65
|
|
|
{ |
66
|
|
|
$factory = $this->createMock(Factory::class); |
67
|
|
|
$spec = $this->createMock(Specification::class); |
68
|
|
|
|
69
|
|
|
$this->handler->registerFactory(get_class($spec), $factory); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->assertEquals( |
72
|
|
|
$factory, |
73
|
|
|
$this->registry->getFactory($spec) |
|
|
|
|
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testHandle() |
78
|
|
|
{ |
79
|
|
|
$factory = $this->prophesize(Factory::class); |
80
|
|
|
|
81
|
|
|
$spec = $this->createMock(Specification::class); |
82
|
|
|
$this->handler->registerFactory(get_class($spec), $factory->reveal()); |
83
|
|
|
|
84
|
|
|
$builtQuery = $this->getMockForAbstractClass(AbstractQuery::class); |
85
|
|
|
|
86
|
|
|
$factory |
87
|
|
|
->create($spec, $this->qb) |
88
|
|
|
->willReturn($builtQuery) |
89
|
|
|
->shouldBeCalled() |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this->assertEquals( |
93
|
|
|
$builtQuery, |
94
|
|
|
$this->handler->handle($spec) |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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: