Skip to content

Commit

Permalink
Update CI (#48)
Browse files Browse the repository at this point in the history
* Remove Travis, add GitHub Actions

* Bump minimum PHP version to 7.3

* Write initial test(s) for env:get command

* No longer test on PHP 7.2

* Apply fixes from StyleCI

* Test the env:set command

* Test the env:delete command

* Use expectsQuestion method

The 'expectsConfirmation' method wasn't added until Laravel 7.2, so
we'll default to 'expectsQuestion' for now, while we still support
Laravel 6.x.

* Test the env:list command

* Test env:example command

* Clean up workflow configuration file

* Write tests for env:sync command

* Add OS in job name

* Use full laravel/framework dependency

* Enable the "fileinfo" extension in workflow

* Add Dependabot configuration file

* Update sven/file-config to tagged release

* Use expectsTable method in EnvListTest

* Update badge in readme

* Rewrite tests workflow

---------

Co-authored-by: Sven Luijten <[email protected]>
  • Loading branch information
svenluijten and svenluijten authored Aug 11, 2023
1 parent 6433131 commit bb769b4
Show file tree
Hide file tree
Showing 18 changed files with 436 additions and 72 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
36 changes: 36 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Tests (PHP)
on:
pull_request:
push:
branches:
- main
- '*.x'

jobs:
phpunit:
name: PHP ${{ matrix.php }} (${{ matrix.os }} with ${{ matrix.dependency-version }} deps)
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php: [7.3, 7.4]
dependency-version: [highest, lowest]

steps:
- uses: actions/checkout@v3

- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, fileinfo
coverage: none

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-version }}
composer-options: "--prefer-dist"

- name: Execute tests
run: vendor/bin/phpunit
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ for more information.
[ico-version]: https://img.shields.io/packagist/v/sven/flex-env.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/sven/flex-env.svg?style=flat-square
[ico-build]: https://img.shields.io/travis/svenluijten/flex-env?style=flat-square
[ico-build]: https://img.shields.io/github/actions/workflow/status/svenluijten/flex-env/tests.yml?style=flat-square
[ico-styleci]: https://styleci.io/repos/49644781/shield

[link-packagist]: https://packagist.org/packages/sven/flex-env
[link-downloads]: https://packagist.org/packages/sven/flex-env
[link-build]: https://travis-ci.org/svenluijten/flex-env
[link-build]: https://github.com/svenluijten/flex-env/actions/workflows/tests.yml
[link-styleci]: https://styleci.io/repos/49644781
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
}
],
"require": {
"php": "^7.1.3",
"illuminate/support": "5.8.* || ^6.0",
"sven/file-config": "dev-master"
"php": "^7.3 || ^8.0",
"laravel/framework": "^8.22",
"sven/file-config": "^3.1.0"
},
"require-dev": {
"graham-campbell/testbench": "^5.2",
"phpunit/phpunit": "7.* || ^8.3"
"graham-campbell/testbench": "^5.4",
"phpunit/phpunit": "^8.3 || ^9.0",
"mockery/mockery": "^1.4"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 2 additions & 13 deletions src/Commands/EnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sven\FlexEnv\Commands;

use Illuminate\Console\Command;
use Sven\FileConfig\Drivers\Env;
use Sven\FileConfig\Drivers\DotEnv;
use Sven\FileConfig\File;
use Sven\FileConfig\Store;

Expand All @@ -17,22 +17,11 @@ protected function config(string $fileName = null): Store

$file = new File($envPath.DIRECTORY_SEPARATOR.$fileName);

return new Store($file, new Env());
return new Store($file, new DotEnv());
}

protected function key(): string
{
return mb_strtoupper($this->argument('key'));
}

protected function value(): string
{
$value = (string) $this->argument('value');

if (preg_match('/[^a-z0-9-_]/', $value)) {
$value = '"'.$value.'"';
}

return $value;
}
}
4 changes: 2 additions & 2 deletions src/Commands/EnvExampleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Sven\FileConfig\Drivers\Env;
use Sven\FileConfig\Drivers\DotEnv;
use Symfony\Component\Console\Input\InputArgument;

class EnvExampleCommand extends EnvCommand
Expand All @@ -29,7 +29,7 @@ public function handle(Filesystem $files): int
})
->toArray();

$contents = (new Env())->export($values);
$contents = (new DotEnv())->export($values);
$file = $this->laravel->environmentPath().DIRECTORY_SEPARATOR.$this->argument('name');

$files->put($file, $contents);
Expand Down
14 changes: 7 additions & 7 deletions src/Commands/EnvListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle(): int
return [$key, $value];
});

$this->table(['Key', 'Value'], $data);
$this->table(['Key', 'Value'], $data->toArray());

