Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General: Load ENV as configuration #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Lunr/Core/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ public function load_file(string $identifier): void
$this->size_invalid = TRUE;
}

/**
* Load environment variables as configuration.
*
* @return void
*/
public function load_environment(): void
{
$config = $this->config;

foreach ($_ENV as $key => $value)
{
$config[strtolower($key)] = $value;
}

if (!empty($config))
{
$config = $this->convert_array_to_class($config);
}

$this->config = $config;
$this->size_invalid = TRUE;
}

/**
* Convert an input array recursively into a Configuration class hierarchy.
*
Expand Down
88 changes: 88 additions & 0 deletions src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* This file contains the ConfigurationLoadFileTest class.
*
* SPDX-FileCopyrightText: Copyright 2011 M2mobi B.V., Amsterdam, The Netherlands
* SPDX-FileCopyrightText: Copyright 2022 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Core\Tests;

Check failure on line 11 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

There must be one blank line after the namespace declaration

Check failure on line 11 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Header blocks must be separated by a single blank line


/**

Check failure on line 14 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

There must be exactly one blank line before the class comment
* This tests loading configuration files via the Configuration class.
*
* @covers \Lunr\Core\Configuration
*/
class ConfigurationLoadEnvironmentTest extends ConfigurationTest
{

/**
* TestCase Constructor.
*/
public function setUp(): void
{
$this->setUpArray($this->construct_test_array());
}

/**
* Test loading the environment correctly.
*/
public function testLoadEnvironment(): void
{
$_ENV = [];

$_ENV['LOAD_ONE'] = 'Value';
$_ENV['LOAD_TWO'] = 'String';

$this->class->load_environment();

$this->config['load_one'] = 'Value';
$this->config['load_two'] = 'String';

$this->assertEquals($this->config, $this->class->toArray());

unset($_ENV['LOAD_ONE']);
unset($_ENV['LOAD_TWO']);
}

/**
* Test loading a correct config file.
*/
public function testLoadFileOverwritesValues(): void
{
$_ENV = [];

$_ENV['TEST1'] = 'Test';

$this->class->load_environment();

$config = [];

Check warning on line 62 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Equals sign not aligned with surrounding assignments; expected 10 spaces but found 19 spaces
$config['test1'] = 'Test';

Check warning on line 63 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Equals sign not aligned with surrounding assignments; expected 1 space but found 10 spaces
$config['test2'] = $this->config['test2'];

Check warning on line 64 in src/Lunr/Core/Tests/ConfigurationLoadEnvironmentTest.php

View workflow job for this annotation

GitHub Actions / php-tests / phpcs / PHPCS

Equals sign not aligned with surrounding assignments; expected 1 space but found 10 spaces

$this->assertEquals($config, $this->class->toArray());

unset($_ENV['TEST1']);
}

/**
* Test that loading a file invalidates the cached size value.
*/
public function testLoadFileInvalidatesSize(): void
{
$property = $this->reflection->getProperty('size_invalid');
$property->setAccessible(TRUE);

$this->assertFalse($property->getValue($this->class));

$this->class->load_file('correct');

$this->assertTrue($property->getValue($this->class));
}

}

?>
Loading