PHPUnit - 测试文件包含方法(include、require、require_once)

发布于 2024-08-22 13:03:53 字数 664 浏览 7 评论 0原文

我正在编写和测试一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-08-29 13:03:53

我不确定您是否应该测试语言功能而不是您自己的代码。也就是说,您可以通过 class_existsfunction_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 or function_exists, assuming the included files define known classes or functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文