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