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 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
|
|||
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), |
||
47 | $this->createMock(Specification::class) |
||
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); |
||
68 | $registry->register(get_class($andx->getSecondPart()), $factory); |
||
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()); |
||
83 | } |
||
84 | } |
||
85 |
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.