2016-09-14 83 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/'], 
     ]; 
    } 
}