giberti/phpunit-local-server

PHPUnit 测试的本地 HTTP 服务器

v3.0.0 2022-05-01 01:31 UTC

This package is auto-updated.

Last update: 2024-09-09 21:50:12 UTC


README

Build and Test

为PHPUnit提供HTTP服务器测试用例。该服务器由PHP内置服务器提供支持,用于测试与网络相关的调用。

安装

此库需要PHP 7.3或更高版本,包括PHP 8.0, 8.1和8.2。它将与PHPUnit 8和9版本一起运行。

composer require giberti/phpunit-local-server

用法

  • 创建一个包含您要执行的代码的目录
  • 扩展\Giberti\PHPUnitLocalServer\LocalServerTestCase,就像扩展\PHPUnit\Framework\TestCase一样
  • 在测试方法中或对整个类启动服务器
  • 向服务器发送请求

使用提示

  • 尽可能重用现有服务器。频繁重启会减慢您的测试速度。
  • 您可以通过覆盖类上的静态$phpBinary属性来提供不同的php二进制文件。
单个测试

调用createServerWithDocroot()createServerWithRouter()辅助方法,然后执行您的测试。

use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test extends LocalServerTestCase
{
    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');
        $url = $this->getLocalServerUrl() . '/foo';

        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}
使用相同配置的多个测试

为了优化测试性能,最好尽可能重用服务器。为了简化这一点,只需在类开始处定义一个具有所需配置的setupBeforeClass()方法即可启动服务器。

use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test extends LocalServerTestCase
{
    public static function setupBeforeClass() {
        static::createServerWithDocroot('./tests/localhost');
    }

    public function testFoo() {
        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }

    public function testBar() {
        $url = $this->getLocalServer() . '/bar';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}
修改服务器运行时版本

可以在不同于运行测试套件的PHP运行时下运行服务器。这有助于在多个PHP版本下测试您的代码。以下示例中,服务器将在主机测试系统上的/usr/local/bin/目录中使用PHP 7.3和8.1可执行文件启动。您的路径可能不同。

use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test73 extends LocalServerTestCase
{
    static $phpBinary = '/usr/local/bin/php73';

    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');

        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}

class Test81 extends LocalServerTestCase
{
    static $phpBinary = '/usr/local/bin/php81';

    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');

        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}

方法

以下方法提供与本地服务器交互。

public bool LocalServerTestCase::createServerWithDocroot(string $docroot)

使用文档根创建本地服务器。

static::createServerWithDocroot('./path/to/site/files');

public bool LocalServerTestCase::createServerWithRouter(string $router)

使用路由器文件创建本地服务器。如果您正在使用框架,这可能是您的文档路由中的index.php文件。

static::createServerWithRouter('./path/to/router.php');

public void LocalServerTestCase::destroyServer(void)

删除本地服务器。这在重置会话状态时很有用。这将在tearDownAfterClass()生命周期方法中自动调用。

static::destroyServer();

public string LocalServerTestCase::getServerUrl(void)

服务器的端口通常为8000,然而,在发生冲突的情况下,它会被动态分配。访问主机的最安全方式是调用getServerUrl()方法,并使用它作为任何URL构造的根。

$schemeHost = $this->getServerUrl();
$fullUrl    = $schemeHost . "/path/to/file/to/access";

echo $fullUrl; // http://localhost:8000/path/to/file/to/access