用于与 WellRESTed 一起使用的测试用例和双例

v2.0.2 2020-06-26 15:03 UTC

This package is auto-updated.

Last update: 2024-08-27 00:11:54 UTC


README

本软件包为与PHPUnit和WellRESTed一起使用提供了一些测试用例子类。

要使用,请将wellrested/test添加到Composer的require-dev部分。

测试用例

RequestHandlerTestCase

WellRESTed\Test\TestCases\RequestHandlerTestCase派生一个子类来测试实现PSR-15的Psr\Http\Server\RequestHandlerInterface的处理程序。以下是一个示例

<?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

要测试实现PSR-15的Psr\Http\Server\MiddlewareInterface的中间件,从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

要测试实现旧版WellRESTed\MiddlewareInterface或旧版callable的类,请使用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);
  }
}