最近准备着手开发Magento的插件,为了保证插件的代码质量,决定采用TDD的方法进行开发。在PHP实战中看到的TDD的开发方法,突然觉得豁然开朗,开发起来一点都不费力气,现在终于有机会亲自实现了。

当然,在卷起袖子之前,还需要配置好PHPUnit的开发环境。在PHPUnit的最新版本已经迁移到自己的pear.phpunit.de网站上了。本文假设你已经配置好Xampp的开发环境,我之前的文章PHP开发环境的搭建

首先升级你的pear版本,最新PHPUnit 3.6 要求PEAR的版本较高。在开始菜单打开CMD命令行。

cd /d D:\xampp\php
pear config-show
CONFIGURATION (CHANNEL PEAR.PHP.NET):
=====================================
Auto-discover new Channels     auto_discover    1
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          D:\xampp\php
PEAR documentation directory   doc_dir          D:\xampp\php\docs
PHP extension directory        ext_dir          D:\xampp\php\ext
PEAR directory                 php_dir          D:\xampp\php\pear
PEAR Installer cache directory cache_dir        D:\xampp\php\tmp
PEAR configuration file        cfg_dir          D:\xampp\php\cfg
directory
PEAR data directory            data_dir         D:\xampp\php\data
PEAR Installer download        download_dir     D:\xampp\php\tmp
directory
PHP CLI/CGI binary             php_bin          D:\xampp\php\.\php.exe
php.ini location               php_ini          <not set>
--program-prefix passed to     php_prefix       <not set>
PHP's ./configure
--program-suffix passed to     php_suffix       <not set>
PHP's ./configure
PEAR Installer temp directory  temp_dir         D:\xampp\php\tmp
PEAR test directory            test_dir         D:\xampp\php\tests
PEAR www files directory       www_dir          D:\xampp\php\www
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            0
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          c:\gnupg\gpg.exe
Signature Key Directory        sig_keydir       C:\Windows\pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         C:\Windows\pear.ini
System Configuration File      Filename         C:\Windows\pearsys.ini

以上是我的pear配置文件,仅供参考,输入以下命令升级pear的版本

pear upgrade pear

查看升级后的版本

pear –V
PEAR Version: 1.9.4
PHP Version: 5.3.5
Zend Engine Version: 2.3.0
Running on: Windows NT ARTHUR-PC 6.1 build 7600 (Unknow Windows version
Ultimate Edition) i586

升级PHPUnit的版本到3.6,pear自带的版本太低了。

pear upgrade pear/PHPUnit

出现更新失败的信息

pear/PHPUnit is already installed and is the same as the released version 1.3.2
  upgrade failed

先卸载当前的PHPUnit版本

pear uninstall pear/PHPUnit

注意设置自动添加频道

pear config-set auto_discover 1
添加PHPUnit的必要频道
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com

安装PHPUnit

pear install --alldeps phpunit/PHPUnit

查看PHPUnit的版本

phpunit –V
 
PHPUnit 3.6.3 by Sebastian Bergmann.

添加pear的路径到你的环境变量,例如D:\xampp\php。这样就能直接打pear命令了。

编写你的第一个测试

cd /d D:\xampp\htdocs\dev142\tests

新建index.php文件

<?php
//This is my first test
class MyFirstTest extends PHPUnit_Framework_TestCase{
    public function testFirst(){
        $stack = array();
        $this-&gt;assertEquals(0,count($stack));
    }
}
?>

测试你编写的代码

phpunit index.php
PHPUnit 3.6.3 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.50Mb
 
OK (1 test, 1 assertion)

参考

http://pear.phpunit.de/

http://amiworks.co.in/talk/installing-pear-and-phpunit-on-wamp-and-windows-7/

https://github.com/sebastianbergmann/phpunit/

http://blog.lixiphp.com/windows-install-pear-phpunit/

 

另外,安装完之后,检查php\pear\PHPUnit\Extensions 目录下是否有包含Selenium开头的文件以及文件夹,如果不存在,则需要另外安装:pear install phpunit/PHPUnit_Selenium
否则使用的时候会提示找不到文件:

Failed opening required 'PHPUnit/Extensions/SeleniumTestCase.php'