使用 PHPUnit 的测试数据库/数据进行功能测试 [英] Functionality testing with Test database/data for PHPUnit
问题描述
我对 PHPUnit 测试有点陌生,所以感谢您的帮助,看看我是否遵循了正确的方法.
I am kind of new to PHPUnit test so appreciate your help to see whether I'm following the right approach or not.
我正在尝试测试我的方法;我有两个问题
I'm trying to test my methods; I have two questions
1) 如何让我的测试数据库包含虚拟数据?现在我在 pdo_connect 类和该类的构造函数中有我所有的数据库设置,我正在做所有的数据库初始化,包括 dbname、host、...;我已经包括了我想在这里测试的方法和我的测试用例;我的问题是根据不同的环境更改我的数据库进行测试是否是一个好习惯;例如,如果我的设置中的环境"变量是测试",我将使用测试"数据库,其中包含虚拟数据和...
1) How can I have my test database with dummy data? right now I have all of my database settings in pdo_connect class and in constructor of that class I'm doing all of the database initializations, including dbname, host,...; I have included my method that I want to test here and my test case as well; my question is whether it is a good practice to change my database for test based on different environments; for example if the 'environment' variable in my settings is 'test' I use the 'test' database which has dummy data and ...
2) 如果您确认我下面提到的示例测试用例是对我的方法进行功能测试的正确方法,我也非常感谢!
2) Also I really appreciate it if you confirm my example test case which is mentioned below is the right way of functionality testing of my method!
static public function get_images($id) {
try {
$conn = new pdo_connect();
$query = " SELECT ....";
$result = $conn->prepare($query);
$result->bindParam(':id', $id);
$result->execute();
$array_result = $result->fetchAll(PDO::FETCH_OBJ);
$result_Set = array($paginate_result, TRUE);
}
catch (Exception $e) {
$result_Set = array($e->getMessage(), FALSE);
}
return $result_Set;
}
和我的测试:
class SomeTest extends PHPUnit_Framework_TestCase {
public function __construct() {
require_once('../includes/model.php');
}
public function test_id_not_exist() {
$con = $this->getMock('conn');
$dao = new Model($con);
$result = MODEL::get_images(555);
$expected = array(array(), True);
self::assertEquals($expected, $result);
}
}
如果您需要更多说明,请告诉我...我再次提前感谢...
Please let me know if you need more clarification... and again I appreciate it in advance...
推荐答案
作为一般规则:测试静态函数可能会很棘手,但在您的情况下它可能会奏效.
As a general rule: Testing static function can get tricky, but in your case it might work.
如果这是您的实际代码,则无法切换到测试数据库,因为您在 get_images 中使用 $conn = new pdo_connect() 进行连接.
If this is your actual code, you can't switch to a test database, because you connect in get_images with $conn = new pdo_connect().
您需要的是某种依赖注入的方式来插入连接.你不需要模拟它,你可以使用到测试数据库的真实连接.
What you need is some way of dependency injection to insert the connection. You don't need to mock it, you can just use a real connection to the test database.
你可以这样做:
class Model {
private static $_conn;
public static function setDb($conn) {
self::$_conn = $conn;
}
static public function get_images($id) {
try {
$conn = self::$_conn;
...
} catch (Exception $e) {
$result_Set = array($e->getMessage(), false);
}
return $result_Set;
}
}
您的测试将变成这样(我还删除了一些其他问题):
Your test would become this (I also removed some other issues):
public function test_id_not_exist() {
$con = new pdo_connect();
Model::setDb($con);
$result = Model::get_images(555);
$expected = array(array(), True);
$this->assertEquals($expected, $result);
}
要正确地为您的测试数据库设置种子,您需要在您的测试类中实现一个 setUp 函数,然后可以插入一些虚拟数据.
To seed your test database properly you would implement a setUp function in your test class, which then can insert some dummy data.
这篇关于使用 PHPUnit 的测试数据库/数据进行功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!