Completed
Push — 1.8 ( 8d6730...a2d9e7 )
by
unknown
42:28
created

testInheritanceConfiguration()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 23
nc 3
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Unit\DependencyInjection;
4
5
use OroCRM\Bundle\MagentoBundle\DependencyInjection\OroCRMMagentoExtension;
6
use OroCRM\Bundle\MagentoBundle\DependencyInjection\Configuration;
7
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
class OroCRMMagentoExtensionTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function testConfigPassedToConnectors()
13
    {
14
        $config = [
15
            'sync_settings' => [
16
                'mistiming_assumption_interval' => '10 minutes',
17
                'initial_import_step_interval' => '1 day',
18
                'region_sync_interval' => '1 day',
19
                'skip_ssl_verification' => false
20
            ]
21
        ];
22
23
        $container = new ContainerBuilder();
24
        $extension = new OroCRMMagentoExtension();
25
26
        $extension->load(['oro_crm_magento' => $config], $container);
27
28
        $tagged = $container->findTaggedServiceIds('orocrm_magento.bundle_config.aware');
29
30
        $missedConfigDefinitions = [];
31
        foreach (array_keys($tagged) as $serviceId) {
32
            $definition = $container->getDefinition($serviceId);
33
34
            $definition->getArguments();
0 ignored issues
show
Unused Code introduced by
The call to the method Symfony\Component\Depend...inition::getArguments() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
35
            $configArguments = array_filter(
36
                $definition->getArguments(),
37
                function ($arg) use ($config) {
38
                    return $arg === $config;
39
                }
40
            );
41
42
            if (!$configArguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configArguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
                $missedConfigDefinitions[] = $serviceId;
44
            }
45
        }
46
47
        $this->assertEquals([], $missedConfigDefinitions, 'Should contain config array');
48
    }
49
50
    /**
51
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
52
     * @expectedExceptionMessage Strategy configuration contains unknown fields "unknown_field"
53
     */
54
    public function testInvalidAccountDiscoveryConfiguration()
55
    {
56
        $config = [
57
            [
58
                'account_discovery' => [
59
                    'fields'   => [
60
                        'field1' => null,
61
                        'field2' => [
62
                            'field2.1' => null
63
                        ]
64
                    ],
65
                    'strategy' => [
66
                        'field1'        => 'some',
67
                        'unknown_field' => 'other'
68
                    ]
69
                ]
70
            ],
71
            null
72
        ];
73
        $container = new ContainerBuilder();
74
        $extension = new OroCRMMagentoExtension();
75
        $extension->load($config, $container);
76
    }
77
78
    /**
79
     * @dataProvider inheritanceConfigurationDataProvider
80
     *
81
     * @param array $config
82
     * @param array $resultConfig
83
     */
84
    public function testInheritanceConfiguration(array $config, array $resultConfig)
85
    {
86
        $container = new ContainerBuilder();
87
        $extension = new OroCRMMagentoExtension();
88
        $extension->load($config, $container);
89
        $services = $container->findTaggedServiceIds('orocrm_magento.bundle_config.aware');
90
        foreach ($services as $serviceId => $tagAttributes) {
91
            $tagAttributes = reset($tagAttributes);
92
            if (isset($tagAttributes['argument_number'])) {
93
                $serviceDefinition = $container->getDefinition($serviceId);
94
                $serviceArgument   = $serviceDefinition->getArgument($tagAttributes['argument_number']);
95
                self::assertArrayHasKey(Configuration::DISCOVERY_NODE, $serviceArgument);
96
                self::assertArrayHasKey(
97
                    Configuration::DISCOVERY_OPTIONS_KEY,
98
                    $serviceArgument[Configuration::DISCOVERY_NODE]
99
                );
100
                self::assertArrayHasKey(
101
                    Configuration::DISCOVERY_STRATEGY_KEY,
102
                    $serviceArgument[Configuration::DISCOVERY_NODE]
103
                );
104
                self::assertArrayHasKey(
105
                    Configuration::DISCOVERY_FIELDS_KEY,
106
                    $serviceArgument[Configuration::DISCOVERY_NODE]
107
                );
108
                self::assertEquals(
109
                    $serviceArgument[Configuration::DISCOVERY_NODE],
110
                    $resultConfig[Configuration::DISCOVERY_NODE]
111
                );
112
            }
113
        }
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function inheritanceConfigurationDataProvider()
120
    {
121
        return [
122
            'one config block' => [
123
                'config'       => [
124
                    null,
125
                    [
126
                        'account_discovery' => [
127
                            'fields'  => ['field1' => null, 'field2' => null, 'field3' => null,],
128
                            'options' => ['match' => 'first', 'empty' => false,]
129
                        ]
130
                    ]
131
                ],
132
                'resultConfig' => [
133
                    'account_discovery' => [
134
                        'fields'   => ['field1' => null, 'field2' => null, 'field3' => null,],
135
                        'options'  => ['match' => 'first', 'empty' => false,],
136
                        'strategy' => []
137
                    ]
138
                ]
139
            ],
140
            'two config block' => [
141
                'config'       => [
142
                    null,
143
                    [
144
                        'account_discovery' => [
145
                            'fields'  => ['field1' => null],
146
                            'options' => ['match' => 'first', 'empty' => false]
147
                        ]
148
                    ],
149
                    [
150
                        'account_discovery' => [
151
                            'fields'  => ['field1' => null, 'field2' => null, 'field3' => null],
152
                            'options' => ['match' => 'first', 'empty' => false]
153
                        ]
154
                    ]
155
                ],
156
                'resultConfig' => [
157
                    'account_discovery' => [
158
                        'fields'   => ['field1' => null,],
159
                        'options'  => ['match' => 'first', 'empty' => false,],
160
                        'strategy' => []
161
                    ]
162
                ]
163
            ]
164
        ];
165
    }
166
}
167