Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot committed Apr 14, 2024
1 parent ee15ed3 commit 731515c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
"phpstan": "php --version && php vendor/bin/phpstan --version && php -d memory_limit=1G vendor/bin/phpstan analyse -c ruleset-phpstan.neon -vvv",
"cs": "./vendor/bin/php-cs-fixer fix -vvv --show-progress=dots --config=ruleset-php_cs.php",
"test": "./vendor/bin/phpunit",
"rector": "./vendor/bin/rector process --config=rector.php",
"test-all": [
"@test",
"@cs",
"@phpstan",
"@cs"
"@rector",
"@test"
]
},
"extra": {
Expand Down
11 changes: 1 addition & 10 deletions src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\ServiceProvider;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -50,15 +49,7 @@ protected function getFileContents(string $file = ''): string
{
$envFile = $this->envEditor->getFilesManager()->getFilePath($file);

if (!$this->filesystem->exists($envFile)) {
throw new EnvException(__(ServiceProvider::TRANSLATE_PREFIX.'exceptions.fileNotExists', ['name' => $envFile]), 0);
}

try {
return $this->filesystem->get($envFile);
} catch (\Exception) {
throw new EnvException(__(ServiceProvider::TRANSLATE_PREFIX.'exceptions.fileNotExists', ['name' => $envFile]), 2);
}
return $this->filesystem->get($envFile);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/EnvFileContentManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function wrong_file_throws_exception(): void
public function fail_to_retrieve_file_contents(): void
{
$manager = $this->getEnvFileContentManager();
$file = $file = config('env-editor.paths.backupDirectory').DIRECTORY_SEPARATOR.'not-existed-file';
$file = config('env-editor.paths.backupDirectory').DIRECTORY_SEPARATOR.'not-existed-file';

self::expectException(EnvException::class);
self::expectExceptionMessage('File "'.$file.'" does not Exists !!!');
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Helpers/EnvKeysManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,30 @@ public function adds_keys(): void
EnvEditorFacade::addKey('FOO', 'bar2');
} catch (\Exception $e) {
self::assertInstanceOf(EnvException::class, $e);
$this->assertEquals('Key "FOO" already Exists !!!', $e->getMessage());
unlink($fullPath);
}
}

#[Test]
public function adds_two_keys_in_different_group(): void
{
$fileName = 'dummy.tmp';
$fullPath = $this->createNewDummyFile($fileName);
$this->app->loadEnvironmentFrom($fileName);

EnvEditorFacade::addKey('FOO1', 'bar');
EnvEditorFacade::addKey('FOO2', 'bar');

$envData = EnvEditorFacade::getEnvFileContent();

$firstKey = $envData->firstWhere('key', 'FOO1');
$secondKey = $envData->firstWhere('key', 'FOO2');

$this->assertGreaterThan($firstKey->group, $secondKey->group);
unlink($fullPath);
}

protected function getEnvKeysManager(): EnvKeysManager
{
$envEditor = new EnvEditor(
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ public function delete_backup_works_and_returns_bool(): void
$this->assertFalse(file_exists($file));
}

#[Test]
public function delete_backup_throws_exception_if_empty_sting_given(): void
{
$manager = $this->getEnvFilesManager();
$this->expectException(EnvException::class);
$this->expectExceptionMessage('You have to provide a FileName !!!');
$manager->deleteBackup('');
}

private function createAndTestPath(string $path): void
{
$path = realpath($path);
Expand Down

0 comments on commit 731515c

Please sign in to comment.