forked from nbgrp/env-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCastEnvVarProcessorTest.php
More file actions
133 lines (111 loc) · 3.92 KB
/
ArrayCastEnvVarProcessorTest.php
File metadata and controls
133 lines (111 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace Nbgrp\Tests\EnvBundle;
use Nbgrp\EnvBundle\ArrayCastEnvVarProcessor;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* @internal
*/
#[CoversClass(ArrayCastEnvVarProcessor::class)]
final class ArrayCastEnvVarProcessorTest extends TestCase
{
#[DataProvider('provideSuccessCases')]
public function testSuccess(string $prefix, array $envValue, array $expected): void
{
$processor = new ArrayCastEnvVarProcessor();
self::assertSame($expected, $processor->getEnv($prefix, 'test-string', static fn (): array => $envValue));
}
/**
* @return \Generator<array{string, array, array}>
*/
public static function provideSuccessCases(): iterable
{
yield 'bool-array' => [
'bool-array',
[1, 2, 0, -1, 'true', 'false', 'yes', 'no', 'on', 'off', 'any'],
[true, true, false, true, true, false, true, false, true, false, false],
];
yield 'int-array' => [
'int-array',
['0', '1', '-2.0', '3.5', 0, 1, -2.0, 3.5],
[0, 1, -2, 3, 0, 1, -2, 3],
];
yield 'float-array' => [
'float-array',
[0.0, -0.0, 1.1, -2.0, 3.5, '0.0', '-0.0', '1.1', '-2.0', '3.5'],
[0.0, 0.0, 1.1, -2.0, 3.5, 0.0, 0.0, 1.1, -2.0, 3.5],
];
yield 'base64-array' => [
'base64-array',
['ZW5jb2RlZA==', 'ZW5jb2RlZDI'],
['encoded', 'encoded2'],
];
yield 'base64url-array' => [
'base64url-array',
['PD9lbmM_Pg==', 'PDw_MT8-Pg'],
['<?enc?>', '<<?1?>>'],
];
yield 'string-array' => [
'string-array',
[1, 2, 3, true, false],
['1', '2', '3', '1', ''],
];
yield 'bool-array with keys' => [
'bool-array',
['t' => 1, 'f' => 0],
['t' => true, 'f' => false],
];
}
#[DataProvider('provideInvalidNumericCases')]
public function testInvalidNumeric(string $prefix, array $envValue, string $expectedMessageFormat): void
{
$processor = new ArrayCastEnvVarProcessor();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(\sprintf($expectedMessageFormat, 'DM'));
$processor->getEnv($prefix, 'DM', static fn (): array => $envValue);
}
/**
* @return \Generator<array{string, array, string}>
*/
public static function provideInvalidNumericCases(): iterable
{
yield [
'int-array',
['0', '1', 'NaN'],
'Non-numeric member of environment variable "%s" cannot be cast to int.',
];
yield [
'float-array',
['0.0', '1.0', '1.0.1'],
'Non-numeric member of environment variable "%s" cannot be cast to float.',
];
}
#[DataProvider('provideInvalidBase64Cases')]
public function testInvalidBase64(string $prefix, array $envValue, string $expectedMessageFormat): void
{
$processor = new ArrayCastEnvVarProcessor();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(\sprintf($expectedMessageFormat, 'DM'));
$processor->getEnv($prefix, 'DM', static fn (): array => $envValue);
}
/**
* @return \Generator<array{string, array, string}>
*/
public static function provideInvalidBase64Cases(): iterable
{
yield [
'base64-array',
['Z!==', 'Z!'],
'Environment variable "%s" must be a valid base64 string.',
];
yield [
'base64url-array',
['-=', '_!'],
'Environment variable "%s" must be a valid base64url string.',
];
}
}