Skip to content

Commit 3756435

Browse files
committed
[#.x] - added tests for env manager
1 parent 243e081 commit 3756435

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Tests\Unit;
15+
16+
use PHPUnit\Framework\TestCase;
17+
18+
abstract class AbstractUnitTestCase extends TestCase
19+
{
20+
/**
21+
* Return a long series of strings to be used as a password
22+
*
23+
* @return string
24+
*/
25+
public function getStrongPassword(): string
26+
{
27+
return substr(base64_encode(random_bytes(512)), 0, 128);
28+
}
29+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Tests\Unit\Domain\Services\Env\Adapters;
15+
16+
use Phalcon\Api\Domain\Exceptions\InvalidConfigurationArgumentException;
17+
use Phalcon\Api\Domain\Services\Env\Adapters\DotEnv;
18+
use Phalcon\Api\Domain\Services\Env\EnvFactory;
19+
use Phalcon\Api\Domain\Services\Env\EnvManager;
20+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
21+
22+
final class DotEnvTest extends AbstractUnitTestCase
23+
{
24+
private string $envFile;
25+
26+
protected function setUp(): void
27+
{
28+
$this->envFile = EnvManager::appPath()
29+
. '/tests/Fixtures/Domain/Services/Env/'
30+
;
31+
}
32+
33+
public function testLoadSuccess(): void
34+
{
35+
$dotEnv = new DotEnv();
36+
$options = [
37+
'filePath' => $this->envFile,
38+
];
39+
40+
$expected = [
41+
'SAMPLE_STRING' => 'sample_value',
42+
'SAMPLE_INT' => '1',
43+
'SAMPLE_TRUE' => 'true',
44+
'SAMPLE_FALSE' => 'false',
45+
];
46+
$actual = $dotEnv->load($options);
47+
48+
$this->assertArrayHasKey('SAMPLE_STRING', $actual);
49+
$this->assertArrayHasKey('SAMPLE_INT', $actual);
50+
$this->assertArrayHasKey('SAMPLE_TRUE', $actual);
51+
$this->assertArrayHasKey('SAMPLE_FALSE', $actual);
52+
53+
$actualArray = [
54+
'SAMPLE_STRING' => $actual['SAMPLE_STRING'],
55+
'SAMPLE_INT' => $actual['SAMPLE_INT'],
56+
'SAMPLE_TRUE' => $actual['SAMPLE_TRUE'],
57+
'SAMPLE_FALSE' => $actual['SAMPLE_FALSE'],
58+
];
59+
60+
$this->assertSame($expected, $actualArray);
61+
}
62+
63+
public function testLoadExceptionForEmptyFilePath(): void
64+
{
65+
$this->expectException(InvalidConfigurationArgumentException::class);
66+
$this->expectExceptionMessage(
67+
'The .env file does not exist at the specified path'
68+
);
69+
70+
$dotEnv = new DotEnv();
71+
$options = [
72+
'filePath' => '',
73+
];
74+
75+
$dotEnv->load($options);
76+
}
77+
78+
public function testLoadExceptionForMissingFile(): void
79+
{
80+
$this->expectException(InvalidConfigurationArgumentException::class);
81+
$this->expectExceptionMessage(
82+
'The .env file does not exist at the specified path'
83+
);
84+
85+
$dotEnv = new DotEnv();
86+
$options = [
87+
'filePath' => '/does/not/exist/',
88+
];
89+
90+
$dotEnv->load($options);
91+
}
92+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Tests\Unit\Domain\Services\Env;
15+
16+
use Phalcon\Api\Domain\Exceptions\InvalidConfigurationArgumentException;
17+
use Phalcon\Api\Domain\Services\Env\Adapters\DotEnv;
18+
use Phalcon\Api\Domain\Services\Env\EnvFactory;
19+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
20+
21+
final class EnvFactoryTest extends AbstractUnitTestCase
22+
{
23+
public function testLoad(): void
24+
{
25+
$factory = new EnvFactory();
26+
$dotEnv = $factory->newInstance('dotenv');
27+
28+
$class = DotEnv::class;
29+
$this->assertInstanceOf($class, $dotEnv);
30+
}
31+
32+
public function testUnknownService(): void
33+
{
34+
$this->expectException(InvalidConfigurationArgumentException::class);
35+
$this->expectExceptionMessage(
36+
'Service unknown is not registered'
37+
);
38+
39+
$factory = new EnvFactory();
40+
$factory->newInstance('unknown');
41+
}
42+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Tests\Unit\Domain\Services\Env;
15+
16+
use Phalcon\Api\Domain\Exceptions\InvalidConfigurationArgumentException;
17+
use Phalcon\Api\Domain\Services\Env\Adapters\DotEnv;
18+
use Phalcon\Api\Domain\Services\Env\EnvFactory;
19+
use Phalcon\Api\Domain\Services\Env\EnvManager;
20+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
21+
use Phalcon\Container\Lazy\Env;
22+
use PHPUnit\Framework\Attributes\BackupGlobals;
23+
use ReflectionClass;
24+
25+
#[BackupGlobals(true)]
26+
final class EnvManagerTest extends AbstractUnitTestCase
27+
{
28+
protected function setUp(): void
29+
{
30+
$ref = new ReflectionClass(EnvManager::class);
31+
$ref->setStaticPropertyValue('isLoaded', false);
32+
$ref->setStaticPropertyValue('settings', []);
33+
}
34+
35+
public function testAppPathReturnsRoot(): void
36+
{
37+
$expected = dirname(__DIR__, 5);
38+
$actual = EnvManager::appPath();
39+
$this->assertSame($expected, $actual);
40+
}
41+
42+
public function testGetFromDotEnvLoad(): void
43+
{
44+
$_ENV = [
45+
'APP_ENV_ADAPTER' => 'dotenv',
46+
'APP_ENV_FILE_PATH' => EnvManager::appPath()
47+
. '/tests/Fixtures/Domain/Services/Env/'
48+
];
49+
50+
$values = [
51+
'SAMPLE_STRING' => 'sample_value',
52+
'SAMPLE_INT' => '1',
53+
'SAMPLE_TRUE' => true,
54+
'SAMPLE_FALSE' => false,
55+
];
56+
57+
$expected = 'default_value';
58+
$actual = EnvManager::get('NON_EXISTENT', 'default_value');
59+
$this->assertSame($expected, $actual);
60+
61+
$expected = $values['SAMPLE_STRING'];
62+
$actual = EnvManager::get('SAMPLE_STRING');
63+
$this->assertSame($expected, $actual);
64+
65+
$expected = $values['SAMPLE_INT'];
66+
$actual = EnvManager::get('SAMPLE_INT');
67+
$this->assertSame($expected, $actual);
68+
69+
$expected = $values['SAMPLE_TRUE'];
70+
$actual = EnvManager::get('SAMPLE_TRUE');
71+
$this->assertSame($expected, $actual);
72+
73+
$expected = $values['SAMPLE_FALSE'];
74+
$actual = EnvManager::get('SAMPLE_FALSE');
75+
$this->assertSame($expected, $actual);
76+
}
77+
}

0 commit comments

Comments
 (0)