mercredi 14 septembre 2016

Testing best practice for Symfony Doctrine connection reset

I'm just looking for advice on how the best way to approach the following method, for testing. Should I mock the EntityManager, or pass a real EntityManager in?

I'm sorry if this is basic testing; but I'm fairly new to testing, and really want to do it right.

/**
 * @param EntityManager $em
 * @param $dbName
 * @param $dbUser
 * @param $dbPassword
 *
 * @throws \Exception
 */
public function resetConnection($em, $dbName, $dbUser, $dbPassword)
{
    try {
        $conn = array(
            'driver'   => 'pdo_mysql',
            'user'     => $dbUser,
            'password' => $dbPassword,
            'dbname'   => $dbName
        );

        $new = \Doctrine\ORM\EntityManager::create(
            $conn,
            $em->getConfiguration(),
            $em->getEventManager()
        );

        return $new;
    }
    catch (\Exception $e) {
        throw $e;
    }

    return false;
}

Currently, I built a mock version of an EntityManager object and also created a mock of Configuration and EventManager objects.

I have a test that checks if the exception triggers; but I want to build some better tests to actually check if the exception triggers properly when the new database doesn't exist; the mock doesn't seem to be stable enough for testing this...

Ideas?

Aucun commentaire:

Enregistrer un commentaire