AndXFactoryTest::testConstruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Tests\GBProd\ElasticaSpecification;
4
5
use Elastica\QueryBuilder;
6
use Elastica\Query\AbstractQuery;
7
use Elastica\Query\BoolQuery;
8
use GBProd\ElasticaSpecification\QueryFactory\AndXFactory;
9
use GBProd\ElasticaSpecification\QueryFactory\Factory;
10
use GBProd\ElasticaSpecification\Registry;
11
use GBProd\Specification\AndX;
12
use GBProd\Specification\Specification;
13
use PHPUnit\Framework\TestCase;
14
15 View Code Duplication
class AndXFactoryTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
16
{
17
    public function testConstruct()
18
    {
19
        $factory = new AndXFactory(new Registry());
20
21
        $this->assertInstanceOf(AndXFactory::class, $factory);
22
    }
23
24
    public function testCreateReturnsAndxQuery()
25
    {
26
        $andx = $this->createAndX();
27
        $registry = $this->createRegistry($andx);
28
29
        $factory = new AndXFactory($registry);
30
31
        $query = $factory->create($andx, new QueryBuilder());
32
33
        $this->assertInstanceOf(BoolQuery::class, $query);
34
35
        $this->assertArrayHasKey('bool', $query->toArray());
36
        $this->assertArrayHasKey('must', $query->toArray()['bool']);
37
        $this->assertCount(2, $query->toArray()['bool']['must']);
38
    }
39
40
    /**
41
     * @return AndX
42
     */
43
    private function createAndX()
44
    {
45
        return new AndX(
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
            $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...
48
        );
49
    }
50
51
    /**
52
     * @param AndX $andx
53
     *
54
     * @return Registry
55
     */
56
    private function createRegistry($andx)
57
    {
58
        $factory = $this->createMock(Factory::class);
59
        $factory
60
            ->expects($this->any())
61
            ->method('create')
62
            ->willReturn($this->createMock(AbstractQuery::class))
63
        ;
64
65
        $registry = new Registry();
66
67
        $registry->register(get_class($andx->getFirstPart()), $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...
68
        $registry->register(get_class($andx->getSecondPart()), $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...
69
70
        return $registry;
71
    }
72
73
74
    public function testCreateThrowExceptionIfNotAndXSpecification()
75
    {
76
        $spec = $this->createMock(Specification::class);
77
        $registry = new Registry();
78
        $factory = new AndXFactory($registry);
79
80
        $this->expectException(\InvalidArgumentException::class);
81
82
        $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...
83
    }
84
}
85