简体   繁体   English

PhpStorm 中的 Phpunit 错误

[英]Phpunit error in PhpStorm

I am running tests in PhpStorm and I get this error.我在 PhpStorm 中运行测试,但出现此错误。 Does anyone know what on earth could be causing it?有谁知道到底是什么原因造成的?

PHP Fatal error: Class 'PHPUnit_TextUI_ResultPrinter' not found in C:\Users\administrator1\AppData\Local\Temp\ide-phpunit.php on line 249 PHP 致命错误:Class 在 C 中未找到“PHPUnit_TextUI_ResultPrinter”:\Users\administrator1\AppData\Local\Temp\ide-phpunit.php 第 249 行

I run tests in other projects - that also utilise ide-phpunit.php and they run just fine.我在其他项目中运行测试 - 也使用ide-phpunit.php并且它们运行得很好。

I had the same issue with Ubuntu 16.10, phpStorm 2017.2 and Laravel 5.5我在 Ubuntu 16.10、phpStorm 2017.2 和 Laravel 5.5 上遇到了同样的问题

Fixed it by uninstalling phpunit from my Ubuntu-system with通过从我的 Ubuntu 系统中卸载 phpunit 来修复它

sudo apt-get remove phpunit
sudo apt-get install --autoremove

or for mac:或对于 mac:

brew uninstall phpunit
brew install phpunit

My PhpStorm configuration (File -> Settings -> Languages & Frameworks -> PHP -> Test Frameworks):我的 PhpStorm 配置(文件 -> 设置 -> 语言和框架 -> PHP -> 测试框架):

phpStorm-配置

Works great, now!效果很好,现在!

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project.我犯了一个愚蠢的错误...只是忘记在项目中添加 phpunit 作为依赖项。 For anyone else that gets this error, to composer.json add:对于遇到此错误的任何其他人,请在 composer.json 中添加:

"require-dev": {
    "phpunit/phpunit": "3.7.*"
},

And then run:然后运行:

composer update

That solved the problem.那解决了问题。

In my case the problem was caused by following set of reasons:在我的情况下,问题是由以下原因引起的:

  1. I installed phpunit using composer with composer require phpunit/phpunit command.我使用 Composer 和composer require phpunit/phpunit命令安装了 phpunit。 I did not pay attention that by default it used php7 and it installed phpunit6 which has class names with namespaces ( PHPUnit\\TextUI\\ResultPrinter ).我没有注意到默认情况下它使用 php7 并且它安装了 phpunit6,它的类名带有命名空间( PHPUnit\\TextUI\\ResultPrinter )。
  2. IDE runs old version of phpunit which expects class names without namespaces ( PHPUnit_TextUI_ResultPrinter ) IDE 运行旧版本的 phpunit,它需要没有命名空间的类名 ( PHPUnit_TextUI_ResultPrinter )

I decided to reinstall phpunit running same composer command as above, but under php 5.6 (because it was important to be compatible with php5.6) and it installed phpunit 5.7 .我决定重新安装phpunit运行与上述相同的 composer 命令,但在php 5.6 (因为与 php5.6 兼容很重要)并且它安装了phpunit 5.7 But it is possible to go with newer version of phpunit and php: Settings > PHP > PHPUnit :: "Use composer autoloader" (set path to phpunit executable inside vendors (it was vendors/bin/phpunit in my case))但是可以使用较新版本的 phpunit 和 php: Settings > PHP > PHPUnit :: "Use composer autoloader" (在供应商内部设置 phpunit 可执行文件的路径(在我的情况下是vendors/bin/phpunit ))

I got this error while using the various modifier -F10 keys in PHPStorm, because it didn't know where to find the correct PHPUnit.我在 PHPStorm 中使用各种修饰符-F10 键时遇到此错误,因为它不知道在哪里可以找到正确的 PHPUnit。

In settings (ctrl-alt-s), search for PHPUnit.在设置 (ctrl-alt-s) 中,搜索 PHPUnit。 In my case I wanted to set it as follows:就我而言,我想将其设置如下:

  • "use composer autoloader" “使用作曲家自动加载器”
  • Path to script: full-path-to-project/vendor/autoload.php (this field was empty for me, with an error showing in the dialog)脚本路径: full-path-to-project/vendor/autoload.php (这个字段对我来说是空的,对话框中显示错误)

