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在响应正确设置,以确保重定向将努力测试)