Symfony4单元测试:带有参数的路由

时间:2018-08-06 21:52:08

标签: unit-testing phpunit symfony4 phpunit-testing

我正在尝试使用PHPUnit学习Symfony4中的单元测试。我有一个控制器MapItemController,它具有通常的CRUD路由。

测试索引路由时,测试通过:

public function testSecuredIndex()
{
    // Fake a session token
    $this->logIn();
    $crawler = $this->client->request('GET', '/map/items');

    $this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
}

但是测试“显示”路线失败:

public function testSecuredShow()
{
    $this->logIn();
    $crawler = $this->client->request('GET', '/map/items/1');

    $this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
}

出现错误:

  

未能断言500等于200。

在/var/logs/test.log的日志中,错误如下:

[2018-08-06 21:33:31] request.CRITICAL: Uncaught PHP Exception 
Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in     
driver: SQLSTATE[HY000] [2002] No such file or directory" at 
/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
line 112 {"exception":"[object] 
(Doctrine\\DBAL\\Exception\\ConnectionException(code: 0): An exception 
occurred in driver: SQLSTATE[HY000] [2002] No such file or directory at 
/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver
/AbstractMySQLDriver.php:112, Doctrine\\DBAL\\Driver\\PDOException(code: 
2002): SQLSTATE[HY000] [2002] No such file or directory at /Users
/cpuzzuol/siccommand/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver  
/PDOConnection.php:50, PDOException(code: 2002): SQLSTATE[HY000] [2002] 
No such file or directory at vendor/doctrine  
/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:46)"} []

我已经阅读了其他几个线程,指出解决方案是在其服务器上安装php-mysql,但是在非测试情况下,我可以很好地连接到mysql。

我假设我在这里缺少一些非常基本的东西,但是任何帮助将不胜感激。

编辑 我想我应该添加“显示”路线中发生的事情...

/**
 * @Route("/map/items/{id}", name="map_items_show")
 */
public function show($id){
  $item = $this->getDoctrine()->getRepository(MapItem::class)->find($id);
  if (!$item) {
    throw $this->createNotFoundException('This map item does not exist.');
  }
  $itemType = $item->getItemType();

  $permissions = json_encode($this->service->getUserMapPermissions());
  return $this->render('map/map_item/show.html.twig', ['id' => $id, 'itemType' => $itemType, 'permissions' => $permissions]);
}

0 个答案:

没有答案