Symfony 测试实体

Symfony test entity

我有实体服务,我创建控制器和操作获取、创建、编辑和删除。我寻找测试,我不知道为什么我有错误?我可以进入这个溃败并获得数据,并且工作正常,但是如果创建客户端并获取状态代码有 302

但是当我在 security.yml 中发表评论时

    access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    #- { path: ^/admin, roles: ROLE_ADMIN }

测试几乎全部在深秋才通过

如何使用ROLE_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 后尝试在内容中查找,但我不知道的常规错误。测试后我有所有 html 我的页面 /admin/services/ 和错误

' does not match PCRE pattern "/Foo/".

    /home/ivan/host/test/src/Artel/AdminBundle/Tests/Controller/ServicesControllerTest.php:73

哪里出错了?

您必须对您的请求进行身份验证。

将以下代码添加到您的测试中 class :

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
}

Simulate authentication in test