1. 创建 UnitTest 环境
选用 Selenium + PHPUnit
1.1 安装 Selenium
a) 去 Selenium 官网( http://seleniumhq.org/download/ )下载最新版本( 2.0rc3 ) selenium-server-2.0rc3.zip ;
b) 解压 ZIP 到合适的位置(我放在 C:/selenium-2.0rc3/ );
c) 管理员权限打开一个 DOS 窗口,进入 Selenium 根目录( C:/selenium-2.0rc3/ );
d) 运行命令: java -jar selenium-server-standalone-2.0rc3
1.2 安装 PHPUnit
a) XAMPP (我使用的是版本 1.7.4 )使用 PEAR ( PHP Extension and Application Repository )来管理 PHP 扩展组件的安装。我将 XAMPP 安装在 C:/XAMPP 目录(为方便下面用 [XAMPP] 指代 XAMPP 根目录),其中 [XAMPP]/php/PEAR 目录下有两个 PHPUnit 相关的子目录: PHPUnit 和 PHPUnit2 。但是这个版本的 PHPUnit 有些问题,我们必须先删除这两个目录,以免和下面将要使用 PEAR 安装的最新版本的 PHPUnit 冲突。
b) 另一个需要注意的是 XAMPP V1.7.4 自带的 PEAR 配置文件有问题。所以在使用 PEAR 安装最新的 PHPUnit 之前,我们必须先从 PEAR 官网上下载最新的配置文件 go-pear.phar ( http://pear.php.net/go-pear.phar )。然后用这个新版本替换 XAMPP 中自带的问题版本( [XAMPP]/php/PEAR/go-pear.phar )
c) 管理员权限打开一个 DOS 窗口,进入 [XAMPP]/php 目录;
运行 go-pear.bat ;
Are you installing a system-wide PEAR or a local copy?
(system|local) [system] : **** 这里我直接 Enter 选择 system
Below is a suggested file layout for your new PEAR installation. To change individual locations, type the number in front of the directory. Type 'all' to change all of them or simply press Enter to accept these locations.
1. Installation base ($prefix) : C:/xampp/php
2. Temporary directory for processing : C:/xampp/php/tmp
3. Temporary directory for downloads : C:/xampp/php/tmp
4. Binaries directory : C:/xampp/php
5. PHP code directory ($php_dir) : C:/xampp/php/pear
6. Documentation directory : C:/xampp/php/docs
7. Data directory : C:/xampp/php/data
8. User-modifiable configuration files directory : C:/xampp/php/cfg
9. Public Web Files directory : C:/xampp/php/www
10. Tests directory : C:/xampp/php/tests
11. Name of configuration file : C:/Windows/pear.ini
12. Path to CLI php.exe : C:/xampp/php/.
1-12, 'all' or Enter to continue: **** 这里我直接 Enter 接受所有缺省目录
Beginning install...
Configuration written to C:/Windows/pear.ini...
Initialized registry...
Preparing to install...
installing phar://C:/xampp/php/PEAR/go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.3.7.tar...
installing phar://C:/xampp/php/PEAR/go-pear.phar/PEAR/go-pear-tarballs/Console_Getopt-1.3.0.tar...
installing phar://C:/xampp/php/PEAR/go-pear.phar/PEAR/go-pear-tarballs/PEAR-1.9.3.tar...
installing phar://C:/xampp/php/PEAR/go-pear.phar/PEAR/go-pear-tarballs/Structures_Graph-1.0.4.tar...
installing phar://C:/xampp/php/PEAR/go-pear.phar/PEAR/go-pear-tarballs/XML_Util-1.2.1.tar...
**** 这些 Warning 貌似可以忽略
PHP Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
ERROR: file /Archive_Tar-1.3.7/Archive/Tar.php does not exist
PHP Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
ERROR: file /Console_Getopt-1.3.0/Console/Getopt.php does not exist
PHP Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
ERROR: file /Structures_Graph-1.0.4/docs/html/media/banner.css does not exist
PHP Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
ERROR: file /XML_Util-1.2.1/examples/example.php does not exist
PHP Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/System.php on line 302
Warning: mkdir(): Invalid argument in phar://C:/xampp/php/PEAR/go-pear.phar/Syst
em.php on line 302
ERROR: file /PEAR-1.9.3/OS/Guess.php does not exist
** WARNING! Old version found at C:/xampp/php, please remove it or be sure to use the new c:/xampp/php/pear.bat command
The 'pear' command is now at your service at c:/xampp/php/pear.bat
**** 实际上这个注册表文件在 [XAMPP]/php 目录下,名称就是 PEAR_EVN.reg 。应该是开发者写文档的时候使用 XAMPP 安装目录变量和文件名 PEAR_EVN.reg 拼接成实际路径。但是忘了在拼接时插入目录分隔符。 [XAMPP] + “PEAR_EVN.reg” => [XAMPP]+ “/PEAR_EVN.reg” 。
* WINDOWS ENVIRONMENT VARIABLES *
For convenience, a REG file is available under C:/xampp/phpPEAR_ENV.reg.
This file creates ENV variables for the current user.
Double-click this file to add it to the current user registry.
请按任意键继续 . . .
d) 更新 PEAR 通道
[XAMPP]/php> pear update-channels
Updating channel "pear.php.net"
Update of Channel "pear.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
e) 更新 PEAR 自身和其现有的包
[XAMPP]/php> pear upgrade –alldeps
Nothing to upgrade
f) 添加 PHPUnit 所需的 PEAR 通道
[XAMPP] /php> pear channel-discover components.ez.no *** PHPUnit 依赖项
Adding Channel "components.ez.no" succeeded
Discovery of channel "components.ez.no" succeeded
[XAMPP] /php> pear channel-discover pear.symfony-project.com *** PHPUnit 依赖项
Adding Channel "pear.symfony-project.com" succeeded
Discovery of channel "pear.symfony-project.com" succeeded
[XAMPP] /php> pear channel-discover pear.phpunit.de *** PHPUnit
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded
g) 更新 PEAR 至最新版本
[XAMPP] /php> pear channel-update pear.php.net *** 添加 PEAR 自身更新通道
[XAMPP] /php> pear.bat list-upgrades *** 列出所有可用更新
[XAMPP] /php> pear upgrade-all *** 安装所有可用更新
h) 安装 PHPUnit 及其依赖项
[XAMPP] /php> pear install --alldeps phpunit/PHPUnit
…………
install ok: channel://pear.phpunit.de/DbUnit-1.0.2
install ok: channel://pear.phpunit.de/File_Iterator-1.2.6
install ok: channel://pear.phpunit.de/Text_Template-1.1.0
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.0
install ok: channel://pear.phpunit.de/PHPUnit_Selenium-1.0.3
install ok: channel://pear.phpunit.de/PHP_TokenStream-1.0.1
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.0.4
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.0.9
install ok: channel://pear.phpunit.de/PHPUnit-3.5.14
如果看到这些组件都被安装,那么恭喜你,可以开始利用 PHPUnit 开始你的 TDD 生活了。