【问题标题】:Symfony2 functional test client request returns error 500Symfony2 功能测试客户端请求返回错误 500
【发布时间】:2015-09-24 01:08:57
【问题描述】:

我有一个控制器,我正在尝试对其进行功能测试。

控制器:

<?php

namespace Zanox\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Exception;

/**
 * 
 * @author Mohamed Ragab Dahab <eng.mohamed.dahab@gmail.com>
 * 
 * @Route("merchant")
 * 
 */
class ReportController extends Controller {

    /**
     * Show transaction report regarding to the given merchant ID
     * @author Mohamed Ragab Dahab <eng.mohamed.dahab@gmail.com>
     * @access public
     * 
     * @Route("/{id}/report", name="merchant-report")
     * 
     * @param int $id   Merchant ID
     */
    public function showAction($id) {
        try {
            //Order Service
            $orderService = $this->get('zanox_app.orderService');

            //merchant Orders
            $orders = $orderService->getMerchantOrders($id);

            //render view and pass orders array
            return $this->render('ZanoxAppBundle:Report:show.html.twig', ['orders' => $orders]);
        } catch (Exception $e) {
            //log errors
        }
    }

}

我创建了如下功能测试:

namespace Zanox\AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ReportControllerTest extends WebTestCase {

    /**
     * 
     */
    public function testShow() {
        //Client instance
        $client = static::createClient();

        //Act like browsing the merchant listing page, via GET method
        $crawler = $client->request('GET', '/merchant/{id}/report', ['id'=> 1]);

        //Getting the response of the requested URL
        $crawlerResponse = $client->getResponse();

        //Assert that Page is loaded ok
        $this->assertEquals(200, $crawlerResponse->getStatusCode());

        //Assert that the response content contains 'Merchant Listing' text
        $this->assertTrue($crawler->filter('html:contains("Merchant Report")')->count() > 0);
    }

}

但是,此测试失败,因为第一个断言返回状态 500 而不是 200

测试日志显示: [2015-07-06 21:00:24] request.INFO:匹配路由“商家报告”。 {"route_parameters":{"_controller":"Zanox\AppBundle\Controller\ReportController::showAction","id":"{id}","_route":"merchant-report"},"request_uri":"@ 987654321@{id}/report?id=1"} []

让您知道 ['id' => 1] 存在于 DB 中。

第一个问题:为什么会失败?

第二个问题:我是否以正确的方式进行功能测试?

【问题讨论】:

  • 你检查错误日志了吗?
  • 该日志显示尝试请求的 URL 是:localhost/merchant{id}/report?id=1
  • @Ragaolla 可以吗?

标签: php symfony phpunit functional-testing


【解决方案1】:

如果您查看日志,您会发现 {id} 参数未正确替换,而是添加到了 Uri 的查询字符串中。所以尝试一下:

$crawler = $client->request('GET', sprintf('/merchant/%d/report', 1));

当使用GET时,第三个参数会为URI添加查询参数,当使用POST时,这些数据将被发布。

【讨论】:

    【解决方案2】:

    至于失败的原因 - 您可以通过在测试中执行控制器代码时使用调试器单步执行控制器代码来解决问题。对于第二个问题,是的,您正在正确地进行简单的功能测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2015-06-08
      • 2023-03-14
      相关资源
      最近更新 更多