1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Enhancement: Turn PHPT test into PHPUnit test case (#942)

This commit is contained in:
Andreas Möller
2024-02-13 12:13:51 +01:00
committed by GitHub
parent b506bb808d
commit 9705aae05a
4 changed files with 86 additions and 66 deletions

View File

@@ -20,6 +20,11 @@
"phpweb\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"phpweb\\Test\\EndToEnd\\": "tests/EndToEnd/"
}
},
"config": {
"platform": {
"php": "8.2.0"

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace phpweb\Test\EndToEnd;
use PHPUnit\Framework;
#[Framework\Attributes\CoversNothing]
final class SmokeTest extends Framework\TestCase
{
#[Framework\Attributes\DataProvider('provideUrl')]
public function testUrlReturnsSuccessfulHttpResponseStatusCode(string $url): void
{
$successfulHttpStatusCodes = [200, 301, 302];
$handle = curl_init();
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
];
curl_setopt_array($handle, $options);
curl_exec($handle);
$httpStatusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
self::assertTrue(in_array($httpStatusCode, $successfulHttpStatusCodes, true), sprintf(
'Failed asserting that the URL "%s" returns a successful HTTP response status code, got "%d" instead.',
$url,
$httpStatusCode,
));
}
/**
* @return \Generator<string, array{0: string}>
*/
public static function provideUrl(): \Generator
{
$httpHost = getenv('HTTP_HOST');
if (!is_string($httpHost)) {
throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.');
}
$pathToRoot = realpath(__DIR__ . '/../..');
$patterns = [
$pathToRoot . '/*.php',
$pathToRoot . '/archive/*.php',
$pathToRoot . '/conferences/*.php',
$pathToRoot . '/license/*.php',
$pathToRoot . '/manual/*.php',
$pathToRoot . '/manual/en/*.php',
$pathToRoot . '/releases/*.php',
$pathToRoot . '/releases/*/*.php',
$pathToRoot . '/releases/*/*/*.php',
];
foreach ($patterns as $pattern) {
$pathsToFiles = glob($pattern);
$paths = str_replace($pathToRoot, '', $pathsToFiles);
foreach ($paths as $path) {
$url = sprintf(
'http://%s%s',
$httpHost,
$path,
);
yield $url => [
$url,
];
}
}
}
}

View File

@@ -1,65 +0,0 @@
--TEST--
paths return HTTP response status code 200, 301, or 302
--FILE--
<?php
declare(strict_types=1);
$httpHost = getenv('HTTP_HOST');
if (!is_string($httpHost)) {
throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.');
}
$pathToRoot = realpath(__DIR__ . '/../..');
$pathsToFiles = [
...glob($pathToRoot . '/*.php'),
...glob($pathToRoot . '/archive/*.php'),
...glob($pathToRoot . '/conferences/*.php'),
...glob($pathToRoot . '/license/*.php'),
...glob($pathToRoot . '/manual/*.php'),
...glob($pathToRoot . '/manual/en/*.php'),
...glob($pathToRoot . '/releases/*.php'),
...glob($pathToRoot . '/releases/*/*.php'),
...glob($pathToRoot . '/releases/*/*/*.php'),
];
$paths = str_replace($pathToRoot, '', $pathsToFiles);
$baseUrl = sprintf(
'http://%s',
$httpHost,
);
$pathsToStatusCodes = array_combine(
$paths,
array_map(static function (string $url) use ($baseUrl): int {
$handle = curl_init();
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => sprintf(
'%s%s',
$baseUrl,
$url,
),
];
curl_setopt_array($handle, $options);
curl_exec($handle);
return curl_getinfo($handle, CURLINFO_HTTP_CODE);
}, $paths),
);
$pathsWithUnexpectedStatusCodes = array_filter($pathsToStatusCodes, static function (int $statusCode): bool {
return !in_array($statusCode, [200, 301, 302], true);
});
var_dump($pathsWithUnexpectedStatusCodes);
?>
--EXPECT--
array(0) {
}

View File

@@ -32,7 +32,7 @@
</source>
<testsuites>
<testsuite name="end-to-end">
<directory suffix=".phpt">EndToEnd/</directory>
<directory suffix=".php">EndToEnd/</directory>
</testsuite>
<testsuite name="unit">
<directory suffix=".phpt">Unit/</directory>