简体   繁体   中英

How can I use PHPUnit's DBUnit with a Microsoft SQL Server database?

I'm using PHPUnit and DBUnit to create tests for a PHP application that runs on the commandline and inserts data into a Microsoft SQL Server database.

The DBUnit documentation ( https://phpunit.de/manual/current/en/database.html#database.supported-vendors-for-database-testing ) states that Microsoft SQL Server is not supported but is accessible through the Zend Framework or Doctrine 2.

When I attempt:

 $pdo = new PDO( "odbc:Driver={ODBC Driver 13 for SQL Server};Server=redacted", $user, $passwd );

 $this->createDefaultDBConnection($pdo, $db);

I get, not surprisingly, the following error:

PHPUnit_Extensions_Database_Exception: Could not find a meta data driver for odbc pdo driver.

I found Zend Framework documentation that shows how to use PHPUnit to write tests related to relational databases ( https://framework.zend.com/manual/1.10/en/zend.test.phpunit.db.html ), and I think I could get that to work if I understood Zend better, so it's possible what I need is some help understanding how to get access to the Zend classes w/out writing an entire Zend application.

But maybe there's some non-Zend way to do it.

If your problem is the PDO connection, I would recommend trying to use the pdo_sqlsrv driver that's bundled with the SQL Server Driver for PHP . The connection string you have to use would look something like this:

new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM