2

I have entity service and I create controller and action get, create, edit and deleted. I look for test and I don't know why I have error? I can enter for this rout and have data, and work fine, but if create client and get status code have 302

But when I comment in security.yml

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

test passed almost all only in the late fall

How to create client with ROLE_ADMIN ?? and this test

 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());
}

and I have

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

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

where error ?

UPDATE

change

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());
}

}

in this step I have error

$this->assertNotRegExp('/Foo/', $this->client->getResponse()->getContent());

after deleted test service function assertNotRegExp try to find in content but error something with regular I dint know. After test I have all html my page /admin/services/ and error

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

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

where error ?

9
  • submit done - when I get /admin/services have list service with link'Create a new entry' . And edit and create and deleted. Commented Feb 7, 2016 at 14:23
  • And which status code you get after hit submit and the page is reloaded ? Commented Feb 7, 2016 at 14:24
  • In chrome in developer tools in network I not see this http, but in postman if get have Status 200 OK Time 1317 ms Commented Feb 7, 2016 at 14:30
  • when app/console router:debug have: services GET ANY ANY /admin/services/ Commented Feb 7, 2016 at 14:33
  • In this action have not form Commented Feb 7, 2016 at 14:34

1 Answer 1

2

You have to make your request authenticated.

Add the following code to your test 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);
}

And use it in your test method before create the client:

public function testCompleteScenario()
{
    $this->logIn();
    // Do your logic
}

See Simulate authentication in test

Sign up to request clarification or add additional context in comments.

9 Comments

$this->client , what is it ?
I try $this->createClient()
See changes in my answer (also you can find all in the documentation)
Remove the trailing slash at end of your url.
I dont understand why need assertNotRegExp, besause this test return failer and this is ok because in my content not strinf Foo. So I use assertRegExp this assert not find Foo and return ok
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.