Passed
Push — master ( d59e62...785ed6 )
by Vladimir
02:26
created

testLoadOnlyCliEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 40
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 54
rs 9.28

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bicycle\TesseractBridgeBundle\Tests\DependencyInjection;
4
5
use Bicycle\Tesseract\Bridge;
6
use Bicycle\TesseractBridgeBundle\DataCollector\TesseractBridgeDataCollector;
7
use Bicycle\TesseractBridgeBundle\DependencyInjection\BicycleTesseractBridgeExtension;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
11
use Symfony\Component\DependencyInjection;
12
13
class BicycleTesseractBridgeExtensionTest extends TestCase
14
{
15
    /** @var BicycleTesseractBridgeExtension */
16
    private BicycleTesseractBridgeExtension $testInstance;
17
18
    /** @var DependencyInjection\ContainerBuilder|MockObject */
19
    private $containerMock;
20
21
    /** @var DependencyInjection\Definition|MockObject */
22
    private $serviceDefMock;
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected function setUp(): void
28
    {
29
        parent::setUp();
30
31
        $this->testInstance = new BicycleTesseractBridgeExtension();
32
        $this->containerMock = $this
33
            ->getMockBuilder(DependencyInjection\ContainerBuilder::class)
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
        $this->serviceDefMock = $this
37
            ->getMockBuilder(DependencyInjection\Definition::class)
38
            ->disableOriginalConstructor()
39
            ->getMock();
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function incorrectConfigDataProvider(): array
46
    {
47
        return [
48
            [
49
                [],
50
                '/^The child ' .
51
                    '(config "integrations" under)|(node "integrations" at path)' .
52
                    ' "bicycle_tesseract_bridge"| must be configured\.$/',
53
            ],
54
            [
55
                ['integrations' => []],
56
                '/^The child ' .
57
                    '(config "integrations" under)|(node "integrations" at path)' .
58
                    ' "bicycle_tesseract_bridge" must be configured\.$/',
59
            ],
60
            [
61
                ['bicycle_tesseract_bridge' => []],
62
                '/^The child ' .
63
                    '(config "integrations" under)|(node "integrations" at path)' .
64
                    ' "bicycle_tesseract_bridge" must be configured\.$/',
65
            ],
66
            [
67
                ['bicycle_tesseract_bridge' => ['integrations' => []]],
68
                '/^Invalid configuration for path "bicycle_tesseract_bridge\.integrations": ' .
69
                    'At least one integration must be enabled$/',
70
            ],
71
            [
72
                ['bicycle_tesseract_bridge' => ['integrations' => ['cli' => [], 'ffi' => []]]],
73
                '/^Invalid configuration for path "bicycle_tesseract_bridge\.integrations": ' .
74
                    'Enabled integrations must have configured path$/',
75
            ],
76
            [
77
                ['bicycle_tesseract_bridge' => ['integrations' => ['cli' => ['path' => 'test'], 'ffi' => []]]],
78
                '/^Invalid configuration for path "bicycle_tesseract_bridge\.integrations": ' .
79
                    'Enabled integrations must have configured path$/',
80
            ],
81
        ];
82
    }
83
84
    /**
85
     * @dataProvider incorrectConfigDataProvider
86
     *
87
     * @param array  $configuration
88
     * @param string $exceptionMessage
89
     */
90
    public function testIncorrectConfiguration(array $configuration, string $exceptionMessage): void
91
    {
92
        $this->expectException(InvalidConfigurationException::class);
93
        $this->expectExceptionMessageMatches($exceptionMessage);
94
        $this->testInstance->load($configuration, $this->containerMock);
95
    }
96
97
    public function testLoad(): void
98
    {
99
        $cliPath = 'cli test path';
100
        $ffiPath = 'ffi test path';
101
        $config = [
102
            'bicycle_tesseract_bridge' => [
103
                'integrations' => [
104
                    'cli' => [
105
                        'enabled' => true,
106
                        'path' => $cliPath,
107
                    ],
108
                    'ffi' => [
109
                        'enabled' => true,
110
                        'path' => $ffiPath,
111
                    ],
112
                ],
113
            ],
114
        ];
115
116
        $bridgeCliDefMock = clone $this->serviceDefMock;
117
        $bridgeFfiDefMock = clone $this->serviceDefMock;
118
119
        $collectorDefMock = clone $this->serviceDefMock;
120
        $collectorDefMock
121
            ->expects(self::exactly(4))
122
            ->method('replaceArgument')
123
            ->withConsecutive([2, $config['bicycle_tesseract_bridge']], [0, $bridgeCliDefMock], [1, $bridgeFfiDefMock]);
124
125
        $this
126
            ->serviceDefMock
127
            ->expects(self::once())
128
            ->method('replaceArgument')
129
            ->with(0, ['binary_path' => $cliPath, 'library_path' => $ffiPath]);
130
131
        $this->containerMock->expects(self::once())->method('fileExists')->willReturn(false);
132
        $this->containerMock
133
            ->expects(self::exactly(4))
134
            ->method('getDefinition')
135
            ->withConsecutive(
136
                [TesseractBridgeDataCollector::class],
137
                ['bicycle.tesseract_bridge.integrations.cli'],
138
                ['bicycle.tesseract_bridge.integrations.ffi'],
139
                [Bridge\Configuration::class]
140
            )
141
            ->willReturnOnConsecutiveCalls(
142
                $collectorDefMock,
143
                $bridgeCliDefMock,
144
                $bridgeFfiDefMock,
145
                $this->serviceDefMock
146
            );
147
148
        self::assertInstanceOf(DependencyInjection\Extension\Extension::class, $this->testInstance);
149
        $this->testInstance->load($config, $this->containerMock);
150
    }
151
152
    public function testLoadOnlyCliEnabled(): void
153
    {
154
        $cliPath = 'cli test path';
155
        $config = [
156
            'bicycle_tesseract_bridge' => [
157
                'integrations' => [
158
                    'cli' => [
159
                        'enabled' => true,
160
                        'path' => $cliPath,
161
                    ],
162
                    'ffi' => [
163
                        'enabled' => false,
164
                        'path' => null,
165
                    ],
166
                ],
167
            ],
168
        ];
169
170
        $bridgeCliDefMock = clone $this->serviceDefMock;
171
172
        $collectorDefMock = clone $this->serviceDefMock;
173
        $collectorDefMock
174
            ->expects(self::exactly(3))
175
            ->method('replaceArgument')
176
            ->withConsecutive([2, $config['bicycle_tesseract_bridge']], [0, $bridgeCliDefMock]);
177
178
        $this
179
            ->serviceDefMock
180
            ->expects(self::once())
181
            ->method('replaceArgument')
182
            ->with(0, ['binary_path' => $cliPath]);
183
184
        $this->containerMock->expects(self::once())->method('fileExists')->willReturn(false);
185
        $this->containerMock
186
            ->expects(self::exactly(3))
187
            ->method('getDefinition')
188
            ->withConsecutive(
189
                [TesseractBridgeDataCollector::class],
190
                ['bicycle.tesseract_bridge.integrations.cli'],
191
                [Bridge\Configuration::class]
192
            )
193
            ->willReturnOnConsecutiveCalls(
194
                $collectorDefMock,
195
                $bridgeCliDefMock,
196
                $this->serviceDefMock
197
            );
198
        $this
199
            ->containerMock
200
            ->expects(self::once())
201
            ->method('removeDefinition')
202
            ->with('bicycle.tesseract_bridge.integrations.ffi');
203
204
        self::assertInstanceOf(DependencyInjection\Extension\Extension::class, $this->testInstance);
205
        $this->testInstance->load($config, $this->containerMock);
206
    }
207
208
    public function testLoadOnlyFfiEnabled(): void
209
    {
210
        $ffiPath = 'ffi test path';
211
        $config = [
212
            'bicycle_tesseract_bridge' => [
213
                'integrations' => [
214
                    'cli' => [
215
                        'enabled' => false,
216
                        'path' => null,
217
                    ],
218
                    'ffi' => [
219
                        'enabled' => true,
220
                        'path' => $ffiPath,
221
                    ],
222
                ],
223
            ],
224
        ];
225
226
        $bridgeFfiDefMock = clone $this->serviceDefMock;
227
228
        $collectorDefMock = clone $this->serviceDefMock;
229
        $collectorDefMock
230
            ->expects(self::exactly(3))
231
            ->method('replaceArgument')
232
            ->withConsecutive([2, $config['bicycle_tesseract_bridge']], [1, $bridgeFfiDefMock]);
233
234
        $this
235
            ->serviceDefMock
236
            ->expects(self::once())
237
            ->method('replaceArgument')
238
            ->with(0, ['library_path' => $ffiPath]);
239
240
        $this->containerMock->expects(self::once())->method('fileExists')->willReturn(false);
241
        $this->containerMock
242
            ->expects(self::exactly(3))
243
            ->method('getDefinition')
244
            ->withConsecutive(
245
                [TesseractBridgeDataCollector::class],
246
                ['bicycle.tesseract_bridge.integrations.ffi'],
247
                [Bridge\Configuration::class]
248
            )
249
            ->willReturnOnConsecutiveCalls(
250
                $collectorDefMock,
251
                $bridgeFfiDefMock,
252
                $this->serviceDefMock
253
            );
254
        $this
255
            ->containerMock
256
            ->expects(self::once())
257
            ->method('removeDefinition')
258
            ->with('bicycle.tesseract_bridge.integrations.cli');
259
260
        self::assertInstanceOf(DependencyInjection\Extension\Extension::class, $this->testInstance);
261
        $this->testInstance->load($config, $this->containerMock);
262
    }
263
}
264