Skip to content

Commit e0964dc

Browse files
committed
Laying groundwork for testing the library
1 parent 1dec62a commit e0964dc

File tree

12 files changed

+314
-61
lines changed

12 files changed

+314
-61
lines changed

.github/workflows/build.yaml

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
name: "build"
2-
32
on:
43
push:
54
branches:
65
- "*.x"
7-
pull_request: ~
6+
paths-ignore:
7+
- "*.md"
8+
pull_request:
9+
paths-ignore:
10+
- "*.md"
811
workflow_dispatch: ~
912

13+
env:
14+
PHP_EXTENSIONS: "mbstring"
15+
1016
jobs:
1117
coding-standards:
1218
name: "Coding Standards"
@@ -25,24 +31,25 @@ jobs:
2531
- name: "Checkout"
2632
uses: "actions/checkout@v4"
2733

28-
- name: "Install PHP with extensions"
34+
- name: "Setup PHP, with composer and extensions"
2935
uses: "shivammathur/setup-php@v2"
3036
with:
31-
coverage: "none"
3237
php-version: "${{ matrix.php-version }}"
33-
34-
- name: "Validate composer.json and composer.lock"
35-
run: "composer validate --strict"
38+
extensions: "${{ env.PHP_EXTENSIONS }}"
39+
coverage: "none"
3640

3741
- name: "Install composer dependencies"
38-
uses: "ramsey/composer-install@v2"
42+
uses: "ramsey/composer-install@v3"
3943
with:
4044
dependency-versions: "${{ matrix.dependencies }}"
4145

42-
- name: "Run ergebnis/composer-normalize"
46+
- name: "Validate composer"
47+
run: "composer validate --strict"
48+
49+
- name: "Check composer normalized"
4350
run: "composer normalize --dry-run"
4451

45-
- name: "Run symplify/easy-coding-standard"
52+
- name: "Check style"
4653
run: "composer check-style"
4754

4855
dependency-analysis:
@@ -54,31 +61,40 @@ jobs:
5461
matrix:
5562
php-version:
5663
- "8.1"
64+
- "8.2"
65+
- "8.3"
5766

5867
dependencies:
68+
- "lowest"
5969
- "highest"
6070

6171
steps:
6272
- name: "Checkout"
6373
uses: "actions/checkout@v4"
6474

65-
- name: "Install PHP with extensions"
75+
- name: "Setup PHP, with composer and extensions"
6676
uses: "shivammathur/setup-php@v2"
6777
with:
6878
coverage: "none"
79+
extensions: "${{ env.PHP_EXTENSIONS }}"
6980
php-version: "${{ matrix.php-version }}"
70-
tools: "composer-require-checker, composer-unused"
81+
82+
- name: "Remove require-dev section in composer.json"
83+
run: "composer config --unset require-dev"
84+
85+
- name: "Remove autoload-dev section in composer.json"
86+
run: "composer config --unset autoload-dev"
87+
88+
- name: "Add shipmonk/composer-dependency-analyser to composer.json"
89+
run: "composer require --dev --no-install --no-update --no-plugins --no-scripts shipmonk/composer-dependency-analyser"
7190

7291
- name: "Install composer dependencies"
73-
uses: "ramsey/composer-install@v2"
92+
uses: "ramsey/composer-install@v3"
7493
with:
7594
dependency-versions: "${{ matrix.dependencies }}"
7695

77-
- name: "Run maglnet/composer-require-checker"
78-
run: "composer-require-checker check"
79-
80-
- name: "Run icanhazstring/composer-unused"
81-
run: "composer-unused"
96+
- name: "Run dependency analysis"
97+
run: "vendor/bin/composer-dependency-analyser"
8298

8399
static-code-analysis:
84100
name: "Static Code Analysis"
@@ -89,25 +105,31 @@ jobs:
89105
matrix:
90106
php-version:
91107
- "8.1"
108+
- "8.2"
109+
- "8.3"
92110

93111
dependencies:
112+
- "lowest"
94113
- "highest"
95114

96115
steps:
97116
- name: "Checkout"
98117
uses: "actions/checkout@v4"
99118

100-
- name: "Install PHP with extensions"
119+
- name: "Setup PHP, with composer and extensions"
101120
uses: "shivammathur/setup-php@v2"
102121
with:
103-
coverage: "none"
104122
php-version: "${{ matrix.php-version }}"
123+
extensions: "${{ env.PHP_EXTENSIONS }}"
124+
coverage: "none"
105125

