Completed
Push — master ( ea4e5e...1a7580 )
by Zbigniew
02:25
created

ZibiosWrikeExtensionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 10.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 12
loc 119
rs 10
c 0
b 0
f 0

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
 * This file is part of the WrikeBundle package.
4
 *
5
 * (c) Zbigniew Ślązak
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zibios\Bundle\WrikeBundle\Tests\DependencyInjection;
12
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Zibios\Bundle\WrikeBundle\Api\Factory;
15
use Zibios\Bundle\WrikeBundle\DependencyInjection\ZibiosWrikeExtension;
16
use Zibios\WrikePhpLibrary\Api;
17
18
/**
19
 * ZibiosWrikeExtensionTest
20
 */
21
class ZibiosWrikeExtensionTest extends DependencyInjectionTestCase
22
{
23
    /**
24
     * Some basic tests to make sure the configuration is correctly processed in the standard case.
25
     *
26
     * @param array $sourceConfig
27
     * @param array $expectedConfig
28
     * @param $expectedExceptionClass
29
     *
30
     * @dataProvider configurationProvider
31
     */
32
    public function test_processConfig(array $sourceConfig, array $expectedConfig, $expectedExceptionClass)
33
    {
34
        $exceptionOccurred = false;
35
        $exceptionClass = '';
36
        $exceptionMessage = '';
37
        $calculatedConfig = [];
38
        try {
39
            $container = $this->getContainer(
40
                [
41
                    $sourceConfig
42
                ]
43
            );
44
45
            $calculatedConfig = $container->getParameter('zibios_wrike');
46
        } catch (\Exception $e) {
47
            $exceptionOccurred = true;
48
            $exceptionClass = get_class($e);
49
            $exceptionMessage = $e->getMessage();
50
        }
51
52
        if ($expectedExceptionClass !== false) {
53
            self::assertTrue(
54
                $exceptionOccurred,
55
                sprintf(
56
                    '"%s" exception should occurred for "%s".',
57
                    $expectedExceptionClass,
58
                    json_encode($sourceConfig)
59
                )
60
            );
61
        }
62
63
        if ($expectedExceptionClass === false) {
64
            self::assertFalse(
65
                $exceptionOccurred,
66
                sprintf(
67
                    '"%s" exception occurred but should not for "%s". Message "%s"',
68
                    $exceptionClass,
69
                    json_encode($sourceConfig),
70
                    $exceptionMessage
71
                )
72
            );
73
            $this->assertEquals($expectedConfig, $calculatedConfig);
74
        }
75
    }
76
77
    public function test_services()
78
    {
79
        $sourceConfiguration = [
80
            'permanent_tokens' => [
81
                'default_token' => 'firstName',
82
                'tokens' => [
83
                    'firstName' => 'firstToken',
84
                    'secondName' => 'secondToken',
85
                    'thirdName' => 'thirdToken',
86
                ]
87
            ]
88
        ];
89
90
        $container = $this->getContainer([$sourceConfiguration]);
91
        self::assertEquals(
92
            Factory::class,
93
            $container->getParameter('zibios_wrike.api_factory.class')
94
        );
95
        $apiFactory = $container->get('zibios_wrike.api_factory');
96
        self::assertInstanceOf(Factory::class, $apiFactory);
97
98
        self::assertEquals(
99
            Api::class,
100
            $container->getParameter('zibios_wrike.api.class')
101
        );
102
        $apiFactory = $container->get('zibios_wrike.api');
103
        self::assertInstanceOf(Api::class, $apiFactory);
104
105
        /** @var array $tokens */
106
        $tokens = $sourceConfiguration['permanent_tokens']['tokens'];
107
        foreach ($tokens as $tokenName => $tokenCode) {
108
            $serviceId = sprintf('zibios_wrike.app.%s', strtolower($tokenName));
109
            self::assertTrue($container->hasDefinition($serviceId));
110
            self::assertInstanceOf(Api::class, $container->get($serviceId));
111
            self::assertEquals($tokenCode, $container->get($serviceId)->getBearerToken());
112
        }
113
114
        $serviceIds = array_keys($container->getDefinitions());
115
        $expectedServiceIds = [
116
            'zibios_wrike.api_factory',
117
            'zibios_wrike.api',
118
            'zibios_wrike.app.firstname',
119
            'zibios_wrike.app.secondname',
120
            'zibios_wrike.app.thirdname',
121
        ];
122
        self::assertEquals($expectedServiceIds, $serviceIds);
123
    }
124
125
    /**
126
     * @param array $config
127
     *
128
     * @return ContainerBuilder
129
     */
130
    protected function getContainer(array $config = [])
131
    {
132
        $container = new ContainerBuilder();
133
        $loader = new ZibiosWrikeExtension();
134
        $loader->load($config, $container);
135
        $container->compile();
136
137
        return $container;
138
    }
139
}
140