NotFactoryTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 0 6 1
A testCreateReturnsNotQuery() 0 15 1
A createNot() 0 6 1
A createRegistry() 0 15 1
A testCreateThrowExceptionIfNotNotSpecification() 0 10 1
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\Factory;
9
use GBProd\ElasticaSpecification\QueryFactory\NotFactory;
10
use GBProd\ElasticaSpecification\Registry;
11
use GBProd\Specification\Not;
12
use GBProd\Specification\Specification;
13
use PHPUnit\Framework\TestCase;
14
15
class NotFactoryTest extends TestCase
16
{
17
    public function testConstruct()
18
    {
19
        $factory = new NotFactory(new Registry());
20
21
        $this->assertInstanceOf(NotFactory::class, $factory);
22
    }
23
24
    public function testCreateReturnsNotQuery()
25
    {
26
        $not = $this->createNot();
27
        $registry = $this->createRegistry($not);
28
29
        $factory = new NotFactory($registry);
30
31
        $query = $factory->create($not, new QueryBuilder());
32
33
        $this->assertInstanceOf(BoolQuery::class, $query);
34
35
        $this->assertArrayHasKey('bool', $query->toArray());
36
        $this->assertArrayHasKey('must_not', $query->toArray()['bool']);
37
        $this->assertCount(1, $query->toArray()['bool']['must_not']);
38
    }
39
40
    /**
41
     * @return Not
42
     */
43
    private function createNot()
44
    {
45
        return new Not(
46
            $this->createMock(Specification::class)
0 ignored issues
show
Documentation introduced by
$this->createMock(\GBPro...n\Specification::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GBProd\Specification\Specification>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
        );
48
    }
49
50
    /**
51
     * @param Not $not
52
     *
53
     * @return Registry
54
     */
55
    private function createRegistry($not)
56
    {
57
        $factory = $this->createMock(Factory::class);
58
        $factory
59
            ->expects($this->any())
60
            ->method('create')
61
            ->willReturn($this->createMock(AbstractQuery::class))
62
        ;
63
64
        $registry = new Registry();
65
66
        $registry->register(get_class($not->getWrappedSpecification()), $factory);
0 ignored issues
show
Documentation introduced by
$factory is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GBProd\ElasticaSp...n\QueryFactory\Factory>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
68
        return $registry;
69
    }
70
71
72
    public function testCreateThrowExceptionIfNotNotSpecification()
73
    {
74
        $spec = $this->createMock(Specification::class);
75
        $registry = new Registry();
76
        $factory = new NotFactory($registry);
77
78
        $this->expectException(\InvalidArgumentException::class);
79
80
        $factory->create($spec, new QueryBuilder());
0 ignored issues
show
Documentation introduced by
$spec is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GBProd\Specification\Specification>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
    }
82
}
83