RoundingRuleTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 11.48 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 7
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testToOptionArray() 0 12 1
A testToArray() 7 15 3
A mockGetRoundingRules() 0 7 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 Meanbee\MagentoRoyalmail\Test\Unit\Model\Config\Source;
4
5
use Meanbee\MagentoRoyalmail\Model\Config\Source\RoundingRule;
6
use Meanbee\MagentoRoyalmail\Model\Rounder;
7
8
/**
9
 * Class RoundingRuleTest
10
 */
11
class RoundingRuleTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var Rounder|\PHPUnit_Framework_MockObject_MockObject
15
     */
16
    protected $rounder;
17
    /**
18
     * @var RoundingRule|\PHPUnit_Framework_MockObject_MockObject
19
     */
20
    protected $roundingRule;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    protected function setUp()
26
    {
27
        $this->rounder = $this->getMockBuilder('Meanbee\MagentoRoyalmail\Model\Rounder')
28
            ->disableOriginalConstructor()
29
            ->getMock();
30
31
        $this->roundingRule = new RoundingRule($this->rounder);
32
    }
33
34
    public function testToOptionArray()
35
    {
36
        $rules = ['none' => 'No rounding performed'];
37
        $this->mockGetRoundingRules($rules);
38
39
        $options = $this->roundingRule->toOptionArray();
40
        $this->assertEquals(1, count($options));
41
42
        $option = array_pop($options);
43
        $this->assertEquals('none', $option['value']);
44
        $this->assertEquals('No rounding performed', $option['label']);
45
    }
46
47
    public function testToArray()
48
    {
49
        $rules = ['none' => 'No rounding performed'];
50
        $this->mockGetRoundingRules($rules);
51
52
        $options = $this->roundingRule->toArray();
53
54 View Code Duplication
        foreach ($options as $option) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            $this->assertInternalType('array', $option);
56
            foreach ($option as $key => $value) {
57
                $this->assertInternalType('string', $key);
58
                $this->assertInstanceOf(\Magento\Framework\Phrase::class, $value);
59
            }
60
        }
61
    }
62
63
64
    protected function mockGetRoundingRules($rules)
65
    {
66
        $this->rounder
67
            ->expects($this->once())
68
            ->method('getRoundingRules')
69
            ->willReturn($rules);
70
    }
71
}
72