You've already forked phpmyadmin
mirror of
https://github.com/phpmyadmin/phpmyadmin.git
synced 2026-10-23 18:29:15 +03:00
138 lines
4.5 KiB
PHP
138 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpMyAdmin\Tests\Utils;
|
|
|
|
use PhpMyAdmin\Dbal\DatabaseInterface;
|
|
use PhpMyAdmin\Http\RequestMethod;
|
|
use PhpMyAdmin\Tests\AbstractTestCase;
|
|
use PhpMyAdmin\Utils\HttpRequest;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Attributes\Medium;
|
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
|
use ReflectionMethod;
|
|
|
|
use function ini_get;
|
|
|
|
#[CoversClass(HttpRequest::class)]
|
|
#[Medium]
|
|
class HttpRequestTest extends AbstractTestCase
|
|
{
|
|
private HttpRequest $httpRequest;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
HttpRequest::setProxySettingsFromEnv();
|
|
|
|
DatabaseInterface::$instance = $this->createDatabaseInterface();
|
|
$this->httpRequest = new HttpRequest();
|
|
}
|
|
|
|
/**
|
|
* Test for http request using Curl
|
|
*
|
|
* @param string $url url
|
|
* @param RequestMethod $method method
|
|
* @param bool $returnOnlyStatus return only status
|
|
* @param bool|string|null $expected expected result
|
|
*/
|
|
#[DataProvider('httpRequests')]
|
|
#[Group('network')]
|
|
#[RequiresPhpExtension('curl')]
|
|
public function testCurl(
|
|
string $url,
|
|
RequestMethod $method,
|
|
bool $returnOnlyStatus,
|
|
bool|string|null $expected,
|
|
): void {
|
|
$result = (new ReflectionMethod(HttpRequest::class, 'curl'))
|
|
->invokeArgs($this->httpRequest, [$url, $method, $returnOnlyStatus]);
|
|
$this->validateHttp($result, $expected);
|
|
}
|
|
|
|
/**
|
|
* Test for http request using fopen
|
|
*
|
|
* @param string $url url
|
|
* @param RequestMethod $method method
|
|
* @param bool $returnOnlyStatus return only status
|
|
* @param bool|string|null $expected expected result
|
|
*/
|
|
#[DataProvider('httpRequests')]
|
|
#[Group('network')]
|
|
public function testFopen(
|
|
string $url,
|
|
RequestMethod $method,
|
|
bool $returnOnlyStatus,
|
|
bool|string|null $expected,
|
|
): void {
|
|
if (! ini_get('allow_url_fopen')) {
|
|
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
|
|
}
|
|
|
|
$result = (new ReflectionMethod(HttpRequest::class, 'fopen'))
|
|
->invokeArgs($this->httpRequest, [$url, $method, $returnOnlyStatus]);
|
|
$this->validateHttp($result, $expected);
|
|
}
|
|
|
|
/**
|
|
* Test for http request using generic interface
|
|
*
|
|
* @param string $url url
|
|
* @param RequestMethod $method method
|
|
* @param bool $returnOnlyStatus return only status
|
|
* @param bool|string|null $expected expected result
|
|
*/
|
|
#[DataProvider('httpRequests')]
|
|
#[Group('network')]
|
|
#[RequiresPhpExtension('curl')]
|
|
public function testCreate(
|
|
string $url,
|
|
RequestMethod $method,
|
|
bool $returnOnlyStatus,
|
|
bool|string|null $expected,
|
|
): void {
|
|
if (! ini_get('allow_url_fopen')) {
|
|
self::markTestSkipped('Configuration directive allow_url_fopen is not enabled.');
|
|
}
|
|
|
|
$result = $this->httpRequest->create($url, $method, $returnOnlyStatus);
|
|
$this->validateHttp($result, $expected);
|
|
}
|
|
|
|
private function validateHttp(string|bool|null $result, string|bool|null $expected): void
|
|
{
|
|
if ($expected === true) {
|
|
self::assertTrue($result);
|
|
} elseif ($expected === false) {
|
|
self::assertFalse($result);
|
|
} elseif ($expected === null) {
|
|
self::assertNull($result);
|
|
} else {
|
|
self::assertIsString($result, 'The request maybe has failed');
|
|
self::assertStringContainsString($expected, $result);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Data provider for HTTP tests
|
|
*
|
|
* @return list<array{string, RequestMethod, bool, bool|string|null}>
|
|
*/
|
|
public static function httpRequests(): array
|
|
{
|
|
return [
|
|
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, true, true],
|
|
['https://www.phpmyadmin.net/test/data', RequestMethod::Post, true, null],
|
|
['https://nonexisting.phpmyadmin.net/test/data', RequestMethod::Get, true, null],
|
|
['https://www.phpmyadmin.net/test/data', RequestMethod::Get, false, 'TEST DATA'],
|
|
['https://www.phpmyadmin.net/test/nothing', RequestMethod::Get, true, false],
|
|
];
|
|
}
|
|
}
|