Completed
Push — master ( b39006...2667f3 )
by GBProd
03:38
created

testCreateThrowExceptionIfNotNotSpecification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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\NotFactory;
9
use GBProd\ElasticaSpecification\QueryFactory\Factory;
10
use GBProd\ElasticaSpecification\Registry;
11
use GBProd\Specification\Not;
12
use GBProd\Specification\Specification;
13
14
class NotFactoryTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testConstruct()
17
    {
18
        $factory = new NotFactory(new Registry());
19
20
        $this->assertInstanceOf(NotFactory::class, $factory);
21
    }
22
23
    public function testCreateReturnsNotQuery()
24
    {
25
        $not = $this->createNot();
26
        $registry = $this->createRegistry($not);
27
28
        $factory = new NotFactory($registry);
29
30
        $query = $factory->create($not, new QueryBuilder());
31
32
        $this->assertInstanceOf(BoolQuery::class, $query);
33
34
        $this->assertArrayHasKey('bool', $query->toArray());
35
        $this->assertArrayHasKey('must_not', $query->toArray()['bool']);
36
        $this->assertCount(1, $query->toArray()['bool']['must_not']);
37
    }
38
39
    /**
40
     * @return Not
41
     */
42
    private function createNot()
43
    {
44
        return new Not(
45
            $this->createMock(Specification::class)
46
        );
47
    }
48
49
    /**
50
     * @param Not $not
51
     *
52
     * @return Registry
53
     */
54
    private function createRegistry($not)
55
    {
56
        $factory = $this->createMock(Factory::class);
57
        $factory
58
            ->expects($this->any())
59
            ->method('create')
60
            ->willReturn($this->createMock(AbstractQuery::class))
61
        ;
62
63
        $registry = new Registry();
64
65
        $registry->register(get_class($not->getWrappedSpecification()), $factory);
66
67
        return $registry;
68
    }
69
70
71
    public function testCreateThrowExceptionIfNotNotSpecification()
72
    {
73
        $spec = $this->createMock(Specification::class);
74
        $registry = new Registry();
75
        $factory = new NotFactory($registry);
76
77
        $this->expectException(\InvalidArgumentException::class);
78
79
        $factory->create($spec, new QueryBuilder());
80
    }
81
}
82