由于我们项目涉及到php,因此需要对php代码进行单元测试。经过一番了解,决定用PHPUnit来测试php。PHPUnit花了不少时间摸索如何配置PHPUnit,看官网的文档也是一把泪。但知道怎么配置后,其实还是很简单的。
系统:Windows 10 专业版
PHP版本:PHP 5.5.12
服务器工具:WAMPSERVER 2.5
PHPUnit版本:PHPUnit 4.8
一、配置PHPUnit
首先到PHPUnit官网(点此进入)下载相应的版本。我们使用的是php 5.5,于是选择PHPUnit 4.8。得到 .phar
文件,并把名字改为 phpunit.phar
。
把该文件放到任意位置。以我自己为例,我把它放到我们项目的目录下,即D:\repository\CourseManagement\mobile_api_test
。
右键 我的电脑 ,选择 属性 。点击左侧的 高级系统设置 。此时弹出 系统属性 ,选择 高级 选项卡,点击右下角的 环境变量 。 在用户变量里面,双击 PATH ,在 变量值 后面添加;D:\repository\CourseManagement\mobile_api_test
(注意最前面的分号,路径填存放phpunit.phar的路径)。配置这个是为了在任意位置都能使用PHPUnit,不配置的话,需要在phpunit.phar的路径下才能使用PHPUnit。
官方文档里有一点没有提到,就是php的环境变量也要设置。比如我的
php.exe
在E:\software\wamp\bin\php\php5.5.12
里,那么也要在 PATH 后面添加;E:\software\wamp\bin\php\php5.5.12
。PS:可能是因为我没有安装PHP的IDE,所以一直没有配置它,偶然之间才想到可能要添加这个环境变量。
按快捷键 Win + R
,输入cmd并回车。进入存放phpunit.phar的路径。输入 echo @php "%~dp0phpunit.phar" %* > phpunit.cmd
并回车。接着输入 phpunit --version
并回车。如果得到输出 PHPUnit x.y.z by Sebastian Bergmann and contributors.
则表示配置好了(如果有误,输入exit
并回车,重新来一遍)。如下图:
二、使用PHPUnit进行测试
使用PHPUnit,必须使用 类 。以login.php为例(位置在D:\repository\CourseManagement\mobile_api
),我们的初始版本是这样的(欢迎吐槽本博客内的代码):
<ol class="dp-c"> <li class="alt"><span><span><php></php></span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="func">error_reporting</span><span>(0); </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="vars">$workNumber</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"login-user"</span><span>]; </span></span></li> <li><span> <span class="vars">$password</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"login-password"</span><span>]; </span></span></li> <li class="alt"><span> <span class="vars">$tableName</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"ident"</span><span>]; </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="vars">$con</span><span> = mysqli_connect(</span><span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>, </span><span class="string">"teacher_class_system"</span><span>); </span></span></li> <li><span> <span class="keyword">if</span><span> (!</span><span class="vars">$con</span><span>) { </span></span></li> <li class="alt"><span> <span class="keyword">die</span><span>(</span><span class="string">'Could not connect: '</span><span> . mysql_error()); </span></span></li> <li><span> } <span class="keyword">else</span><span> { </span></span></li> <li class="alt"><span> mysqli_query(<span class="vars">$con</span><span>, </span><span class="string">"SET NAMES utf8"</span><span>); </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="vars">$result</span><span> = mysqli_query(</span><span class="vars">$con</span><span>, </span><span class="string">"SELECT * FROM $tableName where workNumber = $workNumber and password = $password"</span><span>); </span></span></li> <li><span> <span class="keyword">if</span><span> (mysqli_num_rows(</span><span class="vars">$result</span><span>) </span></span></li> <li class="alt"><span> <span class="func">echo</span><span> </span><span class="string">"false"</span><span>; </span></span></li> <li><span> } <span class="keyword">else</span><span> { </span></span></li> <li class="alt"><span> <span class="vars">$result_arr</span><span> = mysqli_fetch_assoc(</span><span class="vars">$result</span><span>); </span></span></li> <li><span> <span class="func">echo</span><span> json_encode(</span><span class="vars">$result_arr</span><span>, JSON_UNESCAPED_UNICODE); </span></span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li><span> > </span></li> </ol>
这样没法测试,于是进行一翻修改。首先在 login.php
所在文件夹里建立一个文件夹 classes
,并在里面新建 class_login.php
,内容为 login.php
修改后的版本:
<ol class="dp-j"> <li class="alt"><span><span><php></php></span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="keyword">class</span><span> Login { </span></span></li> <li><span> <span class="comment">// 测试工具PHPUnit要求一定要在这里给变量默认值,于是默认为空。</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">public</span><span> function login($workNumber = </span><span class="string">""</span><span>,$password = </span><span class="string">""</span><span>,$tableName = </span><span class="string">""</span><span>) { </span></span></li> <li><span> $con = mysqli_connect(<span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>, </span><span class="string">"teacher_class_system"</span><span>); </span></span></li> <li class="alt"><span> <span class="keyword">if</span><span> (!$con) { </span></span></li> <li><span> die(<span class="string">'Could not connect: '</span><span> . mysqli_error()); </span></span></li> <li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li> <li><span> mysqli_query($con, <span class="string">"SET NAMES utf8"</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span> $result = mysqli_query($con, <span class="string">"SELECT * FROM $tableName where workNumber = $workNumber and password = $password"</span><span>); </span></span></li> <li class="alt"><span> <span class="keyword">if</span><span> (!$result mysqli_num_rows($result) == </span><span class="number">0</span><span>) { </span></span></li> <li><span> <span class="keyword">return</span><span> </span><span class="string">"false"</span><span>; </span></span></li> <li class="alt"><span> } <span class="keyword">else</span><span> { </span></span></li> <li><span> $result_arr = mysqli_fetch_assoc($result); </span></li> <li class="alt"><span> <span class="keyword">return</span><span> json_encode($result_arr, JSON_UNESCAPED_UNICODE); </span></span></li> <li><span> } </span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"><span> > </span></li> </ol>
除此之外,也要修改原来那个 login.php
的内容,修改后如下:
<ol class="dp-j"> <li class="alt"><span><span><php></php></span></span></li> <li><span> error_reporting(<span class="number">0</span><span>); </span></span></li> <li class="alt"><span> </span></li> <li><span> require_once <span class="string">'./classes/class_login.php'</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li><span> $workNumber = $_POST[<span class="string">"login-user"</span><span>]; </span></span></li> <li class="alt"><span> $password = $_POST[<span class="string">"login-password"</span><span>]; </span></span></li> <li><span> $tableName = $_POST[<span class="string">"ident"</span><span>]; </span></span></li> <li class="alt"><span> </span></li> <li><span> $log = <span class="keyword">new</span><span> Login; </span></span></li> <li class="alt"><span> $response = $log->login($workNumber,$password,$tableName); </span></li> <li><span> </span></li> <li class="alt"><span> <span class="keyword">if</span><span>($response != </span><span class="string">"false"</span><span>) { </span></span></li> <li><span> session_start(); </span></li> <li class="alt"><span> $_SESSION[<span class="string">'id'</span><span>]=$tableName; </span></span></li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li><span> echo $response; </span></li> <li class="alt"><span> </span></li> <li><span> > </span></li> </ol>
开始写测试文件
我把测试文件放在 D:\repository\CourseManagement\mobile_api_test
这个文件夹内。新建一个文件 `login_test.php’ ,并写入以下代码:
<ol class="dp-j"> <li class="alt"><span><span><php></php></span></span></li> <li><span> require_once dirname(__FILE__).<span class="string">'/../mobile_api/classes/class_login.php'</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li><span> <span class="keyword">class</span><span> LoginTest </span><span class="keyword">extends</span><span> PHPUnit_Framework_TestCase { </span></span></li> <li class="alt"><span> <span class="keyword">public</span><span> function testLoginSuccess() { </span></span></li> <li><span> $expected = <span class="string">'{"workNumber":"00001","password":"00001","name":"西瓜","sex":"男","birthday":"20151201","department":"计算机","telephone":"110","email":"git@github.com"}'</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li><span> $workNumber = <span class="string">'00001'</span><span>; </span></span></li> <li class="alt"><span> $password = <span class="string">'00001'</span><span>; </span></span></li> <li><span> $tableName = <span class="string">'user_teacher'</span><span>; </span></span></li> <li class="alt"><span> $lg = <span class="keyword">new</span><span> Login; </span></span></li> <li><span> $actual = $lg->login($workNumber,$password,$tableName); </span></li> <li class="alt"><span> </span></li> <li><span> $<span class="keyword">this</span><span>->assertEquals($expected,$actual); </span></span></li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"><span> function testLoginFail() { </span></li> <li><span> $expected = <span class="string">'false'</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li><span> $workNumber = <span class="string">'11111'</span><span>; </span></span></li> <li class="alt"><span> $password = <span class="string">'11111'</span><span>; </span></span></li> <li><span> $tableName = <span class="string">'user_teacher'</span><span>; </span></span></li> <li class="alt"><span> </span></li> <li><span> $lg = <span class="keyword">new</span><span> Login; </span></span></li> <li class="alt"><span> $actual = $lg->login($workNumber,$password,$tableName); </span></li> <li><span> $<span class="keyword">this</span><span>->assertEquals($expected,$actual); </span></span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"><span> </span></li> <li><span> > </span></li> </ol>
执行测试文件
快捷键 Win + R ,输入cmd并回车。进入该测试文件的目录,输入 phpunit login_test.php
执行测试。
简单的测试就完成了。
三、摸索过程
一开始下载PHPUnit,得到 .phar
文件,以为要解压,囧。找了半天才发现有一个能够解压这种文件的网站(点此进入)。然而并没有什么用……
照着官方文档来做,运行时出现错误:
‘php’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Google搜索,必应搜索,StackOverFlow搜索,百度搜索,搜到的答案都没有用。
主要是因为他们都默认你已经配置好了PHP环境变量……
最后想着是不是之前生成的 phpunit.cmd
有问题?于是查看一下这个文件的内容。突然想到是不是PHP环境变量没设置的原因?打开cmd,输入 php --version
。得到:
‘php’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。
和上面的错误一样!果然是这里的问题。于是把 ;E:\software\wamp\bin\php\php5.5.12
添加到环境变量中。再运行 php --version
得到:
<ol class="dp-c"> <li class="alt"><span><span>PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) </span></span></li> <li><span>Copyright (c) 1997-2014 The PHP Group </span></li> <li class="alt"><span>Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies </span></li> <li><span>with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans </span></li> </ol>로그인 후 복사
再进入 phpunit.cmd
所在文件夹,运行 phpunit --version
。得到:
PHPUnit 4.8.18 by Sebastian Bergmann and contributors.
问题解决!
经过这次的探索,以后碰到”找不到xxx”这种问题的时候,首先会想起环境变量的设置。
比如解决这个问题的同一天下午,我想使用Visual Studio Code的Git功能,但是却得到提示:
第一个反应是:我明明安装了msysgit啊。
第二个反应是:会不会是环境变量没配置?打开环境变量配置,果然没有。于是将 git.exe
所在文件夹的路径添加进去。重启visual Studio Code,问题解决!