Php Symfony测试实体
我有实体服务,我创建控制器和操作获取、创建、编辑和删除。我寻找测试,我不知道为什么我有错误?我可以输入这个路由,并有数据,工作正常,但如果创建客户端和获取状态代码有302 但当我在security.yml中发表评论时Php Symfony测试实体,php,symfony,phpunit,Php,Symfony,Phpunit,我有实体服务,我创建控制器和操作获取、创建、编辑和删除。我寻找测试,我不知道为什么我有错误?我可以输入这个路由,并有数据,工作正常,但如果创建客户端和获取状态代码有302 但当我在security.yml中发表评论时 access_control: - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } #- { path: ^/admin, roles: ROLE_ADMIN } 直到深秋,测试才几乎全部通过
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
#- { path: ^/admin, roles: ROLE_ADMIN }
直到深秋,测试才几乎全部通过
如何创建具有角色的客户端\u ADMIN??
这个测试呢
public function testCompleteScenario()
{
// Create a new client to browse the application
$client = static::createClient();
// Create a new entry in the database
$crawler = $client->request('GET', '/admin/services/');
$this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /admin/services/");
$crawler = $client->click($crawler->selectLink('Create a new entry')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array(
'artel_profilebundle_services[services]' => 'Test',
// ... other fields to fill
));
$client->submit($form);
$crawler = $client->followRedirect();
// Check data in the show view
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
// Edit the entity
$crawler = $client->click($crawler->selectLink('Edit')->link());
$form = $crawler->selectButton('Update')->form(array(
'artel_profilebundle_services[services]' => 'Foo',
// ... other fields to fill
));
$client->submit($form);
$crawler = $client->followRedirect();
// Check the element contains an attribute with value equals "Foo"
$this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]');
// Delete the entity
$client->submit($crawler->selectButton('Delete')->form());
$crawler = $client->followRedirect();
// Check the entity has been delete on the list
// **this is 51 line**
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
}
我有
' does not match PCRE pattern "/Foo/".
/home/ivan/host/test/src/Artel/AdminBundle/Tests/Controller/ServicesControllerTest.php:51
错误在哪里
更新
改变
class ServicesControllerTest extends WebTestCase
{
private $client = null;
public function setUp()
{
$this->client = static::createClient();
}
public function logIn()
{
$session = $this->client->getContainer()->get('session');
$firewall = 'default';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
public function testCompleteScenario()
{
// Create a new client to browse the application
$this->logIn();
// Create a new entry in the database
$crawler = $this->client->request('GET', '/admin/services/');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /admin/services/");
$crawler = $this->client->click($crawler->selectLink('Create a new entry')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array(
'artel_profilebundle_services[services]' => 'Test',
// ... other fields to fill
));
$this->client->submit($form);
$crawler = $this->client->followRedirect();
// Check data in the show view
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
// Edit the entity
$crawler = $this->client->click($crawler->selectLink('Edit')->link());
$form = $crawler->selectButton('Update')->form(array(
'artel_profilebundle_services[services]' => 'Foo',
// ... other fields to fill
));
$this->client->submit($form);
$crawler = $this->client->followRedirect();
// Check the element contains an attribute with value equals "Foo"
$this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]');
// Delete the entity
$this->client->submit($crawler->selectButton('Delete')->form());
$crawler = $this->client->followRedirect();
// this is line 73
$this->assertNotRegExp('/Foo/', $this->client->getResponse()->getContent());
}
}
在这一步中,我有一个错误
$this->assertNotRegExp('/Foo/', $this->client->getResponse()->getContent());
删除测试服务函数assertNotRegExp后,尝试在内容中查找,但有一些常规错误,我不知道。测试后,我的页面/admin/services/和错误中包含了所有html
' does not match PCRE pattern "/Foo/".
/home/ivan/host/test/src/Artel/AdminBundle/Tests/Controller/ServicesControllerTest.php:73
错误在哪里 您必须对您的请求进行身份验证 将以下代码添加到测试类:
private $client = null;
public function setUp()
{
$this->client = static::createClient();
}
private function logIn()
{
$session = $this->client->getContainer()->get('session');
$firewall = 'secured_area';
$token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_ADMIN'));
$session->set('_security_'.$firewall, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
并在创建客户端之前在测试方法中使用它:
public function testCompleteScenario()
{
$this->logIn();
// Do your logic
}
请参见提交完成-当我获得/admin/services具有“创建新条目”链接的列表服务时。编辑、创建和删除。点击提交后,你会得到什么状态码,页面被重新加载?在网络开发者工具的chrome中,我没有看到这个http,但是在postman中,如果get have Status 200 OK Time 1317 mswhen app/console router:debug have:services get ANY/admin/services/在这个操作中没有形成$this->client,这是什么?我尝试了$this->createClient()查看我答案中的更改(您也可以在文档中找到所有内容)删除url末尾的斜杠。我不明白为什么需要assertNotRegExp,因为这个测试返回失败,这没关系,因为在我的内容中没有strinf Foo。所以我使用assertRegExp这个断言找不到Foo并返回ok