我有一个执行 mysql 操作的示例类,如下所示。
<?php
class ProjectHandler{
public function getProjectInformation($projectId){
$prMysql = new prMysql;
/* Make connection to database */
$connection = $prMysql->open_store_database();
$sql = sprintf("SELECT product_id, projectName FROM test_projects
WHERE projectId = %d", $platform_id);
$test_result = mysql_query($sql, $connection) or die();
$num_rows = mysql_num_rows($test_result);
if ($num_rows > 0) {
$test_row = mysql_fetch_assoc($test_result);
}
return $test_row;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
哪里prMysql有一个为mysql操作编写的包装类。是否可以模拟场景中的 mysql 调用?
您可以采取两种方法来实现此目的。鉴于您提供的代码示例,两者都有其缺点。
我假设这mysql_query()是一个错字,而你的意思是mysqli_query()。请参阅如何防止 PHP 中的 SQL 注入?了解有关为什么不应该使用这些mysql_*函数以及可以做什么的更多信息。
鉴于你的陈述
其中prMysql是为mysql操作编写的包装类
出于这部分答案的目的,我将假设诸如 之类的调用mysql_query($sql, $connection)实际上被写为$prMysql->query($sql),从而包装了实际的调用。我还将假设该num_results()部分也是用该方法完成的。如果您选择此重构解决方案,则该类prMysql将需要用于查询,并且仅用于连接。
依赖注入背后的想法是,您不会prMysql在使用它的类内部实例化依赖项(在您的情况下),而是在它的外部实例化。这在单元测试中非常有用,因为您可以为正在测试的类提供一个假的或“模拟”对象。可以构造此模拟对象,使其始终向已知输入返回已知值。
这种方法的缺点是您需要重构该类ProjectHandler以将其作为依赖项传递。像下面这样的东西应该足够了,但是你可能需要在有开放连接调用的地方进行尝试:
class ProjectHandler
{
/** @var prMysql */
private $database;
/**
* @param prMysql $database
*/
public function __construct(prMysql $database)
{
$this->database = $database;
}
/**
* @param mixed $projectId
* @return array
*/
public function getProjectInformation($projectId)
{
$prMysql = $this->database;
$sql = sprintf("SELECT product_id, projectName FROM test_projects
WHERE projectId = %d", $platform_id);
$test_row = $pyMysql->query($sql);
return $test_row;
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着您可以prMysql在测试时轻松给出模拟对象,而无需更改被测系统的任何代码。在您的测试方法中,您可能会得到如下所示的内容:
public function testGetProjectInformation()
{
// Here, we create a mock prMysql object so we don't use the original
$prMysql = $this->getMockBuilder(prMysql::class)
->disableOriginalConstructor()
->getMock();
/* Here, we say that we expect a call to mysql_query with a given query,
* and when we do, return a certain result.
* You will also need to mock other methods as required */
$expectedQuery = "SELECT product_id, projectName FROM test_projects
WHERE projectId = 1";
$returnValue = [['product_id' => 1, 'projectName' => 'test Name']];
$prMysql->expects($this->once())
->method('query')
->with($this->equalTo($expectedQuery))
->willReturn($returnValue);
// Here we call the method and do some checks on it
$object = new ProjectHandler($prMysql);
$result = $object->getProjectInformation(1);
$this->assertSame($returnValue, $result);
}
Run Code Online (Sandbox Code Playgroud)
现在,请记住,这只是您需要执行的操作的粗略草图。您需要自己填写详细信息。虽然需要进行大量的重构,但最终还是值得的。
另一种选择是设置一个数据库仅用于测试,并直接连接到该数据库。
phpunit 手册中有整整一章关于此内容,但对于每个测试,它归结为:
就您而言,这样做的优点是您只需更改很少的代码(如果有的话)。这样做的缺点是您的测试会非常慢,并且您已经失去了依赖注入等所提供的抽象。
| 归档时间: |
|
| 查看次数: |
3824 次 |
| 最近记录: |