Skip to content

Commit 38d949c

Browse files
committed
Add skipIfExtensionLoaded() to simplify "extension not loaded" tests
1 parent be184d0 commit 38d949c

3 files changed

Lines changed: 16 additions & 18 deletions

File tree

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ function needsInternet(): void
1818
\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');
1919
}
2020

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+
}
31+
}
32+
2133
}

0 commit comments

Comments
 (0)