Author: Andreas Möller (localheinz)
Committer: GitHub (web-flow)
Pusher: saundefined
Date: 2024-02-13T14:13:51+03:00

Commit: 
https://github.com/php/web-php/commit/9705aae05abed81ffae9fa5acba97766ca858cc3
Raw diff: 
https://github.com/php/web-php/commit/9705aae05abed81ffae9fa5acba97766ca858cc3.diff

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

Changed paths:
  A  tests/EndToEnd/SmokeTest.php
  D  tests/EndToEnd/paths-return-http-response-status-code-200-301-or-302.phpt
  M  composer.json
  M  tests/phpunit.xml


Diff:

diff --git a/composer.json b/composer.json
index 42003e07cd..2b512311e5 100644
--- a/composer.json
+++ b/composer.json
@@ -20,6 +20,11 @@
             "phpweb\\": "src/"
         }
     },
+    "autoload-dev": {
+        "psr-4": {
+            "phpweb\\Test\\EndToEnd\\": "tests/EndToEnd/"
+        }
+    },
     "config": {
         "platform": {
             "php": "8.2.0"
diff --git a/tests/EndToEnd/SmokeTest.php b/tests/EndToEnd/SmokeTest.php
new file mode 100644
index 0000000000..a902df6adc
--- /dev/null
+++ b/tests/EndToEnd/SmokeTest.php
@@ -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,
+                ];
+            }
+        }
+    }
+}
diff --git 
a/tests/EndToEnd/paths-return-http-response-status-code-200-301-or-302.phpt 
b/tests/EndToEnd/paths-return-http-response-status-code-200-301-or-302.phpt
deleted file mode 100644
index 15f1bc8bde..0000000000
--- a/tests/EndToEnd/paths-return-http-response-status-code-200-301-or-302.phpt
+++ /dev/null
@@ -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) {
-}
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
index c8343aa037..3eac985e8a 100644
--- a/tests/phpunit.xml
+++ b/tests/phpunit.xml
@@ -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>

-- 
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to