2016-09-14 47 views
0

我创建了一个中间件至极应该只将用户重定向到其他网站苗条中间件(PHPUnit的)(由参数重定向的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); 
    } 
} 

我已经testet这和重定向作品精细。所以我来到这个点写单元测试。我失败了......这是我的尝试:

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') 
     ); 
    } 
} 

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

我红修身Documentation关于响应对象,并将其最高审计机关:

此方法返回具有新报头值响应对象的副本。 所以我应该从它收到的东西。我也试图返回响应的副本:

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

这不工作为好。任何想法可能导致我的问题以及如何解决它?

(我想如果重定向URL在响应正确设置,以确保重定向将努力测试)

回答

0

你要嘲笑请求,并检查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/'], 
     ]; 
    } 
}