Completed
Push — master ( 098427...ee57de )
by GBProd
04:07 queued 01:59
created

OrXBuilderTest::createOrX()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 4
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\ExpressionBuilder\OrXBuilder;
9
use GBProd\ElasticaSpecification\ExpressionBuilder\Builder;
10
use GBProd\ElasticaSpecification\Registry;
11
use GBProd\Specification\OrX;
12
use GBProd\Specification\Specification;
13
14 View Code Duplication
class OrXBuilderTest extends \PHPUnit_Framework_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...
15
{
16
    public function testConstruct()
17
    {
18
        $builder = new OrXBuilder(new Registry());
19
20
        $this->assertInstanceOf(OrXBuilder::class, $builder);
21
    }
22
23
    public function testBuildReturnsOrxExpression()
24
    {
25
        $orx = $this->createOrX();
26
        $registry = $this->createRegistry($orx);
27
28
        $builder = new OrXBuilder($registry);
29
30
        $query = $builder->build($orx, new QueryBuilder());
31
32
        $this->assertInstanceOf(BoolQuery::class, $query);
33
34
        $this->assertArrayHasKey('bool', $query->toArray());
35
        $this->assertArrayHasKey('should', $query->toArray()['bool']);
36
        $this->assertCount(2, $query->toArray()['bool']['should']);
37
    }
38
39
    /**
40
     * @return OrX
41
     */
42
    private function createOrX()
43
    {
44
        return new OrX(
45
            $this->createMock(Specification::class),
46
            $this->createMock(Specification::class)
47
        );
48
    }
49
50
    /**
51
     * @param OrX $orx
52
     *
53
     * @return Registry
54
     */
55
    private function createRegistry($orx)
56
    {
57
        $builder = $this->createMock(Builder::class);
58
        $builder
59
            ->expects($this->any())
60
            ->method('build')
61
            ->willReturn($this->createMock(AbstractQuery::class))
62
        ;
63
64
        $registry = new Registry();
65
66
        $registry->register(get_class($orx->getFirstPart()), $builder);
67
        $registry->register(get_class($orx->getSecondPart()), $builder);
68
69
        return $registry;
70
    }
71
72
73
    public function testBuildThrowExceptionIfNotOrXSpecification()
74
    {
75
        $spec = $this->createMock(Specification::class);
76
        $registry = new Registry();
77
        $builder = new OrXBuilder($registry);
78
79
        $this->setExpectedException(\InvalidArgumentException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
81
        $expr = $builder->build($spec, new QueryBuilder());
0 ignored issues
show
Unused Code introduced by
$expr is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
    }
83
}
84