#39;Class not found#39; when using namespaces in PHPUnit(在 PHPUnit 中使用命名空间时出现“找不到类)
问题描述
我是 PHPUnit 的新手,在设置它以访问我的 PHP 文件时遇到了一些麻烦.我用于我的应用程序的目录结构是这样的:
I'm new to PHPUnit and am having some trouble setting it up to access my PHP files. The directory structure I'm using for my app is this:
./phpunit.xml
./lib/Application/
  -> Dir1/File1.php (namespace = ApplicationDir1)
  -> Dir1/File2.php
  -> Dir2/File1.php (namespace = ApplicationDir2)
./tests/Application/Tests
  -> Test1.php (namespace = ApplicationTests)
  -> Test2.php 
在我的 PhpUnit.xml 中,我有:
In my PhpUnit.xml, I have:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="false">
  <testsuites>
      <testsuite name="Application">
          <directory>./tests/Application/Tests</directory>
      </testsuite>
  </testsuites>
  <logging>
       <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
       <log type="json" target="/tmp/phpunit-logfile.json"/>
  </logging>
  <filter>
        <whitelist>
            <directory suffix=".php">./lib</directory>
        </whitelist>
  </filter>
</phpunit>
在我的一个测试文件中,我打开:
And in one of my test files, I open with:
namespace ApplicationTests;
use ApplicationDir1File1;
class MyTest extends File1 {}
但它一直在说:
找不到类ApplicationDir1File1"
Class 'ApplicationDir1File1' not found
我哪里错了?
推荐答案
即使你使用 use,你仍然需要包含文件,要么使用 include,require、include_once 或 require_once,或使用 spl_autoload_register 包含文件,如下所示:
Even if you use use, you still have to include the file, either by using include, require, include_once, or require_once, or by using spl_autoload_register to include the file, like so:
spl_autoload_register(function ($class)
{
    include 'lib\' . $class . 'php';
});
当您尝试使用 ApplicationDir1File1 时,脚本将自动运行 include 'libApplicationDir1File1.php'
When you then try to use ApplicationDir1File1 the script will automatically run include 'libApplicationDir1File1.php'
这篇关于在 PHPUnit 中使用命名空间时出现“找不到类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHPUnit 中使用命名空间时出现“找不到类"
				
        
 
            
        - SoapClient 设置自定义 HTTP Header 2021-01-01
 - 如何定位 php.ini 文件 (xampp) 2022-01-01
 - 从 PHP 中的输入表单获取日期 2022-01-01
 - 带有通配符的 Laravel 验证器 2021-01-01
 - Mod使用GET变量将子域重写为PHP 2021-01-01
 - 没有作曲家的 PSR4 自动加载 2022-01-01
 - PHP Count 布尔数组中真值的数量 2021-01-01
 - Laravel 仓库 2022-01-01
 - Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
 - 正确分离 PHP 中的逻辑/样式 2021-01-01
 
				
				
				
				