| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the Micro framework package. |
||
| 7 | * |
||
| 8 | * (c) Stanislau Komar <[email protected]> |
||
| 9 | * |
||
| 10 | * For the full copyright and license information, please view the LICENSE |
||
| 11 | * file that was distributed with this source code. |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace Micro\Framework\Kernel\Test\Unit\Configuration; |
||
| 15 | |||
| 16 | use Micro\Framework\Kernel\Configuration\ApplicationConfigurationInterface; |
||
| 17 | use Micro\Framework\Kernel\Configuration\DefaultApplicationConfiguration; |
||
| 18 | use Micro\Framework\Kernel\Configuration\Exception\InvalidConfigurationException; |
||
| 19 | use PHPUnit\Framework\TestCase; |
||
| 20 | |||
| 21 | class DefaultApplicationConfigurationTest extends TestCase |
||
| 22 | { |
||
| 23 | private ApplicationConfigurationInterface $configuration; |
||
| 24 | |||
| 25 | public const BOOLEAN_VALUES_TRUE = [ |
||
| 26 | 'BOOLEAN_TRUE_STRING' => 'true', |
||
| 27 | 'BOOLEAN_TRUE_STRING_ON' => 'on', |
||
| 28 | 'BOOLEAN_TRUE_INT' => 1, |
||
| 29 | 'BOOLEAN_TRUE_REAL' => true, |
||
| 30 | ]; |
||
| 31 | |||
| 32 | public const BOOLEAN_VALUES_FALSE = [ |
||
| 33 | 'BOOLEAN_FALSE_STRING' => 'false', |
||
| 34 | 'BOOLEAN_FALSE_STRING_OFF' => 'off', |
||
| 35 | 'BOOLEAN_FALSE_INT' => 0, |
||
| 36 | 'BOOLEAN_FALSE_REAL' => false, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | public const BOOLEAN_VALUES_DEFAULT_TEST = [ |
||
| 40 | 'BOOLEAN_NULL' => null, |
||
| 41 | ]; |
||
| 42 | |||
| 43 | public const BOOLEAN_VALUES_INVALID = [ |
||
| 44 | 'BOOLEAN_INVALID' => 'it is invalid value.', |
||
| 45 | 'VALUE_SHOULD_BE_NON_EMPTY' => null, |
||
| 46 | ]; |
||
| 47 | |||
| 48 | protected function setUp(): void |
||
| 49 | { |
||
| 50 | $config = array_merge( |
||
| 51 | self::BOOLEAN_VALUES_TRUE, |
||
| 52 | self::BOOLEAN_VALUES_FALSE, |
||
| 53 | self::BOOLEAN_VALUES_INVALID, |
||
| 54 | self::BOOLEAN_VALUES_DEFAULT_TEST, |
||
| 55 | ); |
||
| 56 | |||
| 57 | $this->configuration = new DefaultApplicationConfiguration($config); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testGetTrue() |
||
| 61 | { |
||
| 62 | $trueKeys = array_keys(self::BOOLEAN_VALUES_TRUE); |
||
| 63 | foreach ($trueKeys as $key) { |
||
| 64 | $this->assertTrue($this->configuration->get($key, false, false)); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | public function testGetFalse() |
||
| 69 | { |
||
| 70 | $trueKeys = array_keys(self::BOOLEAN_VALUES_FALSE); |
||
| 71 | foreach ($trueKeys as $key) { |
||
| 72 | $this->assertFalse($this->configuration->get($key, true, false)); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testGetDefaultTrue() |
||
| 77 | { |
||
| 78 | $keys = array_keys(self::BOOLEAN_VALUES_DEFAULT_TEST); |
||
| 79 | |||
| 80 | foreach ($keys as $key) { |
||
| 81 | $this->assertTrue($this->configuration->get($key, true, false)); |
||
| 82 | $this->assertFalse($this->configuration->get($key, false, false)); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @dataProvider dataProviderExceptionalKeys |
||
| 88 | */ |
||
| 89 | public function testExceptional(string $key, mixed $default) |
||
| 90 | { |
||
| 91 | $this->expectException(InvalidConfigurationException::class); |
||
| 92 | |||
| 93 | var_dump($this->configuration->get($key, $default, false)); |
||
|
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
| 94 | } |
||
| 95 | |||
| 96 | public function dataProviderExceptionalKeys() |
||
| 97 | { |
||
| 98 | return [ |
||
| 99 | ['BOOLEAN_INVALID', false], |
||
| 100 | ['NO_CONFIGURED', null], |
||
| 101 | ['VALUE_SHOULD_BE_NON_EMPTY', null], |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | } |
||
| 105 |