Install PHPUnit

Chapter 1. Installing PHPUnit

Requirements

PHPUnit 5.7 requires PHP 5.6; using the latest version of PHP is highly recommended.
PHPUnit requires the dom and json extensions, which are normally enabled by default.
PHPUnit also requires the pcre, reflection, and spl extensions. These standard extensions are enabled by default and cannot be disabled without patching PHP's build system and/or C sources.
The code coverage report feature requires the Xdebug (2.2.1 or later) and tokenizer extensions. Generating XML reports requires the xmlwriter extension.

PHP Archive (PHAR)

The easiest way to obtain PHPUnit is to download a PHP Archive (PHAR) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file.
The phar extension is required for using PHP Archives (PHAR).
The openssl extension is required for using the --self-update
feature of the PHAR.
If the Suhosin extension is enabled, you need to allow execution of PHARs in your php.ini
:
suhosin.executor.include.whitelist = phar

To globally install the PHAR:
$
**wget https://phar.phpunit.de/phpunit.phar
**$
**chmod +x phpunit.phar
**$
**sudo mv phpunit.phar /usr/local/bin/phpunit
**$
**phpunit --version
**PHPUnit x.y.z by Sebastian Bergmann and contributors.

You may also use the downloaded PHAR file directly:
$
**wget https://phar.phpunit.de/phpunit.phar
**$
**php phpunit.phar --version
**PHPUnit x.y.z by Sebastian Bergmann and contributors.

Windows

Globally installing the PHAR involves the same procedure as manually installing Composer on Windows:
Create a directory for PHP binaries; e.g., C:\bin

Append **;C:\bin
** to your PATH
environment variable (related help)

Download https://phar.phpunit.de/phpunit.phar and save the file as C:\bin\phpunit.phar

Open a command line (e.g., press Windows+R » type **cmd
** » ENTER)

Create a wrapping batch script (results in C:\bin\phpunit.cmd
):
C:\Users\username>
**cd C:\bin
**C:\bin>
*echo @php "%~dp0phpunit.phar" % > phpunit.cmd
**C:\bin>
**exit
**

Open a new command line and confirm that you can execute PHPUnit from any path:
C:\Users\username>
**phpunit --version
**PHPUnit x.y.z by Sebastian Bergmann and contributors.

For Cygwin and/or MingW32 (e.g., TortoiseGit) shell environments, you may skip step 5. above, simply save the file as phpunit
(without .phar
extension), and make it executable via **chmod 775 phpunit
**.

Verifying PHPUnit PHAR Releases

All official releases of code distributed by the PHPUnit Project are signed by the release manager for the release. PGP signatures and SHA1 hashes are available for verification on phar.phpunit.de.
The following example details how release verification works. We start by downloading phpunit.phar
as well as its detached PGP signature phpunit.phar.asc
:
**wget https://phar.phpunit.de/phpunit.phar
****wget https://phar.phpunit.de/phpunit.phar.asc
**
We want to verify PHPUnit's PHP Archive (phpunit.phar
) against its detached signature (phpunit.phar.asc
):
**gpg phpunit.phar.asc
**gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20Agpg: Can't check signature: public key not found
We don't have the release manager's public key (6372C20A
) in our local system. In order to proceed with the verification we need to retrieve the release manager's public key from a key server. One such server is pgp.uni-mainz.de
. The public key servers are linked together, so you should be able to connect to any key server.
**gpg --keyserver pgp.uni-mainz.de --recv-keys 0x4AA394086372C20A
**gpg: requesting key 6372C20A from hkp server pgp.uni-mainz.degpg: key 6372C20A: public key "Sebastian Bergmann sb@sebastian-bergmann.de" importedgpg: Total number processed: 1gpg: imported: 1 (RSA: 1)
Now we have received a public key for an entity known as "Sebastian Bergmann sb@sebastian-bergmann.de". However, we have no way of verifying this key was created by the person known as Sebastian Bergmann. But, let's try to verify the release signature again.
**gpg phpunit.phar.asc
**gpg: Signature made Sat 19 Jul 2014 01:28:02 PM CEST using RSA key ID 6372C20Agpg: 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
At this point, the signature is good, but we don't trust this key. A good signature means that the file has not been tampered. However, due to the nature of public key cryptography, you need to additionally verify that key 6372C20A
was created by the real Sebastian Bergmann.
Any attacker can create a public key and upload it to the public key servers. They can then create a malicious release signed by this fake key. Then, if you tried to verify the signature of this corrupt release, it would succeed because the key was not the "real" key. Therefore, you need to validate the authenticity of this key. Validating the authenticity of a public key, however, is outside the scope of this documentation.
It may be prudent to create a shell script to manage PHPUnit installation that verifies the GnuPG signature before running your test suite. For example:

!/usr/bin/env bashclean=1 # Delete phpunit.phar after the tests are complete?aftercmd="php phpunit.phar --bootstrap bootstrap.php src/tests"gpg --fingerprint D8406D0D82947747293778314AA394086372C20Aif [ $? -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 fifiif [ "$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 fifi# Let's grab the latest release and its signatureif [ ! -f phpunit.phar ]; then wget https://phar.phpunit.de/phpunit.pharfiif [ ! -f phpunit.phar.asc ]; then wget https://phar.phpunit.de/phpunit.phar.ascfi# Verify before runninggpg --verify phpunit.phar.asc phpunit.pharif [ $? -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 fielse 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 1fi

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 5.7:
{ "require-dev": { "phpunit/phpunit": "5.5.*" }}
For a system-wide installation via Composer, you can run:
*composer global require "phpunit/phpunit=5.5."
**
Make sure you have ~/.composer/vendor/bin/
in your path.

Optional packages

The following optional packages are available:
PHP_Invoker

A utility class for invoking callables with a timeout. This package is required to enforce test timeouts in strict mode.
This package is included in the PHAR distribution of PHPUnit. It can be installed via Composer by adding the following "require-dev"
dependency:
*"phpunit/php-invoker": ""
**

DbUnit

DbUnit port for PHP/PHPUnit to support database interaction testing.
This package is included in the PHAR distribution of PHPUnit. It can be installed via Composer by adding the following "require-dev"
dependency:
**"phpunit/dbunit": ">=1.2"
**

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容

  • 白里透红异清纯, 淡妆素雅透冰心。 空灵飘逸远轻尘, 碧色丛中明目清。
    Odette伊菲阅读 124评论 0 1
  • 概述 EL & jstlsh是初学当中比较难的小瓶颈 EL只负责简化取值,jstl只负责对EL的值进行逻辑处理(j...
    AndroidCat阅读 368评论 0 1
  • 可使用 GPU 的 DOCKER 容器 在 GPU 加速的数据中心内轻松部署应用程序容器将应用程序封装到隔离的虚拟...
    安宇雨阅读 3,773评论 0 2
  • 如果可以,就找一个会做饭的爱人。不将就谁,不讨好谁。 前段时间,艾林约我去尚品屋。思索着他不是刚结婚吗,蜜月很忙呀...
    企鹅镜子阅读 552评论 0 0