在安装了 Twig 的 PHPUnit 中进行单元测试
Unit testing in PHPUnit with Twig installed
当我 运行 PHPUnit 时,它会尝试 运行 在意想不到的位置测试文件,即 Twig 安装目录,并多次尝试加载 Twig class 文件。有谁知道如何解决这些问题?
背景
我在 Web 应用程序中使用 Twig(只是 Twig,不是 Symfony),通过 Composer 安装了 Twig。 Composer 和 Twig 安装在自定义位置,我的测试在 /test/
,所以我有以下简化的项目结构:
/src/includes/vendor/
autoload.php
composer/
twig/
twig/
lib/
test/
/test/
bootstrap.php
(various files named, e.g., someClassTest.php)
我的 bootstrap 文件 (/test/bootstrap.php
) 除了加载 Composer 自动加载器外什么都不做:
<?php
require_once dirname(__FILE__) . "/../src/includes/vendor/autoload.php";
我的 PHPUnit 配置文件 (phpunit.xml) 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="All Tests">
<directory>./test/phpunit/</directory>
</testsuite>
</testsuites>
<php>
<ini name="xdebug.default_enable" value="0" />
<ini name="xdebug.remote_autostart" value="0" />
<ini name="xdebug.remote_enable" value="0" />
<ini name="xdebug.overload_var_dump" value="0" />
<ini name="xdebug.show_mem_delta" value="0" />
</php>
</phpunit>
问题
1. PHPUnit 运行在 /src/includes/vendor/composer/twig/twig/test/
.
中进行 Twig 单元测试
为什么?根据我的配置文件,我希望只在 /test
到 运行 之间进行测试。现在,我已经创建了一个单独的配置文件,该文件明确排除了带有以下条目的 Twig 测试:
<exclude>./src/includes/vendor/</exclude>
这感觉很老套而且很脆弱,我不明白为什么有必要这样做。谁能解释一下?
2。如果我尝试 运行 PHPUnit 而不明确排除 Twig 单元测试,我 运行 会遇到自动加载器问题。
如果我 运行 PHPUnit 没有包含我的 bootstrap.php 文件,那么 Composer 自动加载器不会被加载,所以 PHPUnit 在它第一次尝试实例化任何 Twig classes 时死掉.
如果我包含自动加载器,classes 会被加载两次,然后出现此错误:
Fatal error: Cannot redeclare class Twig_Node_Expression_Test in [path to project]\src\includes\vendor\twig\twig\lib\Twig\Node\Expression\Test.php on line 32
Call Stack:
0.1630 223296 1. {main}() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:0
0.4280 857528 2. IDE_Base_PHPUnit_TextUI_Command::main() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:552
0.4280 864000 3. PHPUnit_TextUI_Command->run() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:294
1.8451 1338552 4. PHPUnit_Runner_BaseTestRunner->getTest() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/TextUI/Command.php:150
107.4751 1501304 5. PHPUnit_Framework_TestSuite->addTestFiles() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Runner/BaseTestRunner.php:96
107.6552 1668912 6. PHPUnit_Framework_TestSuite->addTestFile() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Framework/TestSuite.php:416
107.6572 1668888 7. PHPUnit_Util_Fileloader::checkAndLoad() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Framework/TestSuite.php:355
107.6592 1669056 8. PHPUnit_Util_Fileloader::load() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Util/Fileloader.php:76
实际上 运行我在应用程序中没有遇到任何类似的问题;这仅在单元测试时发生。
如果有帮助,我正在 运行ning PHPStorm 9 (PS-141.1717),使用 PHPUnit 3.7.37。
有没有人了解这个问题?
根据评论编辑:PHPStorm执行的命令行如下:
C:\xampp\php\php.exe C:/Users/Ed/AppData/Local/Temp/ide-phpunit.php --bootstrap [path to project]\test\phpunit\bootstrap.php --configuration [path to project]\phpunit.xml [path to project]
在您的 PHPUnit 命令末尾,尝试将 [path to project]
替换为 [path to project]/tests/phpunit
,或者完全省略该部分。
我通过大量修改 PHPStorm 找到了答案。在 "Run/Debug Configurations" 对话框中,我将 "Directory" 检查为 "Test scope." 我将其更改为 "Defined in the configuration file."
现在,PHPUnit 仅 运行 提供 /test/phpunit/
中的测试,如我的 phpunit.xml
中所指定。这意味着它没有 运行 Twig 测试(如预期的那样),消除了我的问题中指定的两个问题。如果我 运行 从 PHPStorm 中进行测试,并且如果我在包含 phpunit.xml
.
的目录中在终端上简单地执行命令 phpunit
,这将起作用
综上所述,如果有人知道如何同时 运行 我的测试和 Twig 测试,而不会出现 "Cannot redeclare class" 错误,请 post 条评论。
当我 运行 PHPUnit 时,它会尝试 运行 在意想不到的位置测试文件,即 Twig 安装目录,并多次尝试加载 Twig class 文件。有谁知道如何解决这些问题?
背景
我在 Web 应用程序中使用 Twig(只是 Twig,不是 Symfony),通过 Composer 安装了 Twig。 Composer 和 Twig 安装在自定义位置,我的测试在 /test/
,所以我有以下简化的项目结构:
/src/includes/vendor/
autoload.php
composer/
twig/
twig/
lib/
test/
/test/
bootstrap.php
(various files named, e.g., someClassTest.php)
我的 bootstrap 文件 (/test/bootstrap.php
) 除了加载 Composer 自动加载器外什么都不做:
<?php
require_once dirname(__FILE__) . "/../src/includes/vendor/autoload.php";
我的 PHPUnit 配置文件 (phpunit.xml) 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
<testsuites>
<testsuite name="All Tests">
<directory>./test/phpunit/</directory>
</testsuite>
</testsuites>
<php>
<ini name="xdebug.default_enable" value="0" />
<ini name="xdebug.remote_autostart" value="0" />
<ini name="xdebug.remote_enable" value="0" />
<ini name="xdebug.overload_var_dump" value="0" />
<ini name="xdebug.show_mem_delta" value="0" />
</php>
</phpunit>
问题
1. PHPUnit 运行在 /src/includes/vendor/composer/twig/twig/test/
.
为什么?根据我的配置文件,我希望只在 /test
到 运行 之间进行测试。现在,我已经创建了一个单独的配置文件,该文件明确排除了带有以下条目的 Twig 测试:
<exclude>./src/includes/vendor/</exclude>
这感觉很老套而且很脆弱,我不明白为什么有必要这样做。谁能解释一下?
2。如果我尝试 运行 PHPUnit 而不明确排除 Twig 单元测试,我 运行 会遇到自动加载器问题。
如果我 运行 PHPUnit 没有包含我的 bootstrap.php 文件,那么 Composer 自动加载器不会被加载,所以 PHPUnit 在它第一次尝试实例化任何 Twig classes 时死掉.
如果我包含自动加载器,classes 会被加载两次,然后出现此错误:
Fatal error: Cannot redeclare class Twig_Node_Expression_Test in [path to project]\src\includes\vendor\twig\twig\lib\Twig\Node\Expression\Test.php on line 32
Call Stack:
0.1630 223296 1. {main}() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:0
0.4280 857528 2. IDE_Base_PHPUnit_TextUI_Command::main() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:552
0.4280 864000 3. PHPUnit_TextUI_Command->run() C:\Users\Ed\AppData\Local\Temp\ide-phpunit.php:294
1.8451 1338552 4. PHPUnit_Runner_BaseTestRunner->getTest() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/TextUI/Command.php:150
107.4751 1501304 5. PHPUnit_Framework_TestSuite->addTestFiles() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Runner/BaseTestRunner.php:96
107.6552 1668912 6. PHPUnit_Framework_TestSuite->addTestFile() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Framework/TestSuite.php:416
107.6572 1668888 7. PHPUnit_Util_Fileloader::checkAndLoad() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Framework/TestSuite.php:355
107.6592 1669056 8. PHPUnit_Util_Fileloader::load() phar://C:/Users/Ed/Documents/Programming/phpunit-lts.phar/phpunit/Util/Fileloader.php:76
实际上 运行我在应用程序中没有遇到任何类似的问题;这仅在单元测试时发生。
如果有帮助,我正在 运行ning PHPStorm 9 (PS-141.1717),使用 PHPUnit 3.7.37。
有没有人了解这个问题?
根据评论编辑:PHPStorm执行的命令行如下:
C:\xampp\php\php.exe C:/Users/Ed/AppData/Local/Temp/ide-phpunit.php --bootstrap [path to project]\test\phpunit\bootstrap.php --configuration [path to project]\phpunit.xml [path to project]
在您的 PHPUnit 命令末尾,尝试将 [path to project]
替换为 [path to project]/tests/phpunit
,或者完全省略该部分。
我通过大量修改 PHPStorm 找到了答案。在 "Run/Debug Configurations" 对话框中,我将 "Directory" 检查为 "Test scope." 我将其更改为 "Defined in the configuration file."
现在,PHPUnit 仅 运行 提供 /test/phpunit/
中的测试,如我的 phpunit.xml
中所指定。这意味着它没有 运行 Twig 测试(如预期的那样),消除了我的问题中指定的两个问题。如果我 运行 从 PHPStorm 中进行测试,并且如果我在包含 phpunit.xml
.
phpunit
,这将起作用
综上所述,如果有人知道如何同时 运行 我的测试和 Twig 测试,而不会出现 "Cannot redeclare class" 错误,请 post 条评论。