Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Added test for int cast and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Radovan Kepák committed Oct 26, 2021
1 parent 6e2275a commit 80ac9f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
8 changes: 4 additions & 4 deletions tests/DI/EnvironmentAdapter.hidden.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require __DIR__ . '/../bootstrap.php';

define('TEMP_FILE', getTempDir() . '/cfg.env');

Mallgroup\setenv('SERVICE_ARRAY', 'one|two');
Mallgroup\setenv('SERVICE_ARRAY', '1|2');

$config = new Config\Loader();
$config->addAdapter('env', EnvironmentAdapter::class);
Expand All @@ -28,7 +28,7 @@ service_password: ::string(secret_password, true)
service_port: ::int(1234)
service_nonstring: ::nonstring(1234)
service_active: ::bool(\'false\')
service_array: ::array(hidden=true)
service_array: ::array(hidden: true, cast: int)
';
$data = $config->load(Tester\FileMock::create($cfg, 'env'));
Assert::equal([
Expand All @@ -45,7 +45,7 @@ Assert::equal([
'service_active' => false,
'service_array' => new Statement(
'Mallgroup\Environment::array',
['name' => 'SERVICE_ARRAY', 'cast' => 'string', 'separator' => '|']
['name' => 'SERVICE_ARRAY', 'cast' => 'int', 'separator' => '|']
),
]
]
Expand All @@ -62,7 +62,7 @@ Assert::match(
service_port: 1234
service_nonstring: "1234"
service_active: false
service_array: ::array(hidden: true, separator: |, cast: string)
service_array: ::array(hidden: true, separator: |, cast: int)
EOD
,
file_get_contents(TEMP_FILE)
Expand Down
47 changes: 29 additions & 18 deletions tests/DI/EnvironmentAdapter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require __DIR__ . '/../bootstrap.php';
define('TEMP_FILE', getTempDir() . '/cfg.env');

Mallgroup\setenv('SERVICE_ARRAY', 'one|two');
Mallgroup\setenv('SERVICE_ARRAY_INT', '1|2');

$config = new Config\Loader();
$config->addAdapter('env', EnvironmentAdapter::class);
Expand All @@ -29,29 +30,35 @@ service_port: ::int(1234)
service_nonstring: ::nonstring(1234)
service_active: ::bool(\'false\')
service_array: ::array(|)
service_array_int: ::array(cast: int)
';
$data = $config->load(Tester\FileMock::create($cfg, 'env'));
Assert::equal([
'parameters' => [
'env' =>
[
'service_user' => 'secret_user',
'service_password' => new Statement(
'Mallgroup\Environment::string',
['name' => 'SERVICE_PASSWORD', 'cast' => 'string', 'default' => 'secret_password']
),
'service_port' => 1234,
'service_nonstring' => '1234',
'service_active' => false,
'service_array' => ['one', 'two'],
]
]
], $data);
'parameters' => [
'env' =>
[
'service_user' => 'secret_user',
'service_password' => new Statement(
'Mallgroup\Environment::string',
[
'name' => 'SERVICE_PASSWORD',
'cast' => 'string',
'default' => 'secret_password'
]
),
'service_port' => 1234,
'service_nonstring' => '1234',
'service_active' => false,
'service_array' => ['one', 'two'],
'service_array_int' => [1, 2],
]
]
], $data);


$config->save($data, TEMP_FILE);
Assert::match(
<<<'EOD'
<<<'EOD'
# generated by Nette
service_user: secret_user
Expand All @@ -62,7 +69,11 @@ Assert::match(
service_array:
- one
- two
service_array_int:
- 1
- 2
EOD
,
file_get_contents(TEMP_FILE)
,
file_get_contents(TEMP_FILE)
);

0 comments on commit 80ac9f1

Please sign in to comment.