最近準備着手開發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->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://amiworks.co.in/talk/installing-pear-and-phpunit-on-wamp-and-windows-7/
另外,安裝完之後,檢查php\pear\PHPUnit\Extensions 目錄下是否有包含Selenium開頭的文件以及文件夾,如果不存在,則需要另外安裝:pear install phpunit/PHPUnit_Selenium
否則使用的時候會提示找不到文件:
Failed opening required 'PHPUnit/Extensions/SeleniumTestCase.php'