creativestyle/app-http-server-mock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

creativestyle/app-http-server-mock

Composer 安装命令:

composer require creativestyle/app-http-server-mock

包简介

An minimal app http server based on PHP cli-server for use in tests

README 文档

README

Travis Build Status

PHP HTTP App Server for use in tests

This library allows to query HTTP endpoints in your unit/integration tests without spinning up a whole webserver.

It uses PHP's built-in web-server underneath, but it's completely opaque and you don't have to worry about anything.

Usage

WARNING This has to be installed as a composer dependency - it may not work if you just drop it in.

composer require --dev creativestyle/app-http-server-mock

Now you need to subclass Creativestyle\AppHttpServerMock\Server and implement the only abstract method registerRequestHandlers:

<?php

namespace YourNamespace;

use Creativestyle\AppHttpServerMock\Server;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class YourTestServer extends Server
{
    protected function registerRequestHandlers()
    {
        $this->registerRequestHandler('GET', '/', function(Request $request) {
            return new Response('Hello');
        });
        
        $this->registerRequestHandler(['PUT', 'POST'], '/number/(?<num>\d+)/test', function(Request $request, array $args) {
            return [
                'arrays' => [
                    'are',
                    'transformed',
                    'into',
                    'json' => ['how' => 'automatically']
                ],
                'your_method' => $request->getMethod(),
                'your_number' => $args['num']
            ];
        });
    }
}

And now you can just use your server in the tests:

<?php

namespace YouNamespace\Tests;

use YourNamespace\YourTestServer;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use PHPUnit\Framework\TestCase;

class YourTest extends TestCase
{
    /**
     * @var YourTestServer
     */
    private static $testServer;

    public static function setUpBeforeClass()
    {
        self::$testServer = new YourTestServer();
        self::$testServer->start();
    }

    public static function tearDownAfterClass()
    {
        self::$testServer->stop();
    }

    private function getClient()
    {
        return new Client([
            'base_uri' => self::$testServer->getBaseUrl(),
            'http_errors' => false
        ]);
    }

    public function testSomething()
    {
        $response = $this->getClient()->get('/');

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Hello', $response->getBody()->getContents());
    }
}

Of course you could use the server in the setUp() and tearDown() methods but it's non-optimal from the perf. perspective as the server would be started/stopped before/after each test.

To get more usage examples and see what's possible see the /tests subdirectory of this package - it should be all self-explanatory.

统计信息

  • 总下载量: 2.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固