Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

NormalizerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 19.57 %

Importance

Changes 0
Metric Value
wmc 6
dl 18
loc 92
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldPreserveUnknownOptions() 0 7 1
A _before() 0 6 1
A shouldDeduplicateIndices() 0 12 1
A shouldPreserveUnknownOption() 9 9 1
A shouldStripUnknownOption() 7 7 1
A shouldNormalizeKnownOptionName() 0 19 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 AmaTeam\ElasticSearch\Test\Suite\Functional\Indexing;
4
5
use AmaTeam\ElasticSearch\API\Indexing;
6
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context;
7
use AmaTeam\ElasticSearch\API\Indexing\OptionInterface;
8
use AmaTeam\ElasticSearch\Indexing\Normalizer;
9
use AmaTeam\ElasticSearch\Indexing\Operations;
10
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry;
11
use Codeception\Test\Unit;
12
use PHPUnit\Framework\Assert;
13
use PHPUnit_Framework_MockObject_MockObject as Mock;
14
15
class NormalizerTest extends Unit
16
{
17
    /**
18
     * @var Indexing
19
     */
20
    private $dummy;
21
22
    protected function _before()
23
    {
24
        $this->dummy = (new Indexing())
25
            ->setReadIndices([])
26
            ->setWriteIndices([])
27
            ->setType('doc');
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function shouldNormalizeKnownOptionName()
34
    {
35
        $id = 'alpha';
36
        $friendlyId = 'beta';
37
        /** @var Mock|OptionInterface $option */
38
        $option = $this->createMock(OptionInterface::class);
39
        $option
40
            ->expects($this->any())
41
            ->method('getId')
42
            ->willReturn($id);
43
        $option
44
            ->expects($this->any())
45
            ->method('getFriendlyId')
46
            ->willReturn($friendlyId);
47
        $registry = (new Registry())->register($option);
48
        $input = Operations::from($this->dummy)->setOption($friendlyId, 'value');
49
        $expectation = Operations::from($this->dummy)->setOption($id, 'value');
50
        $output = (new Normalizer($registry))->normalize($input);
51
        Assert::assertEquals($expectation, $output);
52
    }
53
54
    /**
55
     * @test
56
     */
57 View Code Duplication
    public function shouldStripUnknownOption()
0 ignored issues
show
Duplication introduced by
This method 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...
58
    {
59
        $option = 'alpha';
60
        $context = (new Context())->setPreserveUnknownOptions(false);
61
        $input = Operations::from($this->dummy)->setOption($option, 'value');
62
        $output = (new Normalizer())->normalize($input, $context);
63
        Assert::assertEquals($this->dummy, $output);
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function shouldPreserveUnknownOptions()
70
    {
71
        $option = 'alpha';
72
        $context = (new Context())->setPreserveUnknownOptions(true);
73
        $input = Operations::from($this->dummy)->setOption($option, 'value');
74
        $output = (new Normalizer())->normalize($input, $context);
75
        Assert::assertEquals($input, $output);
76
    }
77
78
    /**
79
     * @test
80
     */
81 View Code Duplication
    public function shouldPreserveUnknownOption()
0 ignored issues
show
Duplication introduced by
This method 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...
82
    {
83
        $option = 'alpha';
84
        $context = (new Context())
85
            ->setPreserveUnknownOptions(false)
86
            ->setPreservedOptions([$option]);
87
        $input = Operations::from($this->dummy)->setOption($option, 'value');
88
        $output = (new Normalizer())->normalize($input, $context);
89
        Assert::assertEquals($input, $output);
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function shouldDeduplicateIndices()
96
    {
97
        $index = 'curly';
98
        $indices = [$index, $index, $index];
99
        $input = Operations::from($this->dummy)
100
            ->setReadIndices($indices)
101
            ->setWriteIndices($indices);
102
        $expectation = Operations::from($this->dummy)
103
            ->setReadIndices([$index])
104
            ->setWriteIndices([$index]);
105
        $output = (new Normalizer())->normalize($input);
106
        Assert::assertEquals($expectation, $output);
107
    }
108
}
109