【问题标题】:how to test slim middleware (phpunit)如何测试苗条的中间件(phpunit)
【发布时间】:2016-09-21 20:27:18
【问题描述】:

我创建了一个中间件,它应该只将用户重定向到另一个网站(在请求 URL 中由参数重定向给出)

class Middleware
{
    public function __invoke($request, $response, $next)
    {
        // Call next middleware or app
        $response = $next($request, $response);

        $redirectUrl = //get redirect url

        return $response->withStatus(200)->withHeader('Location', $redirectUrl);
    }
}

我已经对此进行了测试,并且重定向工作正常。所以我来到那个点来编写单元测试。我失败了……这是我的尝试:

class MiddlewareTest extends \PHPUnit_Framework_TestCase
{
    public $request = array(...); //inserted needed properties

    public function testInvoke(String $url) {
        $next = function () : bool
        {
            return true;
        }; //empty function
        $request['request']['scriptUri'] = "/parameterStuff&redirect=" . $url; //overwrite the Uri with provided Url
        $redirect = new Middleware($request, array(), $next);

        //just to test if result of response still empty
        $iCount = count((array)$redirect);
        $this->assertEquals(0, $iCount);

    }

    public function invokeProvider() : array
    {
        return array(
            array('http://example.com')
        );
    }
}

这个测试是成功的,但它不应该......这个函数的返回应该是一个有效的响应。我在浏览器中对此进行了测试并回显了返回。它在那里有一个值,它是带有预期标题的正确响应。我在单元测试中收到的返回值是一个空对象。

我将 Slim Documentation 标记为响应对象,它是:

此方法返回具有新标头值的 Response 对象的副本。 所以我绝对应该从中得到一些东西。我还尝试返回响应的副本:

$copyresponse = response->withStatus(200)->withHeader('Location', $redirectUrl);
return $copyresponse;

这也不行。任何想法可能导致我的问题以及如何解决它?

(我想测试在响应中是否正确设置了重定向 url,以确保重定向可以正常工作)

【问题讨论】:

    标签: unit-testing redirect phpunit response slim


    【解决方案1】:

    您必须模拟请求并检查Location 标头是否设置正确,其长度为1,状态码为200。我写了somedifferentmiddleware,我用了这个方法。

    class LocationTest extends \PHPUnit_Framework_TestCase
    {
        /**
         * PSR7 request object.
         *
         * @var Psr\Http\Message\RequestInterface
         */
        protected $request;
    
        /**
         * PSR7 response object.
         *
         * @var Psr\Http\Message\ResponseInterface
         */
        protected $response;
    
        protected $headers;
    
        protected $serverParams;
    
        protected $body;
    
        /**
         * Run before each test.
         */
        public function setUp()
        {
            $uri = Uri::createFromString('https://example.com:443/foo/bar');
            $this->headers = new Headers();
            $this->headers->set('REMOTE_ADDR', '127.0.0.1');
            $this->cookies = [];
            $env = Environment::mock();
            $this->serverParams = $env->all();
            $this->body = new Body(fopen('php://temp', 'r+'));
            $this->response = new Response();
            $this->request = new Request('GET', $uri, $this->headers, $this->cookies, $this->serverParams, $this->body);
        }
    
        /**
         * @dataProvider locationProvider
         */
        public function testLocation($url)
        {
            $options = array(
              'ip' => '192.*',
            );
            $mw = new RestrictRoute($options);
    
            $next = function ($req, $res) {
                return $res;
            };
            $uri = Uri::createFromString('https://example.com:443/foo/bar?redirect=' . $url);
            $this->request = new Request('GET', $uri, $this->headers, $this->cookies, $this->serverParams, $this->body);
            $redirect = $mw($this->request, $this->response, $next);
            $location = $redirect->getHeader('Location');
            $this->assertEquals($redirect->getStatusCode(), 200);
            $this->assertEquals(count($location), 1);
            $this->assertEquals($location[0], $url);
        }
    
        public function locationProvider(){
          return [
            ['http://www.google.it'],
            ['http://stackoverflow.com/'],
          ];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-19
      • 2018-08-28
      • 2020-07-01
      • 2018-09-05
      • 2020-03-28
      • 2022-01-26
      • 2020-10-05
      • 1970-01-01
      相关资源
      最近更新 更多