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

ceTest.php$1   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 100 %

Coupling/Cohesion

Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
cbo 0
dl 37
loc 37
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 DateInterval;
8
use DateTime;
9
use Lamoda\QueueBundle\Entity\QueueEntityInterface;
10
use Lamoda\QueueBundle\Service\DelayService;
11
use Lamoda\QueueBundle\Service\DelayStrategyResolver;
12
use Lamoda\QueueBundle\Strategy\Delay\DelayStrategyInterface;
13
use Lamoda\QueueBundle\Tests\Unit\QueueEntity;
14
use Lamoda\QueueBundle\Tests\Unit\SymfonyMockTrait;
15
use PHPUnit_Framework_TestCase;
16
17
class DelayServiceTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Bug introduced by
There is one abstract method getMockBuilder in this class; you could implement it, or declare this class as abstract.
Loading history...
18
{
19
    use SymfonyMockTrait;
20
21
    /**
22
     * @param QueueEntityInterface $queue
23
     * @param QueueEntityInterface $expectedQueue
24
     * @param DateTime             $dateTime
25
     *
26
     * @dataProvider dataDelayQueue
27
     */
28
    public function testDelayQueue(
29
        QueueEntityInterface $queue,
30
        QueueEntityInterface $expectedQueue,
31
        DateTime $dateTime
32
    ): void {
33
        $strategyService = $this->createDelayStrategyResolver();
34
        $logger          = $this->getMockLogger();
35
        /** @var DelayService | \PHPUnit_Framework_MockObject_MockObject $delayService */
36
        $delayService = $this->getMockBuilder(DelayService::class)
37
            ->setConstructorArgs([$strategyService, $logger])
38
            ->setMethods(['getStartDateTime'])
39
            ->getMock();
40
        $delayService->method('getStartDateTime')
41
            ->willReturn($dateTime);
42
43
        $actualQueue = $delayService->delayQueue($queue);
44
        $this->assertEquals($expectedQueue, $actualQueue);
45
    }
46
47
    /**
48
     * @return array
49
     * @throws \Exception
50
     *
51
     */
52
    public function dataDelayQueue(): array
53
    {
54
        $strategyService = $this->createDelayStrategyResolver();
55
        $dateTime        = new DateTime();
56
        $queue           = $this->createQueue();
57
        $strategy        = $strategyService->getStrategy($queue->getName());
58
        $expected        = clone $queue;
59
        $expected->setWaiting($dateTime->add($strategy::getInterval()));
60
61
        return [
62
            [
63
                $queue,
64
                $expected,
65
                $dateTime,
66
            ],
67
        ];
68
    }
69
70
    private function createQueue(): QueueEntity
71
    {
72
        return new QueueEntity('queue', 'exchange', 'ClassJob', []);
73
    }
74
75
    private function createDelayStrategyResolver(): DelayStrategyResolver
76
    {
77
        $strategy = new class() implements DelayStrategyInterface {
78
            public function generateInterval(int $iteration): DateInterval
79
            {
80
                return static::getInterval();
81
            }
82
83
            public static function getInterval(): DateInterval
84
            {
85
                return new DateInterval('PT1M');
86
            }
87
        };
88
89
        return new DelayStrategyResolver($this->createStrategiesList(['test_strategy_key' => $strategy]), ['queue' => 'test_strategy_key']);
90
    }
91
92 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...
93
    {
94
        return new class($strategies) implements \Iterator {
95
            /**
96
             * @var array
97
             */
98
            protected $strategies;
99
100
            public function __construct(array $strategies)
101
            {
102
                $this->strategies = $strategies;
103
            }
104
105
            public function current()
106
            {
107
                return current($this->strategies);
108
            }
109
110
            public function next()
111
            {
112
                return next($this->strategies);
113
            }
114
115
            public function key()
116
            {
117
                return key($this->strategies);
118
            }
119
120
            public function valid()
121
            {
122
                return key($this->strategies) !== null;
123
            }
124
125
            public function rewind()
126
            {
127
                return reset($this->strategies);
128
            }
129
130
        };
131
    }
132
}
133