106126
- name: "Install composer dependencies"
107-
uses: "ramsey/composer-install@v2"
127+
uses: "ramsey/composer-install@v3"
108128
with:
109129
dependency-versions: "${{ matrix.dependencies }}"
110130

131+
- name: "Static analysis"
132+
run: "composer analyse"
111133
unit-tests:
112134
name: "Unit tests"
113135

@@ -117,6 +139,8 @@ jobs:
117139
matrix:
118140
php-version:
119141
- "8.1"
142+
- "8.2"
143+
- "8.3"
120144

121145
dependencies:
122146
- "lowest"
@@ -126,16 +150,26 @@ jobs:
126150
- name: "Checkout"
127151
uses: "actions/checkout@v4"
128152

153+
- name: "Build Docker image"
154+
run: "docker build -t setono/deployer-cron --no-cache ./tests/docker"
155+
156+
- name: "Run Docker container"
157+
run: "docker run -d -p 2222:22 setono/deployer-cron"
158+
159+
- name: "Change permissions"
160+
run: "chmod 600 ./tests/docker/ssh/id_rsa && chmod 644 ./tests/docker/ssh/id_rsa.pub"
161+
129162
- name: "Setup PHP, with composer and extensions"
130163
uses: "shivammathur/setup-php@v2"
131164
with:
132-
php-version: "${{ matrix.php-version }}"
133165
coverage: "none"
166+
extensions: "${{ env.PHP_EXTENSIONS }}"
167+
php-version: "${{ matrix.php-version }}"
134168

135169
- name: "Install composer dependencies"
136-
uses: "ramsey/composer-install@v2"
170+
uses: "ramsey/composer-install@v3"
137171
with:
138172
dependency-versions: "${{ matrix.dependencies }}"
139173

140174
- name: "Run phpunit"
141-
run: "composer phpunit"
175+
run: "vendor/bin/phpunit"

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@
22
/composer.lock
33
/.phpunit.result.cache
44
/.build/
5+
6+
###> phpunit/phpunit ###
7+
/phpunit.xml
8+
.phpunit.result.cache
9+
###< phpunit/phpunit ###

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ require_once 'recipe/setono_dotenv.php';
4545

4646
This will automatically hook into the default flow of Deployer.
4747

48+
## Testing
49+
50+
1. Set correct permissions on the SSH keys:
51+
52+
```shell
53+
chmod 600 tests/docker/ssh/id_rsa && chmod 644 tests/docker/ssh/id_rsa.pub
54+
```
55+
56+
2. Build the Docker image:
57+
58+
```shell
59+
docker build -t setono/deployer-dotenv --no-cache ./tests/docker
60+
```
61+
62+
3. Run the Docker container:
63+
64+
```shell
65+
docker run -d -p 2222:22 setono/deployer-dotenv
66+
```
67+
68+
4. Run the tests:
69+
70+
```shell
71+
vendor/bin/phpunit
72+
```
73+
4874
[ico-version]: https://poser.pugx.org/setono/deployer-dotenv/v/stable
4975
[ico-license]: https://poser.pugx.org/setono/deployer-dotenv/license
5076
[ico-github-actions]: https://github.com/Setono/deployer-dotenv/workflows/build/badge.svg