return 0;
}
Expand All @@ -42,14 +42,14 @@ protected function getOptions()
protected function resolveReferences(Store $config): \Closure
{
return function (Collection $data) use ($config) {
return $data->map(function ($value) use ($config) {
preg_match('/\$\{(.+)\}/', $value, $matches);

if (isset($matches[1])) {
return $data->map(function ($value, $key) use ($config) {
$newValue = preg_replace_callback('/\$\{(.+)\}/m', function (array $matches) use ($config) {
return $config->get($matches[1]);
}
}, $value);

$config->set($key, $newValue);

return $value;
return $newValue;
});
};
}
Expand Down
7 changes: 6 additions & 1 deletion src/Commands/EnvSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ public function handle(): int
return 0;
}

protected function value(): string
{
return (string) $this->argument('value') ?: $this->ask('What should the new value be?');
}

protected function getArguments()
{
return [
['key', InputArgument::REQUIRED, 'The name of the environment variable to set.'],
['value', InputArgument::REQUIRED, 'The value to set the environment variable to.'],
['value', InputArgument::OPTIONAL, 'The value to set the environment variable to.', null],
];
}
}
2 changes: 1 addition & 1 deletion src/Commands/EnvSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(): int
}

if ($this->option('dry-run')) {
$table = Collection::make($real->all())
$table = Collection::make($missingKeys)
->map(function ($value, $key) {
return [$key, $value];
})
Expand Down
70 changes: 69 additions & 1 deletion tests/Feature/EnvDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,73 @@

class EnvDeleteTest extends TestCase
{
//
/** @test */
public function it_can_delete_an_entry(): void
{
$this->setEnvironment(<<<'ENV'
HELLO=world
FOO=bar
SOMETHING=else
ENV
);

$this->artisan('env:delete FOO')
// ->expectsConfirmation('Are you sure you want to delete "FOO" from your .env file?', 'yes')
->expectsQuestion('Are you sure you want to delete "FOO" from your .env file?', true)
->expectsOutput('Successfully deleted the entry "FOO" from your .env file.')
->assertExitCode(0);

$this->assertStringEqualsFile($this->app->environmentFilePath(), <<<'ENV'
HELLO=world
SOMETHING=else

ENV
);
}

/** @test */
public function it_does_not_ask_for_confirmation_when_the_force_flag_is_supplied(): void
{
$this->setEnvironment(<<<'ENV'
HELLO=world
FOO=bar
SOMETHING=else
ENV
);

$this->artisan('env:delete FOO --force')
->expectsOutput('Successfully deleted the entry "FOO" from your .env file.')
->assertExitCode(0);

$this->assertStringEqualsFile($this->app->environmentFilePath(), <<<'ENV'
HELLO=world
SOMETHING=else

ENV
);
}

/** @test */
public function it_does_not_delete_the_entry_when_no_is_answered(): void
{
$this->setEnvironment(<<<'ENV'
HELLO=world
FOO=bar
SOMETHING=else
ENV
);

$this->artisan('env:delete FOO')
// ->expectsConfirmation('Are you sure you want to delete "FOO" from your .env file?', 'no')
->expectsQuestion('Are you sure you want to delete "FOO" from your .env file?', false)
->expectsOutput('Alright, no changes made.')
->assertExitCode(1);

$this->assertStringEqualsFile($this->app->environmentFilePath(), <<<'ENV'
HELLO=world
FOO=bar
SOMETHING=else
ENV
);
}
}
62 changes: 61 additions & 1 deletion tests/Feature/EnvExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,65 @@

class EnvExampleTest extends TestCase
{
//
/** @test */
public function it_generates_an_example_file_with_empty_values(): void
{
$this->setEnvironment(<<<'ENV'
APP_NAME="Some Name Here"
APP_LOCALE=en_US
DB_USERNAME=root
DB_PASSWORD=root
DB_HOST=localhost
ENV
);

$this->artisan('env:example')
->assertExitCode(0);

$examplePath = $this->app->environmentPath().DIRECTORY_SEPARATOR.'.env.example';
$this->assertFileExists($examplePath);
$this->assertStringEqualsFile($examplePath, <<<'ENV'
APP_NAME=
APP_LOCALE=
DB_USERNAME=
DB_PASSWORD=
DB_HOST=

ENV
);
}

/** @test */
public function it_preserves_comments_when_generating_example_environment_file(): void
{
$this->setEnvironment(<<<'ENV'
# Application Settings
APP_NAME="Some Name Here"
APP_LOCALE=en_US
# Database
DB_USERNAME=root
DB_PASSWORD=root
DB_HOST=localhost
ENV
);

$this->artisan('env:example');

$examplePath = $this->app->environmentPath().DIRECTORY_SEPARATOR.'.env.example';
$this->assertStringEqualsFile($examplePath, <<<'ENV'
# Application Settings
APP_NAME=
APP_LOCALE=
# Database
DB_USERNAME=
DB_PASSWORD=
DB_HOST=

ENV
);
}
}
Loading

0 comments on commit bb769b4

Please sign in to comment.