Skip to content

Commit

Permalink
Laying groundwork for testing the library
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Oct 3, 2024
1 parent 1dec62a commit e0964dc
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 61 deletions.
82 changes: 58 additions & 24 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: "build"

on:
push:
branches:
- "*.x"
pull_request: ~
paths-ignore:
- "*.md"
pull_request:
paths-ignore:
- "*.md"
workflow_dispatch: ~

env:
PHP_EXTENSIONS: "mbstring"

jobs:
coding-standards:
name: "Coding Standards"
Expand All @@ -25,24 +31,25 @@ jobs:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"

- name: "Validate composer.json and composer.lock"
run: "composer validate --strict"
extensions: "${{ env.PHP_EXTENSIONS }}"
coverage: "none"

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

- name: "Run ergebnis/composer-normalize"
- name: "Validate composer"
run: "composer validate --strict"

- name: "Check composer normalized"
run: "composer normalize --dry-run"

- name: "Run symplify/easy-coding-standard"
- name: "Check style"
run: "composer check-style"

dependency-analysis:
Expand All @@ -54,31 +61,40 @@ jobs:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.3"

dependencies:
- "lowest"
- "highest"

steps:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
extensions: "${{ env.PHP_EXTENSIONS }}"
php-version: "${{ matrix.php-version }}"
tools: "composer-require-checker, composer-unused"

- name: "Remove require-dev section in composer.json"
run: "composer config --unset require-dev"

- name: "Remove autoload-dev section in composer.json"
run: "composer config --unset autoload-dev"

- name: "Add shipmonk/composer-dependency-analyser to composer.json"
run: "composer require --dev --no-install --no-update --no-plugins --no-scripts shipmonk/composer-dependency-analyser"

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

- name: "Run maglnet/composer-require-checker"
run: "composer-require-checker check"

- name: "Run icanhazstring/composer-unused"
run: "composer-unused"
- name: "Run dependency analysis"
run: "vendor/bin/composer-dependency-analyser"

static-code-analysis:
name: "Static Code Analysis"
Expand All @@ -89,25 +105,31 @@ jobs:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.3"

dependencies:
- "lowest"
- "highest"

steps:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP with extensions"
- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
coverage: "none"

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

- name: "Static analysis"
run: "composer analyse"
unit-tests:
name: "Unit tests"

Expand All @@ -117,6 +139,8 @@ jobs:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.3"

dependencies:
- "lowest"
Expand All @@ -126,16 +150,26 @@ jobs:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Build Docker image"
run: "docker build -t setono/deployer-cron --no-cache ./tests/docker"

- name: "Run Docker container"
run: "docker run -d -p 2222:22 setono/deployer-cron"

- name: "Change permissions"
run: "chmod 600 ./tests/docker/ssh/id_rsa && chmod 644 ./tests/docker/ssh/id_rsa.pub"

- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
coverage: "none"
extensions: "${{ env.PHP_EXTENSIONS }}"
php-version: "${{ matrix.php-version }}"

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

- name: "Run phpunit"
run: "composer phpunit"
run: "vendor/bin/phpunit"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
/composer.lock
/.phpunit.result.cache
/.build/

###> phpunit/phpunit ###
/phpunit.xml
.phpunit.result.cache
###< phpunit/phpunit ###
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ require_once 'recipe/setono_dotenv.php';

This will automatically hook into the default flow of Deployer.

## Testing

1. Set correct permissions on the SSH keys:

```shell
chmod 600 tests/docker/ssh/id_rsa && chmod 644 tests/docker/ssh/id_rsa.pub
```

2. Build the Docker image:

```shell
docker build -t setono/deployer-dotenv --no-cache ./tests/docker
```

3. Run the Docker container:

```shell
docker run -d -p 2222:22 setono/deployer-dotenv
```

4. Run the tests:

```shell
vendor/bin/phpunit
```

[ico-version]: https://poser.pugx.org/setono/deployer-dotenv/v/stable
[ico-license]: https://poser.pugx.org/setono/deployer-dotenv/license
[ico-github-actions]: https://github.com/Setono/deployer-dotenv/workflows/build/badge.svg
Expand Down
22 changes: 22 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;