composer-dependency-analyser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
4+
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
5+
6+
return (new Configuration())
7+
->addPathToExclude(__DIR__ . '/tests')
8+
->ignoreUnknownClasses([
9+
Deployer\Deployer::class,
10+
Deployer\Task\Context::class,
11+
])
12+
->ignoreUnknownFunctions([
13+
'Deployer\after',
14+
'Deployer\before',
15+
'Deployer\get',
16+
'Deployer\run',
17+
'Deployer\set',
18+
'Deployer\task',
19+
'Deployer\upload',
20+
])
21+
->ignoreErrorsOnPackage('deployer/deployer', [ErrorType::UNUSED_DEPENDENCY])
22+
;

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
"webmozart/assert": "^1.11"
1818
},
1919
"require-dev": {
20+
"phpseclib/phpseclib": "^3.0",
2021
"phpunit/phpunit": "^10.5",
21-
"setono/code-quality-pack": "^2.8.2"
22+
"setono/code-quality-pack": "^2.8.3",
23+
"shipmonk/composer-dependency-analyser": "^1.7",
24+
"symfony/flex": "^2.4"
2225
},
2326
"prefer-stable": true,
2427
"autoload": {
@@ -34,7 +37,8 @@
3437
"config": {
3538
"allow-plugins": {
3639
"ergebnis/composer-normalize": true,
37-
"dealerdirect/phpcodesniffer-composer-installer": false
40+
"dealerdirect/phpcodesniffer-composer-installer": false,
41+
"symfony/flex": true
3842
},
3943
"sort-packages": true
4044
},

src/task/setono_dotenv.php

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
use function Deployer\run;
1515
use function Deployer\task;
1616
use function Deployer\test;
17+
use function Deployer\upload;
1718
use Symfony\Component\Console\Helper\Table;
1819
use Symfony\Component\Console\Helper\TableSeparator;
19-
use Symfony\Component\Console\Output\OutputInterface;
2020
use Symfony\Component\Dotenv\Dotenv;
2121
use Webmozart\Assert\Assert;
22-
use function Deployer\upload;
2322

2423
/**
2524
* This step has to come AFTER the deploy:update_code step because
@@ -66,45 +65,14 @@
6665
return;
6766
}
6867

69-
$output = output();
70-
71-
$outputVariablesFunction = static function (OutputInterface $output, array $variables): void {
72-
ksort($variables);
73-
74-
$table = new Table($output);
75-
$table->setRows([
76-
['Variable', 'Value'],
77-
new TableSeparator(),
78-
]);
79-
80-
/**
81-
* @var string $key
82-
* @var string $val
83-
*/
84-
foreach ($variables as $key => $val) {
85-
$table->addRow([$key, $val]);
86-
}
87-
88-
$table->render();
89-
};
90-
91-
$evalEnv = static function (string $envContents): array {
92-
/** @var array<string, scalar> $res */
93-
$res = eval('?>' . $envContents);
94-
Assert::isArray($res);
95-
Assert::allScalar($res);
96-
97-
return $res;
98-
};
99-
10068
/**
10169
* We want two arrays to begin with. This allows us to easily compare the two arrays later on
10270
* when the $variables may have been changed by the user
10371
*/
104-
$variables = $initialVariables = $evalEnv(run('cat {{release_path}}/.env.local.php'));
72+
$variables = $initialVariables = evaluatePhpEnvFile('{{release_path}}/.env.local.php');
10573

10674
while (true) {
107-
$outputVariablesFunction($output, $variables);
75+
outputEnvironmentVariables($variables);
10876

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

95+
while (true) {
96+
outputEnvironmentVariables($variables);
97+
98+
$confirmation = askConfirmation('Do you want to remove ' . (isset($confirmation) ? 'more' : 'any') . ' environment variables?');
99+
if (false === $confirmation) {
100+
break;
101+
}
102+
103+
while (true) {
104+
$variable = ask('Input environment variable. Press <return> when you are finished removing', '', array_keys($variables));
105+
if ('' === $variable) {
106+
break;
107+
}
108+
109+
unset($variables[$variable]);
110+
}
111+
}
112+
127113
$stage = getStage();
128114

129115
/**
@@ -187,3 +173,40 @@ function getStage(): string
187173

188174
return $stage;
189175
}
176+
177+
function outputEnvironmentVariables(array $variables): void
178+
{
179+
ksort($variables);
180+
181+
$table = new Table(output());
182+
$table->setRows([
183+
['Variable', 'Value'],
184+
new TableSeparator(),
185+
]);
186+
187+
/**
188+
* @var string $key
189+
* @var string $val
190+
*/
191+
foreach ($variables as $key => $val) {
192+
$table->addRow([$key, $val]);
193+
}
194+
195+
$table->render();
196+
}
197+
198+
/**
199+
* @return array<string, scalar>
200+
*/
201+
function evaluatePhpEnvFile(string $path): array
202+
{
203+
$data = run(sprintf('cat %s', $path));
204+
Assert::stringNotEmpty($data);
205+
206+
/** @var array<string, scalar> $res */
207+
$res = eval('?>' . $data);
208+
Assert::isArray($res);
209+
Assert::allScalar($res);
210+
211+
return $res;
212+
}

0 commit comments

Comments
 (0)