定制 giberti/phpunit-local-server 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

giberti/phpunit-local-server

Composer 安装命令:

composer require giberti/phpunit-local-server

包简介

A local HTTP server for PHPUnit tests

README 文档

README

Build and Test

Provides an HTTP server test case for PHPUnit. The server is powered by PHP's built-in server for testing of network related calls.

Installing

This library requires PHP 7.3 or newer, including PHP 8.0 through 8.5. It will run with PHPUnit versions 8, 9, 10, and 11.

composer require giberti/phpunit-local-server

Usage

  • Create a directory that will contain the code you want to execute
  • Extend the \Giberti\PHPUnitLocalServer\LocalServerTestCase as if you were extending \PHPUnit\Framework\TestCase
  • Start a server in the test method or for the entire class
  • Make requests against the server

Usage Tips

  • Whenever possible, re-use the existing server. Frequent restarts will slow down your tests.
  • You can provide a different php binary by overriding the static $phpBinary property on the class.
A single test

Call either the createServerWithDocroot() or createServerWithRouter() helper method and then execute your test.

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');
    }
}
Several tests using the same configuration

To optimize performance of your tests, it's best to re-use the server whenever possible. To make this easier, simply start the server at the beginning of the class by defining a setupBeforeClass() method with your desired configuration.

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');
    }
}
Modifying the server runtime version

It's possible to run the server under a different PHP runtime than the version running your test suite. This can help with testing your code under multiple versions of PHP. In the example below, the server will start with the PHP 7.3 and 8.1 executable in /usr/local/bin/ on the host test system. Your path may be different.

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');
    }
}

Methods

The following methods are provided to interact with the local server.

public bool LocalServerTestCase::createServerWithDocroot(string $docroot)

Creates a local server using a document root.

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

public bool LocalServerTestCase::createServerWithRouter(string $router)

Creates a local server using a router file. If you are using a framework, this is most likely the index.php file in your document route.

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

public void LocalServerTestCase::destroyServer(void)

Removes the local server. Useful to reset the session state. This is automatically called in the tearDownAfterClass() lifecycle method.

static::destroyServer();

public string LocalServerTestCase::getServerUrl(void)

The port for the server will usually be 8000, however, it is dynamically assigned in the event of a conflict. The safest way to access the host is to call the getServerUrl() method and use that as the root for any Url construction.

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

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

giberti/phpunit-local-server 适用场景与选型建议

giberti/phpunit-local-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 317 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 giberti/phpunit-local-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 giberti/phpunit-local-server 我们能提供哪些服务?
定制开发 / 二次开发

基于 giberti/phpunit-local-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 317
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 4
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-13