1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoliaSpecification\QueryFactory; |
4
|
|
|
|
5
|
|
|
use GBProd\AlgoliaSpecification\QueryFactory\Factory; |
6
|
|
|
use GBProd\AlgoliaSpecification\QueryFactory\NotFactory; |
7
|
|
|
use GBProd\AlgoliaSpecification\Registry; |
8
|
|
|
use GBProd\Specification\Not; |
9
|
|
|
use GBProd\Specification\OrX; |
10
|
|
|
use GBProd\Specification\Specification; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class NotFactoryTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testConstruct() |
16
|
|
|
{ |
17
|
|
|
$factory = new NotFactory(new Registry()); |
18
|
|
|
|
19
|
|
|
$this->assertInstanceOf(NotFactory::class, $factory); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testCreateReturnsNotQuery() |
23
|
|
|
{ |
24
|
|
|
$not = $this->createNot(); |
25
|
|
|
$registry = $this->createRegistry($not); |
26
|
|
|
|
27
|
|
|
$factory = new NotFactory($registry); |
28
|
|
|
|
29
|
|
|
$query = $factory->create($not); |
30
|
|
|
|
31
|
|
|
$this->assertEquals('NOT query', $query); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Not |
36
|
|
|
*/ |
37
|
|
|
private function createNot() |
38
|
|
|
{ |
39
|
|
|
return new Not( |
40
|
|
|
$this->createMock(Specification::class) |
|
|
|
|
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Not $not |
46
|
|
|
* |
47
|
|
|
* @return Registry |
48
|
|
|
*/ |
49
|
|
|
private function createRegistry($not) |
50
|
|
|
{ |
51
|
|
|
$factory = $this->createMock(Factory::class); |
52
|
|
|
$factory |
53
|
|
|
->expects($this->any()) |
54
|
|
|
->method('create') |
55
|
|
|
->willReturn('query') |
56
|
|
|
; |
57
|
|
|
|
58
|
|
|
$registry = new Registry(); |
59
|
|
|
|
60
|
|
|
$registry->register(get_class($not->getWrappedSpecification()), $factory); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $registry; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testCreateThrowExceptionIfNotNotSpecification() |
66
|
|
|
{ |
67
|
|
|
$spec = $this->createMock(Specification::class); |
68
|
|
|
$registry = new Registry(); |
69
|
|
|
$factory = new NotFactory($registry); |
70
|
|
|
|
71
|
|
|
$this->expectException(\InvalidArgumentException::class); |
72
|
|
|
|
73
|
|
|
$factory->create($spec); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testCreateThrowExceptionIfWrappedSpecificationIsAGroup() |
77
|
|
|
{ |
78
|
|
|
$spec = new Not( |
79
|
|
|
new OrX( |
80
|
|
|
$this->createMock(Specification::class), |
|
|
|
|
81
|
|
|
$this->createMock(Specification::class) |
|
|
|
|
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$registry = new Registry(); |
86
|
|
|
$factory = new NotFactory($registry); |
87
|
|
|
|
88
|
|
|
$this->expectException(\InvalidArgumentException::class); |
89
|
|
|
|
90
|
|
|
$factory->create($spec); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: