PHPUnit - 测试文件包含方法(include、require、require_once)
我正在编写和测试一个 ClassLoader 组件,该组件可以多次实例化,并具有类名及其相关路径之间的各种映射。每个类加载器都应该作为特定包的加载器。
是否有一种简单、不引人注目的方法来测试或模拟包含由类加载器处理的文件?
让我用一个最简单的加载器来澄清:
class TestTwoPackageLoader implements IPackageLoader
{
private $directory;
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
$this->directory = dirname(__FILE__);
}
public function loadClass($class)
{
if (isset($this->classes[$class]))
include $this->directory.'/'.$this->classes[$class];
}
private $classes = array(
'SecClass' => 'test_two/SecClass.php',
'ThClass' => 'test_two/ThClass.php',
);
}
I'm writing and testing a ClassLoader component, which can be instantiated many times, with various mappings between class names and their relevant paths. Each ClassLoader should work as a loader for a specific package.
Is there an easy, unobtrusive way to test or mock inclusion of files handled by the ClassLoader?
Let me clarify with a simplest possible Loader:
class TestTwoPackageLoader implements IPackageLoader
{
private $directory;
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
$this->directory = dirname(__FILE__);
}
public function loadClass($class)
{
if (isset($this->classes[$class]))
include $this->directory.'/'.$this->classes[$class];
}
private $classes = array(
'SecClass' => 'test_two/SecClass.php',
'ThClass' => 'test_two/ThClass.php',
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否应该测试语言功能而不是您自己的代码。也就是说,您可以通过
class_exists
或function_exists
测试是否成功包含,假设包含的文件定义了已知的类或函数。I'm not certain if you should be testing language functionality, rather than your own code. That said, you might test for successful inclusion via
class_exists
orfunction_exists
, assuming the included files define known classes or functions.