Passed
Push — feature/initial-implementation ( 69714c...aa3ce7 )
by Fike
02:16
created

NormalizerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 17.31 %

Importance

Changes 0
Metric Value
wmc 7
dl 18
loc 104
rs 10
c 0
b 0
f 0

7 Methods

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