I have a ini file 开发者_Python百科with db connection settings. I am creating an installer and need help to:
- test db connection settings in ini file with errors (wrong user/pass or cannot select db)
- insert db structure using .sql file.
Is this possible with ZF, I can't find any info anywhere. Should I just do this using direct queries?
Thanks!
Add $db->getConnection()
to make connections and catch the error if any comes around.
try {
$db->getConnection();
} catch( Exception $e ) {
print_r($e->getMessage());
die;
}
In your unit test, you could probably call $adapter->getConnect()
within a try/catch
that checks for a Zend_Db_Adapter_Exception
, subclasses of which are thrown on a failed connection. In the catch
, you can call $this->fail()
to signal the failed test.
But typically, you would have a different set of db connection params when APPLICATION_ENV = 'testing'
than you would for the other APPLICATION_ENV
's. So in this sense, I am with @jakenoble. It sounds less like a unit testing issue than a deployment issue. As such, some kind of command-line script - even one that uses the same bootstrap as the main app - might be more appropriate.
This isn't ZF specific so isn't something you should be accomplishing with ZF. ZF doesn't have a Database Adapter called MightExist
. If the username or password are wrong then ZF will error and you get no where.
Firstly is this a deployment issue or a testing issue?
If it is a deployment issue then you need something to deploy your code automatically, maybe a bash script? This could check the users in the database without relying on ZF at all. The bash script could also set up your database by importing your .sql file.
If it is a testing issue then it is different all together. You should use something like PHPUnit to test for the existence of users, their passwords and to check that data exists, if not insert it using your .sql file.
精彩评论