Skip to content

Commit 9705aae

Browse files
authored
Enhancement: Turn PHPT test into PHPUnit test case (#942)
1 parent b506bb8 commit 9705aae

File tree

4 files changed

+86
-66
lines changed

4 files changed

+86
-66
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"phpweb\\": "src/"
2121
}
2222
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"phpweb\\Test\\EndToEnd\\": "tests/EndToEnd/"
26+
}
27+
},
2328
"config": {
2429
"platform": {
2530
"php": "8.2.0"

tests/EndToEnd/SmokeTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Test\EndToEnd;
6+
7+
use PHPUnit\Framework;
8+
9+
#[Framework\Attributes\CoversNothing]
10+
final class SmokeTest extends Framework\TestCase
11+
{
12+
#[Framework\Attributes\DataProvider('provideUrl')]
13+
public function testUrlReturnsSuccessfulHttpResponseStatusCode(string $url): void
14+
{
15+
$successfulHttpStatusCodes = [200, 301, 302];
16+
17+
$handle = curl_init();
18+
19+
$options = [
20+
CURLOPT_RETURNTRANSFER => true,
21+
CURLOPT_URL => $url,
22+
];
23+
24+
curl_setopt_array($handle, $options);
25+
26+
curl_exec($handle);
27+
28+
$httpStatusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
29+
30+
self::assertTrue(in_array($httpStatusCode, $successfulHttpStatusCodes, true), sprintf(
31+
'Failed asserting that the URL "%s" returns a successful HTTP response status code, got "%d" instead.',
32+
$url,
33+
$httpStatusCode,
34+
));
35+
}
36+
37+
/**
38+
* @return \Generator<string, array{0: string}>
39+
*/
40+
public static function provideUrl(): \Generator
41+
{
42+
$httpHost = getenv('HTTP_HOST');
43+
44+
if (!is_string($httpHost)) {
45+
throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.');
46+
}
47+
48+
$pathToRoot = realpath(__DIR__ . '/../..');
49+
50+
$patterns = [
51+
$pathToRoot . '/*.php',
52+
$pathToRoot . '/archive/*.php',
53+
$pathToRoot . '/conferences/*.php',
54+
$pathToRoot . '/license/*.php',
55+
$pathToRoot . '/manual/*.php',
56+
$pathToRoot . '/manual/en/*.php',
57+
$pathToRoot . '/releases/*.php',
58+
$pathToRoot . '/releases/*/*.php',
59+
$pathToRoot . '/releases/*/*/*.php',
60+
];
61+
62+
foreach ($patterns as $pattern) {
63+
$pathsToFiles = glob($pattern);
64+
65+
$paths = str_replace($pathToRoot, '', $pathsToFiles);
66+
67+
foreach ($paths as $path) {
68+
$url = sprintf(
69+
'http://%s%s',
70+
$httpHost,
71+
$path,
72+
);
73+
74+
yield $url => [
75+
$url,
76+
];
77+
}
78+
}
79+
}
80+
}

tests/EndToEnd/paths-return-http-response-status-code-200-301-or-302.phpt

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</source>
3333
<testsuites>
3434
<testsuite name="end-to-end">
35-
<directory suffix=".phpt">EndToEnd/</directory>
35+
<directory suffix=".php">EndToEnd/</directory>
3636
</testsuite>
3737
<testsuite name="unit">
3838
<directory suffix=".phpt">Unit/</directory>

0 commit comments

Comments
 (0)