PHPUnit(Phar)只有在测试失败时才会出现致命错误
PHPUnit (Phar) Fatal Error Only When Test Fails
我选择使用 Phar
of PHPUnit
(phpunit-4.8.26.phar) 来单元测试我自定义构建的 PHP 框架和应用程序。我使用旧的稳定版本的原因是因为我需要与 PHP 5.4.29
.
兼容
我选择不使用 Composer
来获取 PHPUnit 的原因是它往往会用依赖项污染我的 vendors
文件夹。我喜欢让它尽可能保持苗条。
我正在使用 Windows 7 Pro SP1
、WampDeveloper v5.4.0.1
、ProPhpStorm 2016.1.2
、Phing
和一大堆其他很酷的东西来做我的事。
问题
- I CAN 运行 从 PhpStorm 中成功通过单元测试。
- I CAN 运行 从 PhpStorm 中成功通过了一组单元测试。
- 我不能 运行 从 PhpStorm 中成功进行单元测试失败。
- I CAN 运行 从命令行成功通过单元测试。
- I CAN 运行一组命令成功通过单元测试
- 我不能 运行 失败单元测试从命令行成功。
而不是 PHPUnit
显示典型的测试失败消息,它退出时出现以下错误:
Warning: require(Composer\Autoload\ClassLoader.php): failed to open stream: No such file or directory in D:\WampDeveloper\Websites\qclean.development\bootstrap\Autoloader.php on line 23
Fatal error: require(): Failed opening required 'Composer\Autoload\ClassLoader.php' (include_path='.;D:\WampDeveloper\Tools\PEAR\pear;D:\WampDeveloper\Tools\PEAR;D:\WampDeveloper\Components\Php\PEAR;D:\WampDeveloper\Tools\PHPMailer;') in D:\WampDeveloper\Websites\qclean.development\bootstrap\Autoloader.php on line 23
以及在上面展开的屏幕截图:
支持信息
我的目录结构:
我的单元测试脚本ConfigurationTest.php
<?php
/**
* Created by PhpStorm
* User:
* Date: 04/06/16
* Time: 12:04 PM
*/
namespace nova\tests\configuration;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Class ConfigurationTest
*
* @package nova\tests\configuration
*/
class ConfigurationTest extends TestCase
{
protected function setUp()
{
parent::setUp();
}
public function test()
{
$result = false;
$this->assertTrue($result);
}
protected function tearDown()
{
parent::tearDown();
}
}
我的PHP单位XML配置文件TestAll.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../bootstrap/Start.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
syntaxCheck="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
verbose="false">
<testsuites>
<testsuite name="Nova Framework Test Suite">
<!-- <directory>.</directory> -->
<directory>./configuration</directory>
<exclude>./input</exclude>
<exclude>./request</exclude>
<exclude>./security</exclude>
<exclude>./validation</exclude>
</testsuite>
</testsuites>
</phpunit>
最后是我的 Autoloader Autoloader.php
<?php
// Ref: https://github.com/philsturgeon/fig-standards
/**
* Define the application autoloader function.
*/
function autoload($className)
{
$className = ltrim($className, '\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $namespace) . '.php';
require $fileName;
}
/**
* Register the autoloader functions.
*/
spl_autoload_register('autoload');
错误和警告表示无法加载Composer\Autoload\ClassLoader.php
文件。我不明白的是为什么当我使用 Phar
时它要求这个文件?从互联网上阅读片段表明 Phar 应该有一个内部自动加载器,但我看不到一个。
我不想安装 Composer
只是为了获得它的自动加载器。这将破坏尝试单独使用 Phar 的目的。
我将 PHPUnit Phar
路径添加到我的 Windows %path%
但这没有任何区别。我知道如果 PHPUnit 是使用 PEAR
.
安装的,那么应该这样做
任何对此 'hair pulling out matter' 的帮助将不胜感激...
所有你需要做的去 Settings > Language & Frameworks > PHP > PHPUnit
并选择 PHPUnit library
- 并将路径设置为 phpunit.phar
,就是这样。
如果您查看堆栈跟踪,您会发现默认情况下错误是在 class_exists
. This function calls __autoload
上触发的。这意味着将调用您注册的自动加载器。在这种情况下,将调用存在于项目外部的 class。
因此您应该在需要文件之前向您的自动加载器添加额外的 file_exists
检查。您需要一个不存在的文件。
if (file_exists($fileName)) {
require $fileName;
}
或者只是抑制错误(require
不会抛出异常,所以使用@
):
@require $fileName;
我选择使用 Phar
of PHPUnit
(phpunit-4.8.26.phar) 来单元测试我自定义构建的 PHP 框架和应用程序。我使用旧的稳定版本的原因是因为我需要与 PHP 5.4.29
.
我选择不使用 Composer
来获取 PHPUnit 的原因是它往往会用依赖项污染我的 vendors
文件夹。我喜欢让它尽可能保持苗条。
我正在使用 Windows 7 Pro SP1
、WampDeveloper v5.4.0.1
、ProPhpStorm 2016.1.2
、Phing
和一大堆其他很酷的东西来做我的事。
问题
- I CAN 运行 从 PhpStorm 中成功通过单元测试。
- I CAN 运行 从 PhpStorm 中成功通过了一组单元测试。
- 我不能 运行 从 PhpStorm 中成功进行单元测试失败。
- I CAN 运行 从命令行成功通过单元测试。
- I CAN 运行一组命令成功通过单元测试
- 我不能 运行 失败单元测试从命令行成功。
而不是 PHPUnit
显示典型的测试失败消息,它退出时出现以下错误:
Warning: require(Composer\Autoload\ClassLoader.php): failed to open stream: No such file or directory in D:\WampDeveloper\Websites\qclean.development\bootstrap\Autoloader.php on line 23
Fatal error: require(): Failed opening required 'Composer\Autoload\ClassLoader.php' (include_path='.;D:\WampDeveloper\Tools\PEAR\pear;D:\WampDeveloper\Tools\PEAR;D:\WampDeveloper\Components\Php\PEAR;D:\WampDeveloper\Tools\PHPMailer;') in D:\WampDeveloper\Websites\qclean.development\bootstrap\Autoloader.php on line 23
以及在上面展开的屏幕截图:
支持信息
我的目录结构:
我的单元测试脚本ConfigurationTest.php
<?php
/**
* Created by PhpStorm
* User:
* Date: 04/06/16
* Time: 12:04 PM
*/
namespace nova\tests\configuration;
use PHPUnit_Framework_TestCase as TestCase;
/**
* Class ConfigurationTest
*
* @package nova\tests\configuration
*/
class ConfigurationTest extends TestCase
{
protected function setUp()
{
parent::setUp();
}
public function test()
{
$result = false;
$this->assertTrue($result);
}
protected function tearDown()
{
parent::tearDown();
}
}
我的PHP单位XML配置文件TestAll.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../bootstrap/Start.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
syntaxCheck="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
verbose="false">
<testsuites>
<testsuite name="Nova Framework Test Suite">
<!-- <directory>.</directory> -->
<directory>./configuration</directory>
<exclude>./input</exclude>
<exclude>./request</exclude>
<exclude>./security</exclude>
<exclude>./validation</exclude>
</testsuite>
</testsuites>
</phpunit>
最后是我的 Autoloader Autoloader.php
<?php
// Ref: https://github.com/philsturgeon/fig-standards
/**
* Define the application autoloader function.
*/
function autoload($className)
{
$className = ltrim($className, '\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $namespace) . '.php';
require $fileName;
}
/**
* Register the autoloader functions.
*/
spl_autoload_register('autoload');
错误和警告表示无法加载Composer\Autoload\ClassLoader.php
文件。我不明白的是为什么当我使用 Phar
时它要求这个文件?从互联网上阅读片段表明 Phar 应该有一个内部自动加载器,但我看不到一个。
我不想安装 Composer
只是为了获得它的自动加载器。这将破坏尝试单独使用 Phar 的目的。
我将 PHPUnit Phar
路径添加到我的 Windows %path%
但这没有任何区别。我知道如果 PHPUnit 是使用 PEAR
.
任何对此 'hair pulling out matter' 的帮助将不胜感激...
所有你需要做的去 Settings > Language & Frameworks > PHP > PHPUnit
并选择 PHPUnit library
- 并将路径设置为 phpunit.phar
,就是这样。
如果您查看堆栈跟踪,您会发现默认情况下错误是在 class_exists
. This function calls __autoload
上触发的。这意味着将调用您注册的自动加载器。在这种情况下,将调用存在于项目外部的 class。
因此您应该在需要文件之前向您的自动加载器添加额外的 file_exists
检查。您需要一个不存在的文件。
if (file_exists($fileName)) {
require $fileName;
}
或者只是抑制错误(require
不会抛出异常,所以使用@
):
@require $fileName;