return (new Configuration())
->addPathToExclude(__DIR__ . '/tests')
->ignoreUnknownClasses([
Deployer\Deployer::class,
Deployer\Task\Context::class,
])
->ignoreUnknownFunctions([
'Deployer\after',
'Deployer\before',
'Deployer\get',
'Deployer\run',
'Deployer\set',
'Deployer\task',
'Deployer\upload',
])
->ignoreErrorsOnPackage('deployer/deployer', [ErrorType::UNUSED_DEPENDENCY])
;
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"webmozart/assert": "^1.11"
},
"require-dev": {
"phpseclib/phpseclib": "^3.0",
"phpunit/phpunit": "^10.5",
"setono/code-quality-pack": "^2.8.2"
"setono/code-quality-pack": "^2.8.3",
"shipmonk/composer-dependency-analyser": "^1.7",
"symfony/flex": "^2.4"
},
"prefer-stable": true,
"autoload": {
Expand All @@ -34,7 +37,8 @@
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"dealerdirect/phpcodesniffer-composer-installer": false
"dealerdirect/phpcodesniffer-composer-installer": false,
"symfony/flex": true
},
"sort-packages": true
},
Expand Down
93 changes: 58 additions & 35 deletions src/task/setono_dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
use function Deployer\run;
use function Deployer\task;
use function Deployer\test;
use function Deployer\upload;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Dotenv\Dotenv;
use Webmozart\Assert\Assert;
use function Deployer\upload;

/**
* This step has to come AFTER the deploy:update_code step because
Expand Down Expand Up @@ -66,45 +65,14 @@
return;
}

$output = output();

$outputVariablesFunction = static function (OutputInterface $output, array $variables): void {
ksort($variables);

$table = new Table($output);
$table->setRows([
['Variable', 'Value'],
new TableSeparator(),
]);

/**
* @var string $key
* @var string $val
*/
foreach ($variables as $key => $val) {
$table->addRow([$key, $val]);
}

$table->render();
};

$evalEnv = static function (string $envContents): array {
/** @var array<string, scalar> $res */
$res = eval('?>' . $envContents);
Assert::isArray($res);
Assert::allScalar($res);

return $res;
};

/**
* We want two arrays to begin with. This allows us to easily compare the two arrays later on
* when the $variables may have been changed by the user
*/
$variables = $initialVariables = $evalEnv(run('cat {{release_path}}/.env.local.php'));
$variables = $initialVariables = evaluatePhpEnvFile('{{release_path}}/.env.local.php');

while (true) {
$outputVariablesFunction($output, $variables);
outputEnvironmentVariables($variables);

$confirmation = askConfirmation('Do you want to update ' . (isset($confirmation) ? 'more' : 'any') . ' environment variables?');
if (false === $confirmation) {
Expand All @@ -124,6 +92,24 @@
}
}

while (true) {
outputEnvironmentVariables($variables);

$confirmation = askConfirmation('Do you want to remove ' . (isset($confirmation) ? 'more' : 'any') . ' environment variables?');
if (false === $confirmation) {
break;
}

while (true) {
$variable = ask('Input environment variable. Press <return> when you are finished removing', '', array_keys($variables));
if ('' === $variable) {
break;
}

unset($variables[$variable]);
}
}

$stage = getStage();

/**
Expand Down Expand Up @@ -187,3 +173,40 @@ function getStage(): string

return $stage;
}

function outputEnvironmentVariables(array $variables): void
{
ksort($variables);

$table = new Table(output());
$table->setRows([
['Variable', 'Value'],
new TableSeparator(),
]);

/**
* @var string $key
* @var string $val
*/
foreach ($variables as $key => $val) {
$table->addRow([$key, $val]);
}

$table->render();
}

/**
* @return array<string, scalar>
*/
function evaluatePhpEnvFile(string $path): array
{
$data = run(sprintf('cat %s', $path));
Assert::stringNotEmpty($data);

/** @var array<string, scalar> $res */
$res = eval('?>' . $data);
Assert::isArray($res);
Assert::allScalar($res);

return $res;
}
Loading

0 comments on commit e0964dc

Please sign in to comment.