wellrested/test 问题修复 & 功能扩展

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

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

wellrested/test

最新稳定版本:v2.0.2

Composer 安装命令:

composer require wellrested/test

包简介

Test cases and doubles for use with WellRESTed

README 文档

README

This package provides a handful of test case subclasses for use with PHPUnit and WellRESTed.

To use, add wellrested/test to your Composer's require-dev section.

Test Cases

RequestHandlerTestCase

Subclass WellRESTed\Test\TestCases\RequestHandlerTestCase to test a handler that implements PSR-15's Psr\Http\Server\RequestHandlerInterface. Here's an example:

<?php

use Psr\Http\Server\RequestHandlerInterface;
use WellRESTed\Test\TestCases\RequestHandlerTestCase;

class MyHandlerTest extends RequestHandlerTestCase
{
  public function setUp(): void
  {
    parent::setUp();

    // Configure the default request.
    $this->request = $this->request
      ->withAttribute('id', 12345);
  }

  protected function getHandler(): RequestHandlerInterface
  {
    // Return a configured instance of the handler under test.
    return new MyHandler();
  }

  public function testReturns200()
  {
    // Call `dispatch` to send the request to the handler under test and return
    // the response.
    $response = $this->dispatch();
    $this->assertEquals(200, $response->getStatusCode());
  }
}

MiddlewareTestCase

To test middlware implementing PSR-15's Psr\Http\Server\MiddlewareInterface, subclass WellRESTed\Test\TestCases\MiddlewareTestCase.

<?php

use Psr\Http\Server\RequestHandlerInterface;
use WellRESTed\Message\Response;
use WellRESTed\Test\TestCases\MiddlewareTestCase;

class MyMiddlewareTest extends MiddlewareTestCase
{
  public function setUp(): void
  {
    parent::setUp();

    // Configure the default request.
    $this->request = $this->request
      ->withAttribute('id', 12345);

    // Configure the upstream handler the middleware may call.
    // Set the `response` member to the respone the handler should return.
    $this->handler->response = new Response(200);
  }

  protected function getMiddleware(): MiddlewareInterface
  {
    // Return a configured instance of the middleware under test.
    return new MyMiddleware();
  }

  public function testDelegatesToUpstreamHandler()
  {
    // Call `dispatch` to send the request to the middleware under test and
    // return the response.
    $response = $this->dispatch();

    // You can make assertions on the `handler` member to check if the upstream
    // handler was called.

    // The `called` member will be true if the handler was called.
    $this->assertTrue($this->handler->called);
    // The `request` member will be set with the request passed to the handler.
    $this->assertSame($this->request, $this->handler->request);
  }
}

LegacyMiddlewareTestCase

To test classes implementing the legacy WellRESTed\MiddlewareInterface or legacy callables, use WellRESTed\Test\TestCases\LegacyMiddlewareInterface.

<?php

class MyLegacyMiddlewareTest extends LegacyMiddlewareTestCase
{
  public function setUp(): void
  {
    parent::setUp();

    // Configure the default request.
    $this->request = $this->request
      ->withAttribute('id', 12345);

    // Configure the `next` member.
    $this->next->upstreamResponse = new Response(200);
  }

  protected function getMiddleware()
  {
    // Return the legacy middleware under test.
    return new MyLegacyMiddleware();
  }

  public function testDelegatesToNext()
  {
    // Call `dispatch` to send the request to the middleware under test and
    // return the response.
    $response = $this->dispatch();

    // You can make assertions on the `next` member.

    // The `called` member will be true if `next` was called.
    $this->assertTrue($this->next->called);
    // The `request` member will be set with the request passed to `next`.
    $this->assertSame($this->request, $this->next->request);
  }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-23