Otherwise it would try to use some cached version of the library ( /tmp/ide-phpunit.php ) instead of the composer version.否则它会尝试使用库的一些缓存版本( /tmp/ide-phpunit.php )而不是 composer 版本。 If you prefer to use some globally installed binary, you could of course use the Path to phpunit.phar option.如果您更喜欢使用某些全局安装的二进制文件,您当然可以使用Path to phpunit.phar选项的Path to phpunit.phar

I had same problem with PHP7.1 and PHPUnit 6.3 via composer but I resolved via phpunit.phar.我通过 Composer 在 PHP7.1 和 PHPUnit 6.3 上遇到了同样的问题,但我通过 phpunit.phar 解决了。 (in the options : select path to phpunit.phar, click on download and click on refresh) (在选项中:选择phpunit.phar的路径,点击下载并点击刷新)

在此处输入图片说明

After updating to latest composer, composer run-script drupal-phpunit-upgrade , PHPStorm was still mapped to the old version even though I was loading through autoloader.更新到最新的作曲家composer run-script drupal-phpunit-upgrade ,即使我通过自动加载器加载,PHPStorm 仍然映射到旧版本。 I went to Languages & Frameworks > Php > Test Frameworks and hit the refresh button on the same line as "Path to Script".我转到 Languages & Frameworks > Php > Test Frameworks 并点击“Path to Script”同一行上的刷新按钮。

Now my PHPUnit version reads 6.5.8.现在我的 PHPUnit 版本显示为 6.5.8。

In my case, in phpstorm preferences -> php -> CLI interpreter was set on local, when it needed to use remote php 7 (ubuntu).就我而言,在 phpstorm 首选项 -> php -> CLI 解释器中设置在本地,当它需要使用远程 php 7 (ubuntu) 时。

Because I use vagrant box to have my environment configured on ubuntu.因为我使用 vagrant box 在 ubuntu 上配置我的环境。

I recently came across similar error PHPUnit_TextUI_ResultPrinter not found in TeamCity.php on line 19我最近遇到了类似的错误PHPUnit_TextUI_ResultPrinter not found in TeamCity.php on line 19

I am using PhpStorm 2018.3.3 .我正在使用PhpStorm 2018.3.3 Upon using terminal from PhpStorm $ phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors.在使用$ phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors. PhpStorm $ phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors.终端时$ phpunit PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

from my Ubuntu system $ phpunit --version PHPUnit 5.1.3 by Sebastian Bergmann and contributors.来自我的 Ubuntu 系统$ phpunit --version PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

I had to make sure the my Ubuntu system PHPUnit is updated to 7.5.1.我必须确保我的 Ubuntu 系统 PHPUnit 更新到 7.5.1。 So here's what I did:所以这就是我所做的:

sudo wget https://phar.phpunit.de/phpunit-7.5.1.phar
sudo chmod +x phpunit-7.5.1.phar
sudo mv phpunit-7.5.1.phar /usr/local/bin/phpunit

Then I went to PhpStorm: File > Settings > Language & Frameworks > PHP > Test Frameworks and made sure that the PHPUnit version is also 7.5.1然后我去 PhpStorm:文件 > 设置 > 语言和框架 > PHP > 测试框架并确保 PHPUnit 版本也是 7.5.1

And then from PhpStorm terminal: phpunit and the test cases were running perfectly fine.然后从 PhpStorm 终端: phpunit和测试用例运行得非常好。

I went to the settings and set my path to php as follows: (according to the brew)我进入设置并将我的路径设置为php如下:(根据brew)

/usr/local/opt/php/bin/php

Then phpunit works.然后 phpunit 工作。

I opened the php extension mbstring to solved this problem我打开了 php 扩展 mbstring 解决了这个问题

I got the same error.我得到了同样的错误。 Solved by using phpunit4.8 (older version)使用phpunit4.8(旧版本)解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM