Skip to content

Commit

Permalink
General: Load ENV as configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Nov 1, 2024
1 parent 156088c commit 5f353cb
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
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));
}

}

?>

0 comments on commit 5f353cb

Please sign in to comment.