Failed Conditions
Push — master ( f1c5e1...95a60e )
by GBProd
02:56
created

HandlerTest::testHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Tests\GBProd\ElasticaSpecification;
4
5
use Elastica\QueryBuilder;
6
use Elastica\Query\AbstractQuery;
7
use GBProd\ElasticaSpecification\ExpressionBuilder\AndXBuilder;
8
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder;
9
use GBProd\ElasticaSpecification\ExpressionBuilder\NotBuilder;
10
use GBProd\ElasticaSpecification\ExpressionBuilder\OrXBuilder;
11
use GBProd\ElasticaSpecification\Handler;
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
18
class HandlerTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var Registry
22
     */
23
    private $registry;
24
25
    /**
26
     * @var Handler
27
     */
28
    private $handler;
29
30
    protected function setUp()
31
    {
32
        $this->registry = new Registry();
33
        $this->handler = new Handler($this->registry);
34
    }
35
36
    public function testConstructWillRegisterBaseBuilders()
37
    {
38
        $spec1 = $this->createMock(Specification::class);
39
        $spec2 = $this->createMock(Specification::class);
40
41
        $this->assertInstanceOf(
42
            AndXBuilder::class,
43
            $this->registry->getBuilder(new AndX($spec1, $spec2))
44
        );
45
46
        $this->assertInstanceOf(
47
            OrXBuilder::class,
48
            $this->registry->getBuilder(new OrX($spec1, $spec2))
49
        );
50
51
        $this->assertInstanceOf(
52
            NotBuilder::class,
53
            $this->registry->getBuilder(new Not($spec1))
54
        );
55
    }
56
57
    public function testRegisterBuilderAddBuilderInRegistry()
58
    {
59
        $builder = $this->createMock(Builder::class);
60
        $spec = $this->createMock(Specification::class);
61
62
        $this->handler->registerBuilder(get_class($spec), $builder);
63
64
        $this->assertEquals(
65
            $builder,
66
            $this->registry->getBuilder($spec)
67
        );
68
    }
69
70
    public function testHandle()
71
    {
72
        $this->handler = new Handler(new Registry());
73
74
        $builder = $this->prophesize(Builder::class);
75
76
        $spec = $this->createMock(Specification::class);
77
        $this->handler->registerBuilder(get_class($spec), $builder->reveal());
78
79
        $builtQuery = $this->getMockForAbstractClass(AbstractQuery::class);
80
        $qb = new QueryBuilder();
81
82
        $builder
83
            ->build($spec, $qb)
84
            ->willReturn($builtQuery)
85
            ->shouldBeCalled()
86
        ;
87
88
        $this->assertEquals(
89
            $builtQuery,
90
            $this->handler->handle($spec, $qb)
91
        );
92
    }
93
}
94