Skip to content

Commit 94eaa77

Browse files
committed
test(integration): add sandbox mTLS head connectivity check
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent b8d8435 commit 94eaa77

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
declare(strict_types=1);
7+
8+
namespace LibreCodeCoop\NfsePHP\Tests\Integration\Http;
9+
10+
use LibreCodeCoop\NfsePHP\Tests\Support\LoadsLocalEnv;
11+
use LibreCodeCoop\NfsePHP\Tests\TestCase;
12+
13+
/**
14+
* Optional sandbox connectivity smoke test (mTLS).
15+
* Skips if env vars are not configured.
16+
*/
17+
class SandboxMtlsHeadTest extends TestCase
18+
{
19+
use LoadsLocalEnv;
20+
21+
public function testSandboxHeadWithMtlsWhenEnvIsPresent(): void
22+
{
23+
self::loadLocalEnv();
24+
25+
$url = getenv('NFSE_HEAD_URL') ?: '';
26+
$pfxPath = getenv('NFSE_MTLS_PFX_PATH') ?: '';
27+
$pfxPassword = getenv('NFSE_MTLS_PFX_PASSWORD') ?: '';
28+
29+
if ($url === '' || $pfxPath === '' || $pfxPassword === '') {
30+
self::markTestSkipped('Set NFSE_HEAD_URL, NFSE_MTLS_PFX_PATH and NFSE_MTLS_PFX_PASSWORD to run sandbox mTLS test.');
31+
}
32+
33+
if (!str_starts_with($pfxPath, '/')) {
34+
$pfxPath = dirname(__DIR__, 3) . '/' . ltrim($pfxPath, '/');
35+
}
36+
37+
if (!is_file($pfxPath)) {
38+
self::markTestSkipped('Configured PFX file does not exist for mTLS test.');
39+
}
40+
41+
$cmd = sprintf(
42+
'curl --silent --show-error --output /dev/null --write-out "%%{http_code}" --head --cert-type P12 --cert %s %s; echo "|exit:$?"',
43+
escapeshellarg($pfxPath . ':' . $pfxPassword),
44+
escapeshellarg($url)
45+
);
46+
47+
$result = shell_exec($cmd);
48+
49+
self::assertNotFalse($result, 'curl execution failed');
50+
51+
$result = trim((string) $result);
52+
53+
if (!str_contains($result, '|exit:')) {
54+
self::fail('Unexpected curl result format.');
55+
}
56+
57+
[$httpCode, $exitPart] = explode('|exit:', $result, 2);
58+
$httpCode = trim($httpCode);
59+
$exitCode = (int) trim($exitPart);
60+
61+
if ($exitCode !== 0) {
62+
self::markTestSkipped('mTLS curl failed in local runtime (likely OpenSSL/PFX compatibility).');
63+
}
64+
65+
self::assertContains($httpCode, ['200', '401', '403', '404']);
66+
}
67+
}

0 commit comments

Comments
 (0)