Mocking/Stubbing an Object of a class that implements arrayaccess in PHPUnit(模拟/存根在 PHPUnit 中实现数组访问的类的对象)
问题描述
这是我正在为其编写测试套件的类的构造函数(它扩展了 mysqli):
Here is the constructor of the class I am writing a test suite for (it extends mysqli):
function __construct(Config $c)
{
// store config file
$this->config = $c;
// do mysqli constructor
parent::__construct(
$this->config['db_host'],
$this->config['db_user'],
$this->config['db_pass'],
$this->config['db_dbname']
);
}
传递给构造函数的Config
类实现了php内置的arrayaccess
接口:
The Config
class passed to the constructor implements the arrayaccess
interface built in to php:
class Config implements arrayaccess{...}
如何模拟/存根 Config
对象?我应该使用哪个?为什么?
How do I mock/stub the Config
object? Which should I use and why?
提前致谢!
推荐答案
如果您可以轻松地从数组创建 Config
实例,那将是我的首选.虽然您希望在可行的情况下单独测试您的单元,但简单的协作者(例如 Config
)应该足够安全,可以在测试中使用.设置它的代码可能比等效的模拟对象更容易读写(不易出错).
If you can easily create a Config
instance from an array, that would be my preference. While you want to test your units in isolation where practical, simple collaborators such as Config
should be safe enough to use in the test. The code to set it up will probably be easier to read and write (less error-prone) than the equivalent mock object.
$configValues = array(
'db_host' => '...',
'db_user' => '...',
'db_pass' => '...',
'db_dbname' => '...',
);
$config = new Config($configValues);
话虽如此,你模拟了一个实现 ArrayAccess
就像对待任何其他对象一样.
That being said, you mock an object implementing ArrayAccess
just as you would any other object.
$config = $this->getMock('Config', array('offsetGet'));
$config->expects($this->any())
->method('offsetGet')
->will($this->returnCallback(
function ($key) use ($configValues) {
return $configValues[$key];
}
);
您也可以使用 at
来强制执行特定的访问顺序,但这样会使测试变得非常脆弱.
You can also use at
to impose a specific order of access, but you'll make the test very brittle that way.
这篇关于模拟/存根在 PHPUnit 中实现数组访问的类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模拟/存根在 PHPUnit 中实现数组访问的类的对象


基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01