سؤال

I have a functional test on Symfony2. It creates some test entities, makes tests and removes temporary entities right after tests completed.

[CREATE TEMP ENTITIES] => [RUN TESTS] => [DELETE TEMP ENTITIES]

The problem is when an assertion fails, temp entities aren't get deleted.

Please see below code (I commented the line which isn't executed):

<?php 
public function testChangePassword()
    {
        $enduser = $this->logIn();
        $enduserSalt = $enduser->getSalt();
        $successMsg = $this->translator->trans('change_pwd.success');
        $submitButtonText = $this->translator->trans('change_pwd.submit');

        /**
         * Test Case 1: Try to change password with incorrect old password
         */
        $url = $this->router->generate('userpanel_change_password');
        $crawler = $this->client->request('GET', $url);

        $form = $crawler->selectButton($submitButtonText)->form(
            [
                'password_change[old_password]' => 'xaxaxaxa',
                'password_change[password][password]' => '123456789',
                'password_change[password][confirm]' => '123456789'
            ]
        );

        $this->client->followRedirects();
        $crawler = $this->client->submit($form);

        $this->assertTrue($crawler->filter('html:contains("' . $successMsg . '")')->count() == 2);


        /**
         * Following line isn't executed when above assertion fails.
         */
        $this->removeTestUser();

        /**
         * Test Case 2: Change password success
         */

        $form = $crawler->selectButton($submitButtonText)->form(
            [
                'password_change[old_password]' => $this->testUserInfo['password'],
                'password_change[password][password]' => '123456789',
                'password_change[password][confirm]' => '123456789'
            ]
        );

        $this->client->followRedirects();
        $crawler = $this->client->submit($form);

        $this->assertTrue($crawler->filter('html:contains("' . $successMsg . '")')->count() > 0);
        $this->removeTestUser();
    }

    private function logIn()
    {
        $this->removeTestUser();
        $enduser = $this->createTestUser();

        $session = $this->client->getContainer()->get('session');

        $firewall = 'main';
        $token = new UsernamePasswordToken($enduser, null, $firewall, array('ROLE_USER'));
        $session->set('_security_' . $firewall, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);

        return $enduser;
    }

    private function createTestUser($salt = null)
    {
        $this->removeTestUser();

        $newUser = new Enduser();
        $newUser->setName($this->testUserInfo['name']);
        $newUser->setEmail($this->testUserInfo['email']);

        $factory = $this->client->getContainer()->get('security.encoder_factory');
        $encoder = $factory->getEncoder($newUser);
        $password = $encoder->encodePassword($this->testUserInfo['password'], $newUser->getSalt());
        $newUser->setPassword($password);

        if (!is_null($salt)) {
            $newUser->setSalt($salt);
        }

        $this->em->persist($newUser);
        $this->em->flush();

        return $newUser;
    }

    private function removeTestUser()
    {
        $this->em->getRepository('LabsCoreBundle:Enduser')->createQueryBuilder('E')
            ->delete()
            ->where('E.email = :email')
            ->setParameter('email', $this->testUserInfo['email'])
            ->getQuery()
            ->execute();
    }

First of all is there such a method which "always" runs after tests, like a postExecute in symfony1 (like a shutdown method maybe)?

Since my every test ends with deleting the temporary entities, so I want to implement a mechanism on the class to delete these entities.

I tried to wrap assertion checks with try/catch blocks however it didn't make sense.

هل كانت مفيدة؟

المحلول

You can use the tearDown() method in your test class, which will execute after every test, or use tearDownAfterClass() if you want to run the method just once at the end of all tests

Examples at http://phpunit.de/manual/3.7/en/fixtures.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top