首页
学习
活动
专区
工具
TVP
发布

Installing PHPUnit

要求

PHPUnit 6.4需要 PHP 7; 强烈推荐使用最新版本的 PHP 。

PHPUnit 需要 dom json 扩展,默认情况下通常是启用的。

PHPUnit也需要 pcre reflection spl 扩展。这些标准扩展默认启用,不修补 PHP 的构建系统和/或 C 源不能被禁用。

代码覆盖率报告功能需要 Xdebug(2.5.0或更高版本)和 tokenizer 扩展。生成 XML 报告需要 xmlwriter 扩展。

PHP 档案(PHAR)

获取 PHPUnit 的最简单方法是下载一个 PHP 归档文件(PHAR),该归档文件包含捆绑在一个文件中的所有 PHPUnit 所需的(以及一些可选的)依赖项。

所述 PHAR 扩展需要使用 PHP 归档文件(PHAR)。

如果启用 Suhosin 扩展,您需要允许在您的系统中执行 PHAR php.ini

suhosin.executor.include.whitelist = phar

要全局安装 PHAR :

$ wget https://phar.phpunit.de/phpunit-6.4.phar
$ chmod +x phpunit-6.4.phar
$ sudo mv phpunit-6.4.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

您也可以直接使用下载的 PHAR 文件:

$ wget https://phar.phpunit.de/phpunit-6.4.phar
$ php phpunit-6.4.phar --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

Windows

全局安装 PHAR 包括与在 Windows 上手动安装 Composer 相同的过程:

1. 为 PHP 二进制文件创建一个目录; 例如,C:\bin

