Completed
Pull Request — master (#2)
by Wachter
03:01
created

TaskCompilerPassTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 16.09 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 0
cbo 4
dl 14
loc 87
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C testProcess() 14 84 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
namespace Unit\DependencyInjection;
4
5
use Prophecy\Argument;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
9
use Task\TaskBundle\DependencyInjection\TaskCompilerPass;
10
11
class TaskCompilerPassTest extends \PHPUnit_Framework_TestCase
12
{
13
    public function testProcess()
14
    {
15
        $containerBuilder = $this->prophesize(ContainerBuilder::class);
16
        $schedulerDefinition = $this->prophesize(Definition::class);
17
        $handler1Definition = $this->prophesize(Definition::class);
18
        $handler2Definition = $this->prophesize(Definition::class);
19
20
        $containerBuilder->has(TaskCompilerPass::SCHEDULER_ID)->willReturn(true);
21
        $containerBuilder->getDefinition(TaskCompilerPass::SCHEDULER_ID)->willReturn($schedulerDefinition->reveal());
22
        $containerBuilder->getDefinition('id-1')->willReturn($handler1Definition->reveal());
23
        $containerBuilder->getDefinition('id-2')->willReturn($handler2Definition->reveal());
24
25
        $containerBuilder->findTaggedServiceIds(TaskCompilerPass::INTERVAL_TAG)
26
            ->willReturn(
27
                [
28
                    'id-1' => [
29
                        [
30
                            TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
31
                            TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload',
32
                            TaskCompilerPass::KEY_ATTRIBUTE => 'test-key'
33
                        ],
34
                    ],
35
                    'id-2' => [
36
                        [
37
                            TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
38
                            TaskCompilerPass::KEY_ATTRIBUTE => 'test-key-1'
39
                        ],
40
                        [
41
                            TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
42
                            TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload-2'
43
                        ],
44
                    ],
45
                ]
46
            );
47
48
        $handler1Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
49
            [[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-1']]
50
        );
51
        $handler2Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
52
            [[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-2']]
53
        );
54
55
        $compilerPass = new TaskCompilerPass();
56
        $compilerPass->process($containerBuilder->reveal());
57
58
        $schedulerDefinition->addMethodCall(
59
            TaskCompilerPass::CREATE_FUNCTION_NAME,
60
            Argument::that(
61
                function ($arguments) {
62
                    return
63
                        $arguments[0] === 'handler-1'
64
                        && $arguments[1] === 'test-workload'
65
                        && $arguments[2] === 'daily'
66
                        && $arguments[3] === 'test-key';
67
                }
68
            )
69
        )->shouldBeCalledTimes(1);
70
        $schedulerDefinition->addMethodCall(
71
            TaskCompilerPass::CREATE_FUNCTION_NAME,
72
            Argument::that(
73 View Code Duplication
                function ($arguments) {
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...
74
                    return
75
                        $arguments[0] === 'handler-2'
76
                        && $arguments[1] === null
77
                        && $arguments[2] === 'daily'
78
                        && $arguments[3] === 'test-key';
79
                }
80
            )
81
        )->shouldBeCalledTimes(1);
82
        $schedulerDefinition->addMethodCall(
83
            TaskCompilerPass::CREATE_FUNCTION_NAME,
84
            Argument::that(
85 View Code Duplication
                function ($arguments) {
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...
86
                    return
87
                        $arguments[0] === 'handler-2'
88
                        && $arguments[1] === 'test-workload-2'
89
                        && $arguments[2] === 'daily'
90
                        && $arguments[3] === 'handler-2_daily_s:15:"test-workload-2";';
91
                }
92
            )
93
        )->shouldBeCalledTimes(1);
94
95
        // TODO this test always returns true should be extended
96
    }
97
}
98