2

When I test an entity it creates it in the database but I can't manage to delete it. I think I have the default code to delete the entity but it does not work, is there another way? am I missing something?

Here is the code. I'm using symfony 2.7.8 and Php unit 4.8.0

 public function testCreateCurso()
    {
        // Create a new client to browse the application
        $client = static::createAuthorizedClient();

    // Create a new entry in the database
    $crawler = $client->request('GET', '/admin/curso/');
    $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Unexpected HTTP status code for GET /curso/');

    $crawler = $client->click($crawler->selectLink('Crear Nuevo Curso')->link());

    // Fill in the form and submit it
    $form = $crawler->selectButton('Create')->form(array(
        'appbundle_curso[nombreCurso]' => 'Test',
        'appbundle_curso[codigoCurso]' => 'Test4',
        // ... other fields to fill
    ));

    $client->submit($form);


    $this->assertTrue($client->getResponse()->isRedirect());
    $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('Editar')->link());

    $form = $crawler->selectButton('Update')->form(array(
        'appbundle_curso[nombreCurso]' => 'Foo',
        'appbundle_curso[codigoCurso]' => 'Foo1',
        // ... 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->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
    var_dump($client->getResponse()->getContent());

}
fcpauldiaz
  • 423
  • 6
  • 19

2 Answers2

1

This code is actually showing us how you test the UI, but the real code that is actually deleting the entity...

So, you should first of all check that both scenarios (adding an entity and deleting it) are actually working properly with unit tests (maybe when deleting the entity you're not flushing changes, for example...).

Then, when you have demonstrated yourself that you can actually add and delete the entity, and the controllers are working, then you should test your user interface, and that's what you're showing us.

So, if you've already done this, the issue is on your UI (for example, your button can't be followed).

Maybe a little bit more of information?

mmoreram
  • 687
  • 5
  • 13
0

I was missing this method to delete the entity from the database

   /**
     * Close doctrine connections to avoid having a 'too many connections'
     * message when running many tests
     */
    public function tearDown(){

        parent::tearDown();
    }
fcpauldiaz
  • 423
  • 6
  • 19