Passed
Pull Request — master (#407)
by Kirill
11:08 queued 03:58
created

OptionsTraitTest::getOptionsListForProcessing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 25
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.52
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit\Config\DTO\Traits;
6
7
use PHPUnit\Framework\MockObject\MockObject;
8
use Spiral\Storage\Exception\ConfigException;
9
use Spiral\Storage\Config\DTO\FileSystemInfo\OptionsBasedInterface;
10
use Spiral\Storage\Config\DTO\Traits\OptionsTrait;
11
use Spiral\Tests\Storage\Unit\UnitTestCase;
12
13
class OptionsTraitTest extends UnitTestCase
14
{
15
    private const BASIC_OPTIONS = [
16
        'option1' => 'optionVal1',
17
        'option2' => 'optionVal2',
18
        'option3' => 'optionVal3',
19
    ];
20
21
    /**
22
     * @throws \ReflectionException
23
     */
24
    public function testHasOption(): void
25
    {
26
        $trait = $this->buildBasicOptions();
27
28
        foreach (self::BASIC_OPTIONS as $option => $optionVal) {
29
            $this->assertTrue($trait->hasOption($option));
30
        }
31
32
        $this->assertFalse($trait->hasOption('optionMissed'));
33
    }
34
35
    /**
36
     * @throws \ReflectionException
37
     */
38
    public function testGetOption(): void
39
    {
40
        $trait = $this->buildBasicOptions();
41
42
        foreach (self::BASIC_OPTIONS as $option => $optionVal) {
43
            $this->assertEquals($optionVal, $trait->getOption($option));
44
        }
45
46
        $this->assertNull($trait->getOption('optionMissed'));
47
    }
48
49
    /**
50
     * @dataProvider getOptionsListForHasType
51
     *
52
     * @param string $optionLabel
53
     * @param $optionVal
54
     * @param string $type
55
     * @param bool $expectedResult
56
     *
57
     * @throws \ReflectionException
58
     */
59
    public function testIsOptionHasRequiredType(
60
        string $optionLabel,
61
        $optionVal,
62
        string $type,
63
        bool $expectedResult
64
    ): void {
65
        $trait = $this->buildBasicOptions();
66
67
        $this->assertEquals(
68
            $expectedResult,
69
            $this->callNotPublicMethod($trait, 'isOptionHasRequiredType', [$optionLabel, $optionVal, $type])
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

69
            /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($trait, 'isOptionHasRequiredType', [$optionLabel, $optionVal, $type])

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
        );
71
    }
72
73
    /**
74
     * @throws \ReflectionException
75
     */
76
    public function testIsOptionHasRequiredTypeUnknownTypeFailed(): void
77
    {
78
        $trait = $this->buildBasicOptions();
79
80
        $optionLabel = 'someLabel';
81
        $optionType = 'missedType';
82
83
        $this->expectException(ConfigException::class);
84
        $this->expectExceptionMessage(
85
            \sprintf('Unknown option type detected for option `%s`: %s', $optionLabel, $optionType)
86
        );
87
88
        $this->callNotPublicMethod($trait, 'isOptionHasRequiredType', [$optionLabel, 'someVal', $optionType]);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($trait, 'isOptionHasRequiredType', [$optionLabel, 'someVal', $optionType]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
    }
90
91
    /**
92
     * @dataProvider getOptionsListForProcessing
93
     *
94
     * @param $optionVal
95
     * @param string $type
96
     * @param $expectedVal
97
     *
98
     * @throws \ReflectionException
99
     */
100
    public function testProcessOptionByType($optionVal, string $type, $expectedVal): void
101
    {
102
        $trait = $this->buildBasicOptions();
103
104
        $this->assertEquals(
105
            $expectedVal,
106
            $this->callNotPublicMethod($trait, 'processOptionByType', [$optionVal, $type])
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
            /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($trait, 'processOptionByType', [$optionVal, $type])

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107
        );
108
    }
109
110
    /**
111
     * @throws \ReflectionException
112
     */
113
    public function testValidateRequiredOptionsPassed(): void
114
    {
115
        $requiredOptions = array_keys(self::BASIC_OPTIONS);
116
117
        $trait = $this->buildBasicOptions();
118
119
        $this->callNotPublicMethod(
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
            $trait,
121
            'validateRequiredOptions',
122
            [$requiredOptions, static::BASIC_OPTIONS]
123
        );
124
125
        $this->expectNotToPerformAssertions();
126
    }
127
128
    /**
129
     * @throws \ReflectionException
130
     */
131
    public function testValidateRequiredOptionsFailed(): void
132
    {
133
        $reqOption = 'reqOption';
134
        $msgPostfix = ' some postfix';
135
136
        $requiredOptions = array_keys(self::BASIC_OPTIONS);
137
        $requiredOptions[] = $reqOption;
138
139
        $trait = $this->buildBasicOptions();
140
141
        $this->expectException(ConfigException::class);
142
        $this->expectExceptionMessage(\sprintf('Option `%s` not detected%s', $reqOption, $msgPostfix));
143
144
        $this->callNotPublicMethod(
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

144
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
145
            $trait,
146
            'validateRequiredOptions',
147
            [$requiredOptions, static::BASIC_OPTIONS, $msgPostfix]
148
        );
149
    }
150
151
    public function getOptionsListForHasType(): array
152
    {
153
        $result = [
154
            ['wrongIntOption', 'someStr', OptionsBasedInterface::INT_TYPE, false],
155
            ['floatOption', 1.0, OptionsBasedInterface::FLOAT_TYPE, true],
156
            ['strOption', '1', OptionsBasedInterface::STRING_TYPE, true],
157
            ['wrongStrOption', 1, OptionsBasedInterface::STRING_TYPE, false],
158
            ['wrongBoolOption', '4', OptionsBasedInterface::BOOL_TYPE, false],
159
            ['wrongBoolOption2', 5, OptionsBasedInterface::BOOL_TYPE, false],
160
            ['arrayOption', [1, 2], OptionsBasedInterface::ARRAY_TYPE, true],
161
            ['arrayOption', [], OptionsBasedInterface::ARRAY_TYPE, true],
162
            ['arrayOption', 4, OptionsBasedInterface::ARRAY_TYPE, false],
163
            ['arrayOption', true, OptionsBasedInterface::ARRAY_TYPE, false],
164
        ];
165
166
        foreach ([0, 1, 3, '4', '15'] as $key => $intVal) {
167
            $result[] = ['intOption' . $key, $intVal, OptionsBasedInterface::INT_TYPE, true];
168
        }
169
170
        foreach ([0, 1, '0', '1', true, false] as $key => $boolVal) {
171
            $result[] = ['boolOption' . $key, $boolVal, OptionsBasedInterface::BOOL_TYPE, true];
172
        }
173
174
        $mixedVals = [
175
            1,
176
            5.0,
177
            '0',
178
            'some string',
179
            false,
180
            true,
181
            [],
182
            new \DateTimeImmutable(),
183
            [3, 4],
184
        ];
185
186
        foreach ($mixedVals as $key => $mixedVal) {
187
            $result[] = ['mixedOption' . $key, $mixedVal, OptionsBasedInterface::MIXED_TYPE, true];
188
        }
189
190
        return $result;
191
    }
192
193
    public function getOptionsListForProcessing(): array
194
    {
195
        $date = new \DateTimeImmutable();
196
        return [
197
            ['1', OptionsBasedInterface::INT_TYPE, 1],
198
            ['1.35', OptionsBasedInterface::INT_TYPE, 1],
199
            [7, OptionsBasedInterface::INT_TYPE, 7],
200
            [5.00, OptionsBasedInterface::INT_TYPE, 5],
201
            ['1.00', OptionsBasedInterface::FLOAT_TYPE, 1.00],
202
            [7, OptionsBasedInterface::FLOAT_TYPE, 7.00],
203
            [5.00, OptionsBasedInterface::FLOAT_TYPE, 5.00],
204
            [5.0, OptionsBasedInterface::STRING_TYPE, '5'],
205
            [0, OptionsBasedInterface::STRING_TYPE, '0'],
206
            ['some string', OptionsBasedInterface::STRING_TYPE, 'some string'],
207
            [true, OptionsBasedInterface::STRING_TYPE, '1'],
208
            [true, OptionsBasedInterface::BOOL_TYPE, true],
209
            ['true', OptionsBasedInterface::BOOL_TYPE, true],
210
            [1, OptionsBasedInterface::BOOL_TYPE, true],
211
            [18.0, OptionsBasedInterface::BOOL_TYPE, true],
212
            ['someVal', OptionsBasedInterface::BOOL_TYPE, true],
213
            ['', OptionsBasedInterface::BOOL_TYPE, false],
214
            [0, OptionsBasedInterface::BOOL_TYPE, false],
215
            [null, OptionsBasedInterface::BOOL_TYPE, false],
216
            [[1, 2], OptionsBasedInterface::ARRAY_TYPE, [1, 2]],
217
            [null, OptionsBasedInterface::MIXED_TYPE, null],
218
            [[1], OptionsBasedInterface::MIXED_TYPE, [1]],
219
            [$date, OptionsBasedInterface::MIXED_TYPE, $date],
220
        ];
221
    }
222
223
    /**
224
     * @param array|string[] $options
225
     *
226
     * @return MockObject|OptionsTrait
227
     *
228
     * @throws \ReflectionException
229
     */
230
    private function buildBasicOptions(array $options = self::BASIC_OPTIONS)
231
    {
232
        $trait = $this->getMockForTrait(OptionsTrait::class);
233
234
        $this->setNotPublicProperty($trait, 'options', $options);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:setNotPublicProperty() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

234
        /** @scrutinizer ignore-deprecated */ $this->setNotPublicProperty($trait, 'options', $options);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
235
236
        return $trait;
237
    }
238
}
239