2018-09-13 07:26:42 -05:00
< ? php
/**
* EnvTest.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-09-13 07:26:42 -05:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2018-09-13 07:26:42 -05:00
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests\Unit\Util ;
use LibreNMS\Tests\TestCase ;
2020-07-09 15:03:12 -05:00
use LibreNMS\Util\EnvHelper ;
2018-09-13 07:26:42 -05:00
class EnvTest extends TestCase
{
2023-05-24 22:21:54 +02:00
public function testParseArray () : void
2018-09-13 07:26:42 -05:00
{
putenv ( 'PARSETEST=one,two' );
2020-07-09 15:03:12 -05:00
$this -> assertSame ([ 'one' , 'two' ], EnvHelper :: parseArray ( 'PARSETEST' ), 'Could not parse simple array' );
$this -> assertSame ([ 'default' ], EnvHelper :: parseArray ( 'PARSETESTNOTSET' , 'default' ), 'Did not get default value as expected' );
$this -> assertSame ( null , EnvHelper :: parseArray ( 'PARSETESTNOTSET' ), 'Did not get null as expected when env not set' );
$this -> assertSame ( 3 , EnvHelper :: parseArray ( 'PARSETESTNOTSET' , 3 ), 'Did not get default value (non-array) as expected' );
$this -> assertSame ( 'default' , EnvHelper :: parseArray ( 'PARSETESTNOTSET' , 'default' , [ 'default' ]), 'Did not get default value as expected, excluding it from exploding' );
2018-09-13 07:26:42 -05:00
putenv ( 'PARSETEST=' );
2020-07-09 15:03:12 -05:00
$this -> assertSame ([ '' ], EnvHelper :: parseArray ( 'PARSETEST' , null , []), 'Did not get empty string as expected when env set to empty' );
2018-09-13 07:26:42 -05:00
putenv ( 'PARSETEST=*' );
2020-07-09 15:03:12 -05:00
$this -> assertSame ( '*' , EnvHelper :: parseArray ( 'PARSETEST' , null , [ '*' , '*' ]), 'Did not properly ignore exclude values' );
2018-09-13 07:26:42 -05:00
// clean the environment
putenv ( 'PARSETEST' );
}
2021-08-27 22:48:21 -05:00
2023-05-24 22:21:54 +02:00
public function testSetEnv () : void
2021-08-27 22:48:21 -05:00
{
$this -> assertEquals ( " ONE=one \n TWO=2 \$ \n THREE= \" space space \" \n " , EnvHelper :: setEnv ( " ONE=one \n TWO= \n " , [
'ONE' => 'zero' ,
'TWO' => '2$' ,
'THREE' => 'space space' ,
]));
$this -> assertEquals ( " A=a \n B=b \n C=c \n D=d \n " , EnvHelper :: setEnv ( " #A= \n B=b \n F=blah \n C= \n " , [
'C' => 'c' ,
'D' => 'd' ,
'B' => 'nope' ,
'A' => 'a' ,
], [ 'F' , 'A' ]));
// replace
$this -> assertEquals ( " #COMMENT=something \n COMMENT=else \n " , EnvHelper :: setEnv ( " COMMENT=nothing \n #COMMENT=something " , [
'COMMENT' => 'else' ,
], [ 'COMMENT' ]));
}
2018-09-13 07:26:42 -05:00
}