CakePHP 3.4.2测试POST的响应始终返回NULL [英] CakePHP 3.4.2 Testing POST's response always returning NULL

查看:109
本文介绍了CakePHP 3.4.2测试POST的响应始终返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在测试一个仅按给定ID搜索记录的应用。
可以正常工作,但是测试拒绝在代码中返回响应。奇怪的是,它仅在CLI中显示。

i'm currently testing an app that simply searches a record by the given id. It works fine but the testing refuses to return the response in the code. Strangely it is ONLY shown in the CLI.

我正在使用cakephp提供的phpunit:

I'm using phpunit provided by cakephp:


phpunit / phpunit: ^ 5.7 | ^ 6.0

"phpunit/phpunit": "^5.7|^6.0"

这是有冲突的代码:

 $this->post('/comunas/findByBarrio',[
        'barrio_id'=>1
 ]);
 var_dump($this->_response->body());die(); //This is just a test which always returns NULL... while the CLI shows the actual response, which is a JSON.

在执行GET或POST到任何其他操作时,也会发生相同的问题。
但是这是目标控制器的代码:

Also the same problem occurrs while doing GET or POST to any other action. But here is the targeted controller's code:

public function findByBarrio()
{
    $this->autoRender = false;
    if ($this->request->is('POST'))
    {
        $data = $this->request->getData();
        if (!empty($data['barrio_id']))
        {
            $this->loadModel('Comuna');

            $barrio_id = $data['barrio_id'];

            $comuna = $this->Comuna->find('list',['conditions' => ['barrio_id'=>$barrio_id]])
                ->hydrate(false)
                ->toArray();
            if ($comuna)
            {
                echo json_encode($comuna);
            }
            else
            {
                throw new NotFoundException('0');
                //echo 0; //Comuna no encontrada para el barrio recibido
            }               
        }
        else
        {
            echo -1;
        }
    }
}

在此先谢谢您!

更新1 :我只能通过在 $周围使用 ob_start()和 ob_get_clean()来获得输出this-> post方法。我希望有一种更清洁的方法...

UPDATE 1: I've only managed to get the output by using "ob_start()" and "ob_get_clean()" around the "$this->post" method. I wish there were a cleaner way though...

更新2 :现在可以了!只需使用符合PSR-7的界面即可。谢谢!
这是更正后的控制器:

UPDATE 2: Now it's working! Just by using the PSR-7 compliant interface. Thank you! Here is the corrected controller:

public function findByBarrio()
{
    $this->autoRender = false;

    $this->response = $this->response->withType('json'); //CORRECTION

    if ($this->request->is('POST'))
    {
        $data = $this->request->getData();
        if (!empty($data['barrio_id']))
        {
            $this->loadModel('Comuna');

            $barrio_id = $data['barrio_id'];

            $comuna = $this->Comuna->find('list',['conditions' => ['barrio_id'=>$barrio_id]])
                ->hydrate(false)
                ->toArray();
            if ($comuna)
            {
                $json = json_encode($comuna);

                $this->response->getBody()->write($json); //CORRECTION

            }
            else
            {
                //Comuna no encontrada para el barrio recibido
                $this->response->getBody()->write(0); //CORRECTION
            }               
        }
        else
        {
            //No se recibió el barrio
            $this->response->getBody()->write(-1); //CORRECTION
        }
    }

    return $this->response; //CORRECTION
}


推荐答案

控制器操作是即使在某些情况下(甚至在大多数情况下)都可以工作,也不应该回显数据。输出不是来自渲染视图模板的数据的正确方法是配置并返回响应对象,或使用序列化视图。

Controller actions are not supposed to echo data, even though it might work in some, maybe even most situations. The correct way of outputting data that doesn't stem from a rendered view template, is to configure and return the response object, or to use serialized views.

测试环境依靠正确地执行此操作,因为它不会缓冲可能的输出,但是会使用从控制器操作返回的实际值。

The test environment relies on doing this properly, as it doesn't buffer possible output, but will use the actual value returned from the controller action.

以下基本上是 https://stackoverflow.com/a/42379581/1392379

The following is basically a copy from https://stackoverflow.com/a/42379581/1392379

文档报价:


控制器操作通常使用 Controller :: set()创建一个View用来渲染视图层的上下文。由于CakePHP使用的约定,因此无需手动创建和呈现视图。相反,一旦控制器动作完成,CakePHP将处理渲染并交付View。

Controller actions generally use Controller::set() to create a context that View uses to render the view layer. Because of the conventions that CakePHP uses, you don’t need to create and render the view manually. Instead, once a controller action has completed, CakePHP will handle rendering and delivering the View.

如果由于某些原因您想跳过默认行为,则可以返回动作中具有完全创建的响应的 Cake\Network\Response 对象。

If for some reason you’d like to skip the default behavior, you can return a Cake\Network\Response object from the action with the fully created response.

*从3.4开始,将是 \Cake\Http\Response

食谱>控制器>控制器动作

Cookbook > Controllers > Controller Actions

$content = json_encode($comuna);

$this->response->getBody()->write($content);
$this->response = $this->response->withType('json');
// ...

return $this->response;

符合PSR-7的接口使用不可变的方法,因此使用了<$ c的返回值$ c> withType()。与设置标头和内容不同,通过写入现有流来更改正文不会更改响应对象的状态。

The PSR-7 compliant interface uses immutable methods, hence the utilization of the return value of withType(). Unlike setting headers and stuff, altering the body by writing to an existing stream doesn't change the state of the response object.

CakePHP 3.4.3将添加不可变的 withStringBody 方法,可以替代使用该方法来写入现有流。

CakePHP 3.4.3 will add an immutable withStringBody method that can be used alternatively to writing to an existing stream.

$this->response = $this->response->withStringBody($content);



使用已淘汰的界面



Using the deprecated interface

$content = json_encode($comuna);

$this->response->body($content);
$this->response->type('json');
// ...

return $this->response;



使用序列化视图



Use a serialized view

$content = json_encode($comuna);

$this->set('content', $content);
$this->set('_serialize', 'content');

这还需要使用请求处理程序组件,并启用扩展解析和使用带有<附加code> .json ,或发送带有 application / json accept标头的正确请求。

This requires to also use the request handler component, and to enable extensing parsing and using correponsing URLs with .json appended, or to send a proper request with a application/json accept header.

  • Cookbook > Controllers > Controller Actions
  • Cookbook > Views > JSON and XML views
  • PHP FIG Standards > PSR-7 HTTP message interfaces

这篇关于CakePHP 3.4.2测试POST的响应始终返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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