2. 附加;C:\bin到您的PATH环境变量(相关帮助

3. 下载https://phar.phpunit.de/phpunit-6.4.phar并保存为C:\bin\phpunit.phar

4. 打开命令行(例如,按 Windows + R »键入cmd» ENTER

5. 创建一个包装批处理脚本(结果C:\bin\phpunit.cmd):

C:\Users\username> cd C:\bin C:\bin> echo @php "%~dp0phpunit.phar" %* > phpunit.cmd C:\bin> exit

6. 打开一个新的命令行并确认您可以从任何路径执行PHPUnit:

C:\Users\username> phpunit --version PHPUnit x.y.z by Sebastian Bergmann and contributors.

对于 Cygwin 和/或 MingW32(例如 TortoiseGit )shell 环境,您可以跳过上述第5步,只需将该文件保存为phpunit(不带.phar扩展名),然后通过该文件执行chmod 775 phpunit

验证 PHPUnit PHAR 版本

所有由 PHPUnit 项目发布的正式版代码都由发行版的发布经理签名。phar.phpunit.de上的 PGP 签名和 SHA1 哈希可用于验证。

以下示例详细说明了发布验证的工作原理 我们首先下载phpunit.phar以及分离的 PGP 签名phpunit.phar.asc

wget https://phar.phpunit.de/phpunit.phar
wget https://phar.phpunit.de/phpunit.phar.asc

我们希望验证 PHPUnit 的 PHP Archive(phpunit.phar)是否符合其分离签名(phpunit.phar.asc):

gpg phpunit.phar.asc
gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20A
gpg: Can't check signature: public key not found

我们6372C20A的本地系统中没有发布管理器的公钥()。为了继续进行验证,我们需要从密钥服务器中检索发布管理器的公钥。一个这样的服务器是pgp.uni-mainz.de。公钥服务器连接在一起,所以你应该能够连接到任何关键的服务器。

gpg --keyserver pgp.uni-mainz.de --recv-keys 0x4AA394086372C20A
gpg: requesting key 6372C20A from hkp server pgp.uni-mainz.de
gpg: key 6372C20A: public key "Sebastian Bergmann <sb@sebastian-bergmann.de>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

现在我们收到了名为 “Sebastian Bergmann sb@sebastian-bergmann.de ” 的实体的公钥。但是,我们无法验证这个密钥是由被称为塞巴斯蒂安贝格曼的人创建的。但是,让我们再次尝试验证发行签名。

gpg phpunit.phar.asc
gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20A
gpg: Good signature from "Sebastian Bergmann <sb@sebastian-bergmann.de>"
gpg:                 aka "Sebastian Bergmann <sebastian@php.net>"
gpg:                 aka "Sebastian Bergmann <sebastian@thephp.cc>"
gpg:                 aka "Sebastian Bergmann <sebastian@phpunit.de>"
gpg:                 aka "Sebastian Bergmann <sebastian.bergmann@thephp.cc>"
gpg:                 aka "[jpeg image of size 40635]"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: D840 6D0D 8294 7747 2937  7831 4AA3 9408 6372 C20A

在这一点上,签名是好的,但我们不相信这个关键。好的签名意味着文件没有被篡改。但是,由于公钥密码学的本质,您需要另外验证密钥6372C20A是由真正的 Sebastian Bergmann 创建的。

任何攻击者都可以创建一个公钥并将其上传到公钥服务器。然后他们可以创建一个由这个假密钥签名的恶意发布。然后,如果您试图验证此腐败版本的签名,它会成功,因为该密钥不是“真正的”密钥。因此,您需要验证此密钥的真实性。但是,验证公钥的真实性不在本文档的范围内。

在运行测试套件之前,创建一个 shell 脚本来管理 PHPUnit 安装,验证 GnuPG 签名可能是明智之举。例如:

#!/usr/bin/env bash
clean=1 # Delete phpunit.phar after the tests are complete?
aftercmd="php phpunit.phar --bootstrap bootstrap.php src/tests"
gpg --fingerprint D8406D0D82947747293778314AA394086372C20A
if [ $? -ne 0 ]; then
    echo -e "\033[33mDownloading PGP Public Key...\033[0m"
    gpg --recv-keys D8406D0D82947747293778314AA394086372C20A
    # Sebastian Bergmann <sb@sebastian-bergmann.de>
    gpg --fingerprint D8406D0D82947747293778314AA394086372C20A
    if [ $? -ne 0 ]; then
        echo -e "\033[31mCould not download PGP public key for verification\033[0m"
        exit
    fi
fi

if [ "$clean" -eq 1 ]; then
    # Let's clean them up, if they exist
    if [ -f phpunit.phar ]; then
        rm -f phpunit.phar
    fi
    if [ -f phpunit.phar.asc ]; then
        rm -f phpunit.phar.asc
    fi
fi

# Let's grab the latest release and its signature
if [ ! -f phpunit.phar ]; then
    wget https://phar.phpunit.de/phpunit.phar
fi
if [ ! -f phpunit.phar.asc ]; then
    wget https://phar.phpunit.de/phpunit.phar.asc
fi

# Verify before running
gpg --verify phpunit.phar.asc phpunit.phar
if [ $? -eq 0 ]; then
    echo
    echo -e "\033[33mBegin Unit Testing\033[0m"
    # Run the testing suite
    `$after_cmd`
    # Cleanup
    if [ "$clean" -eq 1 ]; then
        echo -e "\033[32mCleaning Up!\033[0m"
        rm -f phpunit.phar
        rm -f phpunit.phar.asc
    fi
else
    echo
    chmod -x phpunit.phar
    mv phpunit.phar /tmp/bad-phpunit.phar
    mv phpunit.phar.asc /tmp/bad-phpunit.phar.asc
    echo -e "\033[31mSignature did not match! PHPUnit has been moved to /tmp/bad-phpunit.phar\033[0m"
    exit 1
fi
      

Composer

如果使用 Composer 来管理phpunit/phpunit项目的依赖关系,只需在项目composer.json文件中添加(开发时)依赖项即可:

composer require --dev phpunit/phpunit ^6.4

可选包

以下可选软件包可用:

PHP_Invoker

用于使用超时调用可调用的实用程序类。该软件包需要在严格模式下强制执行测试超时。

该包包含在 PHPUnit 的 PHAR 分发中。可以使用以下命令通过 Composer 进行安装:

composer require --dev phpunit/php-invoker

DbUnit

用于 PHP / PHPUnit 的 DbUnit 端口支持数据库交互测试。

该包不包含在 PHPUnit 的 PHAR 分发中。可以使用以下命令通过 Composer 进行安装:

composer require --dev phpunit/dbunit

扫码关注腾讯云开发者

领取腾讯云代金券