Completed
Pull Request — master (#10)
by
unknown
02:39
created

erTest.php$0   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

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
declare(strict_types=1);
4
5
namespace Lamoda\QueueBundle\Tests\Unit\Service;
6
7
use Lamoda\QueueBundle\Exception\UnknownStrategyKeyException;
8
use Lamoda\QueueBundle\Service\DelayStrategyResolver;
9
use Lamoda\QueueBundle\Strategy\Delay\ArithmeticProgressionStrategy;
10
use Lamoda\QueueBundle\Strategy\Delay\GeometricProgressionStrategy;
11
use PHPUnit_Framework_TestCase;
12
13
class DelayStrategyResolverTest extends PHPUnit_Framework_TestCase
14
{
15 View Code Duplication
    public function testGetStrategy()
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...
16
    {
17
        $checkingQueueName = 'queue';
18
        $strategiesByQueues = ['queue' => 'arithmetic_strategy_key'];
19
20
        $resolver = $this->createDelayStrategyResolver($strategiesByQueues);
21
        $strategy = $resolver->getStrategy($checkingQueueName);
22
        $this->assertInstanceOf(ArithmeticProgressionStrategy::class, $strategy);
23
        $this->assertAttributeEquals(100, 'startInterval', $strategy);
24
        $this->assertAttributeEquals(4, 'multiplier', $strategy);
25
    }
26
27 View Code Duplication
    public function testGetDefaultStrategy()
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...
28
    {
29
        $checkingQueueName = 'queue';
30
        $strategiesByQueues = [];
31
32
        $resolver = $this->createDelayStrategyResolver($strategiesByQueues);
33
        $strategy = $resolver->getStrategy($checkingQueueName);
34
        $this->assertInstanceOf(GeometricProgressionStrategy::class, $strategy);
35
        $this->assertAttributeEquals(60, 'startInterval', $strategy);
36
        $this->assertAttributeEquals(3, 'multiplier', $strategy);
37
    }
38
39
    public function testFailGetDefaultStrategy()
40
    {
41
        $this->expectException(UnknownStrategyKeyException::class);
42
        $checkingQueueName = 'queue';
43
        $strategiesByQueues = ['queue' => 'unknown_strategy_key'];
44
45
        $resolver = $this->createDelayStrategyResolver($strategiesByQueues);
46
        $resolver->getStrategy($checkingQueueName);
47
    }
48
49
    private function createDelayStrategyResolver(array $strategiesByQueues): DelayStrategyResolver
50
    {
51
        $arithmeticStrategy = new ArithmeticProgressionStrategy(100, 4);
52
        $geometricStrategy = new GeometricProgressionStrategy(30, 2);
53
        $defaultStrategy = new GeometricProgressionStrategy(60, 3);
54
55
        $strategies = [
56
            'arithmetic_strategy_key' => $arithmeticStrategy,
57
            'geometric_strategy_key' => $geometricStrategy,
58
            'default_delay_strategy' => $defaultStrategy,
59
        ];
60
61
        return new DelayStrategyResolver($this->createStrategiesList($strategies), $strategiesByQueues);
62
    }
63
64 View Code Duplication
    private function createStrategiesList(array $strategies)
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...
65
    {
66
        return new class($strategies) implements \Iterator {
67
            /**
68
             * @var array
69
             */
70
            protected $strategies;
71
72
            public function __construct(array $strategies)
73
            {
74
                $this->strategies = $strategies;
75
            }
76
77
            public function current()
78
            {
79
                return current($this->strategies);
80
            }
81
82
            public function next()
83
            {
84
                return next($this->strategies);
85
            }
86
87
            public function key()
88
            {
89
                return key($this->strategies);
90
            }
91
92
            public function valid()
93
            {
94
                return null !== key($this->strategies);
95
            }
96
97
            public function rewind()
98
            {
99
                return reset($this->strategies);
100
            }
101
        };
102
    }
103
}
104