使用 Composer 安装 PHPUNIT


Install PHPUNIT with Composer

我在Symfony 2上有项目,我想在Windows 7上使用PHPUNIT。

On githut phpunit is:
Composer
Simply add a dependency on phpunit/phpunit to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on PHPUnit 3.7:
{
    "require-dev": {
        "phpunit/phpunit": "3.7.*"
    }
}
For a system-wide installation via Composer, you can run:
composer global require 'phpunit/phpunit=3.7.*'
Make sure you have ~/.composer/vendor/bin/ in your path.

首先,我使用系统范围的安装,但我不知道何时安装。接下来,我添加到我的composer.jsonrequire-dev。这在 C:/wamp/www/myproject/vendor/symfony 中安装了 phpunit。接下来我尝试命令:

 composer install --dev

而且我不能使用phpunit。在cmd中.exe我输入"phpunit",但出现错误:

'phpunit' is not recognized as an internal or external command operable program or batch file

如何使用 phpunit?我有Windows 7,Wamp服务器和php 5.4.12。

当您通过 composer 在 windows 中安装 PHP-Unit 时,全局安装将在

C:'Users'YOUR_USERNAME'AppData'Roaming'Composer

要通过命令行轻松执行phpunit,您需要在Windows环境变量中添加phpunit.bat文件的路径。为此:

  1. 右键单击My Computer
  2. 转到Properties -> Advance system settings
  3. 单击Advance选项卡中的Environment variables

现在将C:'Users'YOUR_USERNAME'AppData'Roaming'Composer'vendor'bin添加到窗口PATH

您现在可以从命令运行 phpunit。请注意,您可能需要重新启动命令提示符才能使更改生效。

软件包的 bin 文件放在配置的 bin 目录中。默认情况下,这是vendor/bin,当您使用symfony标准版时,这是bin文件夹。

要执行此bin文件,请运行./bin/phpunit(或在不使用Symfony标准版时运行./vendor/bin/phpunit

Windows用户必须将其放在双引号中:"bin/phpunit"(或"vendor/bin/phpunit"

composer require --dev phpunit/phpunit ^9

上面的例子假设,作曲家已经在你的$PATH变量上。

你的作曲家.json应该看起来很相似;

{
  "name": "vendor_name/package_name",
  "description": "This project is for practicing writing php unit tests",
  "minimum-stability": "stable",
  "license": "proprietary",
  "authors": [
    {
      "name": "Umair Anwar",
      "email": "umair.anwar@gmail.com"
    }
  ],
  "autoload": {
    "classmap": [
      "src/"
    ]
  },
  "require-dev": {
    "phpunit/phpunit": "^9",
    "phpunit/dbunit": "^4.0"
  }
}

通过作曲家安装 phpunit 的最简单方法是从项目根目录运行。

$ composer require phpunit/phpunit

这将做的是,它将在供应商/bin 内创建一个 phpunit 文件夹你可以运行这样的单元测试。

$ ./vendor/bin/phpunit

在带有作曲家的 Windows 上的操作太简单了,并且按照以下方式对我有用:

Install composer https://getcomposer.org/doc/00-intro.md#installation-windows 转到 您的交响乐文件夹,例如 C:''wamp64''www''symfony''UserManagement 在哪里 composer.json 并运行此命令。 应该向全局注册以没有问题 $phpunit bash: phpunit: 找不到命令

//old version is 5.7 new 6.4 or put newest version.
composer global require --dev phpunit/phpunit ^5.7

我记得在 phpunit 中摸索作曲家依赖项的东西,但永远无法让它工作。

相反,从你的 git bash shell 中:

mkdir ~/bin
cd ~/bin
curl https://phar.phpunit.de/phpunit.phar > phpunit
chmod +x phpunit

退出 Bash,然后开始新的 Bash 会话。

你应该很高兴。 您可以回显$PATH以验证您是否有指向 ~/bin 的路径,但默认情况下似乎添加了一条路径。

https://phar.phpunit.de/phpunit.phar

我也

遇到了同样的问题,并按照以下步骤找出解决方案

WAMP 安装下在 Windows 7 中运行 PHPUnit

  1. 作曲家安装

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

  2. 只需设置环境变量php 单元将安装在供应商/箱的供应商目录中

    路径 : C:''wamp''www''myproject''vendor''bin;

  3. 打开新的命令提示符C:''Users''guny>phpunit --versionPHPUnit 3.7.30 塞巴斯坦·伯格曼