我有以下控制器:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SidebarController extends Controller
{
public function displayCurrentUserInfoToTheSideBarAction()
{
$auth_checker = $this->get('security.authorization_checker');
$token = $this->get('security.token_storage')->getToken();
$user = $token->getUser();
return $this->render('widgets/sidebar_profile.html.twig',['user'=>$user]);
}
}
我想以某种方式对其进行测试,但是我被卡住了,因为我通过细枝的render
函数调用了此方法:
{{ render(controller('AppBundle:Sidebar:displayCurrentUserInfoToTheSideBar')) }}
我实际上不希望确保的是该特定控制器呈现正确的html。通常在交响控制器上进行功能测试是通过模拟首页访问并检查任何与http相关的信息(例如标题,响应代码)并解析html,以查看网站的行为是否应为正常,例如:
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$response=$client->getResponse();
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('/login',$response->headers->get('Location'));
}
}
此测试检查对主页的访问是否重定向到登录屏幕。但是在上面的控制器上,我如何执行类似的测试?