PhalconPHP MVC Micro应用程序:指定请求路径并声明响应代码 [英] PhalconPHP MVC Micro app: Specify a request path and assert the response code

查看:164
本文介绍了PhalconPHP MVC Micro应用程序:指定请求路径并声明响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了单元测试教程,并对其进行了修改,以基于对此信息.我可以成功验证输出字符串,但是不确定如何声明响应状态代码或更改请求路径.

I've followed the unit testing tutorial and modified it to test a HTTP request to Micro MVC app, based on this post. I can successfully validate the output string, however I'm not sure how to assert the response status code or change the request path.

index.php

index.php

<?php

$app = new \Phalcon\Mvc\Micro();

#Default handler for 404
$app->notFound(function () use ($app) {
    $app->response->setStatusCode(404, "Not Found")->sendHeaders();
});

$app->post('/api/robots', function() use ($app) {
    //Parse JSON as an object
    $robot = $app->request->getJsonRawBody();
    //Build the response
    $app->response->setJsonContent($robot);
    return $app->response;
});

$app->get('/', function() {
    echo 'Hello';
});

$app->handle();

tests/UnitTest.php

tests/UnitTest.php

class MvcMicroUnitTest extends \UnitTestCase {

    public function testNotFound() {
        $path = '/invalid';
        $mockRequest = $this->getMock("\\Phalcon\\Http\\Request");
        //TODO: Set an invalid URL $path in the mock
        $this->di->set('request', $mockRequest, true);
        include("../index.php");
        //TODO: Assert status is 404
        $this->expectOutputString('');
    }

    public function testPostRobot() {
        $rawJson = '{"name":"C-3PO","type":"droid","year":1977}';
        $path = '/api/robots';
        $mockRequest = $this->getMock("\\Phalcon\\Http\\Request", array(
            "getJsonRawBody"));
        $mockRequest->expects($this->any())
                ->method("getRawBody")
                ->will($this->returnValue($rawJson));
        //TODO: Set the $path in the mock
        $this->di->set('request', $mockRequest, true);
        include("../index.php");
        //TODO: Assert status is 200
        $this->expectOutputString($rawJson);
    }
}

推荐答案

好消息和坏消息.很好:就您使用标准调度原则而言,您将获得一个响应,其中将包含您需要的信息.一个小技巧–状态成功时,标题设置为false.

Good news and bad news. Good: as far as you use the standard dispatching principle you will have a response, that would contain the information you need. Small trick – when status is success the header is set to false.

/**
 * @param $expected
 * @throws ExpectationFailedException
 * @return $this
 */
protected function assertResponseCode($expected)
{
    $actual = $this->di->getResponse()->getHeaders()->get('Status');

    if ($actual !== false && $expected !== 200 && !preg_match(sprintf('/^%s/', $expected), $actual)) {
        throw new ExpectationFailedException(sprintf('Failed asserting that response code is "%s".', $expected));
    }

    $this->assertTrue(true);
    return $this;
}

不好:您做错了方法.这是功能/验收测试的领域,有一个很棒的框架,称为 Behat .您应该进行自己的研究,但从本质上讲,尽管PHPUnit擅长测试或多或少的独立功能块,但它却可以测试诸如完整请求执行之类的较大功能.稍后,您将开始遇到会话错误,配置错误的环境等问题,所有这些都是因为每个请求都应该在其自己的单独空间中执行,并且您迫使它执行相反的操作.另一方面,Behat的工作方式却非常不同,对于每种情况(发布机器人,查看不存在的页面),它都会向服务器发送一个新请求并检查结果.它主要用于通过对最终结果(响应对象/html/json)进行断言来对所有共同工作的最终测试.

Bad: you are doing it the wrong way. This is area of functional / acceptance testing and there is a fabulous framework called Behat. You should do your own research, but essentially, while PHPUnit is great at testing more or less independent blocks of functionality it sucks at testing bigger things like full request execution. Later you will start experiencing issues with session errors, misconfigured environment, etc., all because each request is supposed to be executed in it's own separate space and you force it into doing the opposite. Behat on the other hand works in a very different way, where for each scenario (post robot, view non-existing page), it sends a fresh request to the server and checks the result. It is mostly used for final testing of everything working together by making assertions on the final result (response object / html / json).

这篇关于PhalconPHP MVC Micro应用程序:指定请求路径并声明响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