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

QueryFactory/OrXFactoryTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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