Completed
Push — master ( ed304f...e36e8e )
by Matthew
05:35
created

DtcQueueExtensionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 32.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 25
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDtcQueueExtension() 0 9 1
A tryBadConfigs() 11 11 2
A testBeanstalkExtension() 0 17 1
A tryGoodConfigs() 14 14 2
A testRabbitMq() 0 12 1
A arrayTest() 0 6 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 Dtc\QueueBundle\Tests\DependencyInjection;
4
5
use Dtc\QueueBundle\DependencyInjection\DtcQueueExtension;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
class DtcQueueExtensionTest extends TestCase
10
{
11
    public function testDtcQueueExtension()
12
    {
13
        $dtcQueueExtension = new DtcQueueExtension();
14
        $configs = [];
15
        $containerBuilder = new ContainerBuilder();
16
        $dtcQueueExtension->load($configs, $containerBuilder);
17
18
        self::assertEquals('dtc_queue', $dtcQueueExtension->getAlias());
19
    }
20
21 View Code Duplication
    public function tryBadConfigs(array $configs)
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...
22
    {
23
        $dtcQueueExtension = new DtcQueueExtension();
24
        $containerBuilder = new ContainerBuilder();
25
        try {
26
            $dtcQueueExtension->load($configs, $containerBuilder);
27
            self::fail('should not reach here');
28
        } catch (\Exception $exception) {
29
            self::assertTrue(true);
30
        }
31
    }
32
33
    public function testBeanstalkExtension()
34
    {
35
        $configs = ['config' => ['beanstalkd' => ['host' => null]]];
36
        $this->tryBadConfigs($configs);
37
        $configs = ['config' => ['beanstalkd' => ['tube' => null]]];
38
        $this->tryBadConfigs($configs);
39
40
        $configs = ['config' => ['beanstalkd' => ['host' => 'somehost']]];
41
        $containerBuilder = $this->tryGoodConfigs($configs);
42
        self::assertEquals('somehost', $containerBuilder->getParameter('dtc_queue.beanstalkd.host'));
43
44
        $configs = ['config' => ['beanstalkd' => ['host' => 'somehost', 'tube' => 'something']]];
45
        $containerBuilder = $this->tryGoodConfigs($configs);
46
47
        self::assertEquals('something', $containerBuilder->getParameter('dtc_queue.beanstalkd.tube'));
48
        self::assertEquals('somehost', $containerBuilder->getParameter('dtc_queue.beanstalkd.host'));
49
    }
50
51 View Code Duplication
    protected function tryGoodConfigs(array $configs)
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...
52
    {
53
        $dtcQueueExtension = new DtcQueueExtension();
54
        $containerBuilder = new ContainerBuilder();
55
56
        try {
57
            $dtcQueueExtension->load($configs, $containerBuilder);
58
        } catch (\Exception $exception) {
59
            self::fail($exception->getMessage());
60
        }
61
        self::assertTrue(true);
62
63
        return $containerBuilder;
64
    }
65
66
    public function testRabbitMq()
67
    {
68
        $configs = ['config' => ['rabbit_mq' => []]];
69
        $this->tryBadConfigs($configs);
70
71
        $configs = ['config' => ['rabbit_mq' => ['host' => 'somehost', 'port' => 1234, 'user' => 'auser', 'password' => 'pass']]];
72
        $containerBuilder = $this->tryGoodConfigs($configs);
73
        $this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'host', 'somehost');
74
        $this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'port', 1234);
75
        $this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'user', 'auser');
76
        $this->arrayTest($containerBuilder, 'dtc_queue.rabbit_mq', 'password', 'pass');
77
    }
78
79
    public function arrayTest(ContainerBuilder $containerBuilder, $parameter, $key, $result)
80
    {
81
        $arr = $containerBuilder->getParameter($parameter);
82
        self::assertArrayHasKey($key, $arr);
83
        self::assertEquals($result, $arr[$key]);
84
    }
85
}
86