Skip to content

Commit cd4c691

Browse files
authored
Simplify env vars in tests (#90)
2 parents 96e7726 + 20ae8d7 commit cd4c691

6 files changed

Lines changed: 34 additions & 26 deletions

File tree

.github/workflows/php-tester-include-skipped.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- name: Install dependencies
2424
run: composer update --no-progress --no-interaction
2525
- name: Run Tester including skipped tests
26-
run: TEST_CASE_RUNNER_INCLUDE_SKIPPED=1 composer tester
26+
run: composer tester-include-skipped

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ composer cs-fix # auto-fix code style
1212
composer phpstan # static analysis
1313
composer psalm # psalm static analysis
1414
composer tester # run all tests (requires curl, gnupg, pcov extensions)
15+
composer tester-include-skipped # run all tests including the network-dependent ones that are skipped by default (sets TEST_CASE_RUNNER_INCLUDE_SKIPPED=1)
1516
composer tester-no-extensions # run only the two extension-absence tests
1617
```
1718

composer.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@
5151
"@tester",
5252
"@psalm"
5353
],
54-
"tester-no-extensions": "TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED=1 vendor/bin/tester -s -c tests/php-unix-no-extensions.ini -d zend.assertions=1 --colors 1 tests/Signature/Providers/SecurityTxtSignatureGnuPgProviderNoExtensionTest.phpt tests/Fetcher/HttpClients/SecurityTxtFetcherCurlClientNoExtensionTest.phpt",
55-
"tester": "vendor/bin/tester -s -c tests/php-unix.ini -d zend.assertions=1 --colors 1 --coverage tests/temp/coverage.html --coverage-src src/ tests/"
54+
"tester-no-extensions": [
55+
"@putenv TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED=1",
56+
"@putenv TEST_CASE_RUNNER_INCLUDE_SKIPPED=0",
57+
"vendor/bin/tester -s -c tests/php-unix-no-extensions.ini -d zend.assertions=1 --colors 1 tests/Signature/Providers/SecurityTxtSignatureGnuPgProviderNoExtensionTest.phpt tests/Fetcher/HttpClients/SecurityTxtFetcherCurlClientNoExtensionTest.phpt"
58+
],
59+
"tester-include-skipped": [
60+
"@putenv TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED=0",
61+
"@putenv TEST_CASE_RUNNER_INCLUDE_SKIPPED=1",
62+
"vendor/bin/tester -s -c tests/php-unix.ini -d zend.assertions=1 --colors 1 --coverage tests/temp/coverage.html --coverage-src src/ tests/"
63+
],
64+
"tester": [
65+
"@putenv TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED=0",
66+
"@putenv TEST_CASE_RUNNER_INCLUDE_SKIPPED=0",
67+
"vendor/bin/tester -s -c tests/php-unix.ini -d zend.assertions=1 --colors 1 --coverage tests/temp/coverage.html --coverage-src src/ tests/"
68+
]
5669
}
5770
}

tests/Fetcher/HttpClients/SecurityTxtFetcherCurlClientNoExtensionTest.phpt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Spaze\SecurityTxt\Fetcher;
88
use Spaze\SecurityTxt\Fetcher\Exceptions\SecurityTxtCannotOpenUrlExtensionNotLoadedException;
99
use Spaze\SecurityTxt\Fetcher\HttpClients\SecurityTxtFetcherCurlClient;
1010
use Tester\Assert;
11-
use Tester\Environment;
1211
use Tester\TestCase;
1312
use Uri\WhatWg\Url;
13+
use function Spaze\SecurityTxt\Test\skipIfExtensionLoaded;
1414

1515
require __DIR__ . '/../../bootstrap.php';
1616

@@ -20,14 +20,7 @@ final class SecurityTxtFetcherCurlClientNoExtensionTest extends TestCase
2020

2121
public function testExceptionWhenExtensionNotLoaded(): void
2222
{
23-
if (extension_loaded('curl')) {
24-
if (getenv('TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED') === '1') {
25-
Assert::fail('The curl extension must not be loaded for this test, run with the php-unix-no-extensions.ini configuration');
26-
} else {
27-
Environment::skip('Run this test with the php-unix-no-extensions.ini configuration');
28-
}
29-
}
30-
23+
skipIfExtensionLoaded('curl');
3124
$client = new SecurityTxtFetcherCurlClient();
3225
Assert::throws(function () use ($client) {
3326
$client->getResponse(new SecurityTxtFetcherUrl(new Url('https://example.com/'), []), 'example.com', '192.0.2.1', SecurityTxtIpAddressType::V4);

tests/Signature/Providers/SecurityTxtSignatureGnuPgProviderNoExtensionTest.phpt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace Spaze\SecurityTxt\Signature\Providers;
66

77
use Spaze\SecurityTxt\Signature\Exceptions\SecurityTxtCannotCreateSignatureExtensionNotLoadedException;
88
use Tester\Assert;
9-
use Tester\Environment;
109
use Tester\TestCase;
10+
use function Spaze\SecurityTxt\Test\skipIfExtensionLoaded;
1111

1212
require __DIR__ . '/../../bootstrap.php';
1313

@@ -17,14 +17,7 @@ final class SecurityTxtSignatureGnuPgProviderNoExtensionTest extends TestCase
1717

1818
public function testExceptionWhenGnupgExtensionNotLoaded(): void
1919
{
20-
if (extension_loaded('gnupg')) {
21-
if (getenv('TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED') === '1') {
22-
Assert::fail('The gnupg extension must not be loaded for this test, run with the php-unix-no-extensions.ini configuration');
23-
} else {
24-
Environment::skip('Run this test with the php-unix-no-extensions.ini configuration');
25-
}
26-
}
27-
20+
skipIfExtensionLoaded('gnupg');
2821
$gnuPg = new SecurityTxtSignatureGnuPgProvider();
2922
Assert::throws(function () use ($gnuPg) {
3023
$gnuPg->getErrorInfo();

tests/bootstrap.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ function needsInternet(): void
1515
if (getenv('TEST_CASE_RUNNER_INCLUDE_SKIPPED') === '1') {
1616
return;
1717
}
18-
\Tester\Environment::skip(sprintf(
19-
'The test uses the Internet, to not skip the test case run it with `%s=%s`',
20-
'TEST_CASE_RUNNER_INCLUDE_SKIPPED',
21-
'1',
22-
));
18+
\Tester\Environment::skip('The test uses the Internet, to not skip the test case run it with TEST_CASE_RUNNER_INCLUDE_SKIPPED=1, or run composer tester-include-skipped to run all skipped tests');
19+
}
20+
21+
22+
function skipIfExtensionLoaded(string $extension): void
23+
{
24+
if (extension_loaded($extension)) {
25+
if (getenv('TEST_CASE_RUNNER_FORCE_EXTENSIONS_NOT_LOADED') === '1') {
26+
\Tester\Assert::fail("The {$extension} extension must not be loaded for this test, run with the php-unix-no-extensions.ini configuration, or run composer tester-no-extensions to run all similar tests");
27+
} else {
28+
\Tester\Environment::skip('Run this test with the php-unix-no-extensions.ini configuration, or run composer tester-no-extensions to run all similar tests');
29+
}
30+
}
2331
}
2432

2533
}

0 commit comments

Comments
 (0)