Failed Conditions
Push — master ( 4651c2...a00b65 )
by GBProd
02:20
created

OrXFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 6 6 1
A testCreateReturnsOrxQuery() 15 15 1
A createOrX() 7 7 1
A createRegistry() 16 16 1
A testCreateThrowExceptionIfNotOrXSpecification() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\OrXFactory;
10
use GBProd\ElasticaSpecification\Registry;
11
use GBProd\Specification\OrX;
12
use GBProd\Specification\Specification;
13
use PHPUnit\Framework\TestCase;
14
15 View Code Duplication
class OrXFactoryTest 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 OrXFactory(new Registry());
20
21
        $this->assertInstanceOf(OrXFactory::class, $factory);
22
    }
23
24
    public function testCreateReturnsOrxQuery()
25
    {
26
        $orx = $this->createOrX();
27
        $registry = $this->createRegistry($orx);
28
29
        $factory = new OrXFactory($registry);
30
31
        $query = $factory->create($orx, new QueryBuilder());
32
33
        $this->assertInstanceOf(BoolQuery::class, $query);
34
35
        $this->assertArrayHasKey('bool', $query->toArray());
36
        $this->assertArrayHasKey('should', $query->toArray()['bool']);
37
        $this->assertCount(2, $query->toArray()['bool']['should']);
38
    }
39
40
    /**
41
     * @return OrX
42
     */
43
    private function createOrX()
44
    {
45
        return new OrX(
46
            $this->createMock(Specification::class),
47
            $this->createMock(Specification::class)
48
        );
49
    }
50
51
    /**
52
     * @param OrX $orx
53
     *
54
     * @return Registry
55
     */
56
    private function createRegistry($orx)
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($orx->getFirstPart()), $factory);
68
        $registry->register(get_class($orx->getSecondPart()), $factory);
69
70
        return $registry;
71
    }
72
73
74
    public function testCreateThrowExceptionIfNotOrXSpecification()
75
    {
76
        $spec = $this->createMock(Specification::class);
77
        $registry = new Registry();
78
        $factory = new OrXFactory($registry);
79
80
        $this->expectException(\InvalidArgumentException::class);
81
82
        $factory->create($spec, new QueryBuilder());
83
    }
84
}
85