我有一个带有后端的Symfony 3.2项目。每个实体都有其 CRUD 控制器、视图等。我准备了
abstract class AbstractControllerTest extends WebTestCase
一个作为每个实体测试的基础。对于每个实体,我使用一个简单的测试来断言 list、show、edit 和 new 返回HTTP 200。
因此,当我运行所有测试时,它会为每个实体测试列表、显示等。问题是在控制器列表中我使用默认顺序的 KNPPaginator。控制器工作正常,但是当我运行测试并到达第二个实体时,由于缺少实体字段,我收到 500 错误。事实证明,该测试从先前的测试中获取了一个 List Query for Pager。因此,默认情况下,实体 A 使用位置字段进行排序。实体 B 没有位置字段,这会导致错误。因此,当 PHPUnit 去测试 A 实体时它是好的,然后它移动到测试 B 实体然后出现错误。我不知道发生了什么,因为排序未保存在会话中,因此 PHPUnit 无法从先前实体的会话中获取查询。有什么想法吗?
抽象控制器测试
abstract class AbstractControllerTest extends WebTestCase
{
/** @var Client $client */
public $client = null;
protected $user = '';
protected $prefix = '';
protected $section = '';
protected $entityId = '';
public function setUp()
{
$this->client = $this->createAuthorizedClient();
}
/**
* @return Client
*/
protected function createAuthorizedClient()
{
$client = static::createClient();
$client->setServerParameter('HTTP_HOST', $client->getContainer()->getParameter('test_info_domain'));
$client->setServerParameter('HTTPS', true);
$client->followRedirects();
$container = $client->getContainer();
$session = $container->get('session');
/** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
$userManager = $container->get('fos_user.user_manager');
/** @var $loginManager \FOS\UserBundle\Security\LoginManager */
$loginManager = $container->get('fos_user.security.login_manager');
$firewallName = $this->section;
/** @var UserInterface $userObject */
$userObject = $userManager->findUserBy(array('username' => $this->user));
$loginManager->logInUser($firewallName, $userObject);
// save the login token into the session and put it in a cookie
$container->get('session')->set('_security_' . $firewallName,
serialize($container->get('security.token_storage')->getToken()));
$container->get('session')->save();
$client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
return $client;
}
public function testIndex()
{
//CRUD index
$this->client->request('GET', sprintf('/%s/%s',$this->section,$this->prefix));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
public function testShow()
{
//CRUD show
$this->client->request('GET', sprintf('/%s/%s/%s/show',$this->section,$this->prefix, $this->entityId));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
public function testEdit()
{
//CRUD edit
$this->client->request('GET', sprintf('/%s/%s/%s/edit',$this->section,$this->prefix, $this->entityId));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
public function testNew()
{
//CRUD new
$this->client->request('GET', sprintf('/%s/%s/new',$this->section,$this->prefix));
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
}
}
以及一个实体的控制器测试类之一的示例
class AgendaCategoryControllerTest extends AbstractControllerTest
{
protected $user = 'tom@test.com';
protected $section = 'admin';
protected $prefix = 'agenda-category';
protected $entityId = '40';
}
如果我分开跑
php phpunit.phar src/Bundle/Tests/Controller/Admin/AControllerTest.php
和
php phpunit.phar src/Bundle/Tests/Controller/Admin/BControllerTest.php
没关系。如果一起运行会有这个奇怪的错误
php phpunit.phar -c phpunit.xml.dist --testsuite=Admin