mjordan/test_rest_server

PHP库,用于构建用于测试目的的REST服务器。

dev-master 2018-01-26 06:12 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:43 UTC


README

简单的实用类,用于创建适合测试REST客户端的本地Web服务器。它使用PHP内置的Web服务器来提供HTTP响应,包括状态码、头和正文。当你在PHPUnit(或SimpleTest等)测试中实例化服务器时,你提供预期的响应细节。你的被测试客户端可以完全访问这个响应。你还可以使用“模板”添加复杂的测试服务器逻辑。

需求

  • PHP 5.5.0或更高版本(已在PHP 5.6、7.0和7.1上测试)。
  • Composer

安装

  1. git https://github.com/mjordan/test_rest_server.git
  2. cd test_rest_server
  3. php composer.phar install(或系统上的等效命令,例如./composer install

或者,使用composer

composer require mjordan/test_rest_server dev-master

并在composer.json文件中

    "require": {
        "mjordan/test_rest_server": "dev-master"
    }

使用

要使用此测试服务器,你需要创建一个TestRestServer实例,它接受四个参数

  • URI(字符串):相对于服务器根的路径。默认服务器模板忽略URI,但提供一个作为代码中测试文档的一种形式是有用的。如果你需要一个对不同URI响应不同的服务器,你可以使用一个检查$_SERVER['REQUEST_URI']值的自定义模板并相应地响应。
  • 响应代码(整数):200、201、401等。
  • 头部(可选;字符串数组):你希望服务器包含在响应中的任何头部。
  • 正文(可选;字符串):响应正文的内文。
$this->server = new TestRestServer('/testing/foo', 201, array('Content-Type: text/plain'), 'Is this thing on?');

实例化你的服务器后,你启动它(使用start()方法)。此时,HTTP客户端可以击中服务器,并将响应值返回。

使用PHPUnit的基本示例

<?php

namespace mjordan\TestRestServer;

use mjordan\TestRestServer\TestRestServer;
// Works with non-Guzzle clients too. It's a real HTTP server!
use GuzzleHttp\Client as GuzzleClient;

// PHPUnit is not the only test tool this will work with. Any PHP test tool is OK.
class ExampleTest extends \PHPUnit\Framework\TestCase
{
    public function testExample()
    {
        $this->server = new TestRestServer('/testing/foo', 201, array('Content-Type: text/plain'), 'Is this thing on?');
        // You can pass a port number into start() if you want. The default is 8001.
        $this->server->start();

        $client = new \GuzzleHttp\Client();
        // Make sure the port number in your request is the same as the
        // one the test server is running on.
        $response = $client->post('https://:8001/testing');
        $response_body = (string) $response->getBody();

        $this->assertEquals('Is this thing on?', $response_body);
        $this->assertEquals(201, $response->getStatusCode());
    }
}

PHPUnit的输出

PHPUnit 4.8.36-1-g18e5f52 by Sebastian Bergmann and contributors.
.

Time: 5.08 seconds, Memory: 7.25MB

OK (1 test, 2 assertions)

更有用的示例

测试服务器的真正用途是可以用来测试包含REST客户端的类。换句话说,你不是直接测试客户端,而是测试使用HTTP客户端的代码。

想象一个简单的类,Sample。它有一个方法request(),该方法使用REST客户端确定属性foo的值

<?php

namespace mjordan\Sample;

use GuzzleHttp\Client as GuzzleClient;

/**
 * Test REST Server Sample Class.
 */
class Sample
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->client = new GuzzleClient();
    }

    public function request()
    {
        $response = $this->client->get('https://:8001/somepath');
        if ($response->getStatusCode() == 200) {
            $this->foo = 'bar';
        }
    }
}

测试

<?php

namespace mjordan\TestRestServer;

use mjordan\TestRestServer\TestRestServer;
use mjordan\Sample\Sample;

class ClassTest extends \PHPUnit\Framework\TestCase
{
    public function testExample()
    {
        $this->server = new TestRestServer('/testing/foo', 200);
        $this->server->start();
 
        $sample = new Sample();
        $sample->request();

        $this->assertEquals('bar', $sample->foo);
    }
}

使用服务器模板

你可以通过将模板的路径作为$server->start()(第一个参数是服务器将运行的端口号,默认为8001)的第二个参数传递来使用自定义测试服务器模板。路径参数的值应该是输出PHP代码的Twig模板文件的完整路径。

  • 你可以在PHP代码中做任何你想做的事情。
  • 你不需要在模板中传递URI、响应代码、头部或正文值,但如果你想也可以。在模板中,它们将可访问,如下所示
    • headers(数组)
    • code(整数)
    • body(字符串)

服务器响应的URI可在$_SERVER['REQUEST_URI']中找到。

$uri = '/testing/foo';
$code = 201;
$headers = array('Content-Type: text/plain');
$path_to_template = '/tmp/my_server_template.tpl';

$this->server = new TestRestServer($uri, $code, $headers, '');
$this->server->start('8001', $path_to_template);

这是一个使用一个变量body的示例服务器模板

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['REQUEST_URI'] == '/foo/bar') {
    // Some logic goes here to generate the response code or
    // headers based on $_SERVER variables.

    http_response_code(201);
    header("Content-Type: application/json");
    // We get the body of the request from our TestRestServer instance.
    print json_encode({{ body }});
}

维护者

开发和反馈

欢迎建议、用例和错误报告。如果你想打开一个pull request,请首先打开一个问题。

要运行测试,请运行composer tests。要运行PSR2代码风格检查,请运行composer style

许可证

Unlicense