Symfony测试实体

时间:2016-02-07 14:05:35

标签: php symfony phpunit

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

哪里出错?

1 个答案:

答案 0 :(得分:2)

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

将以下代码添加到测试类中:

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

相关问题