PHPUnit configuration and usage tutorial under Windows_PHP tutorial

WBOY
Release: 2016-07-12 09:03:59
Original
1022 people have browsed it

PHPUnit configuration and usage tutorial under Windows

Since our project involves PHP, we need to unit test the PHP code. After some understanding, I decided to use PHPUnit to test php. PHPUnit spent a lot of time figuring out how to configure PHPUnit, and it was tearful to read the documentation on the official website. But once you know how to configure it, it's actually very simple.

  • System: Windows 10 Pro

  • PHP version: PHP 5.5.12

  • Server Tools: WAMPSERVER 2.5

  • PHPUnit version: PHPUnit 4.8

1. Configure PHPUnit

First go to the PHPUnit official website (click here to enter) to download the corresponding version. We are using php 5.5, so we chose PHPUnit 4.8. Get the .phar file and change the name to phpunit.phar .

Put this file anywhere. Taking myself as an example, I put it in the directory of our project, namely D:repositoryCourseManagementmobile_api_test.

Right-click My Computer and select Properties. Click Advanced System Settings on the left. At this time, System Properties pops up, select the Advanced tab, and click Environment Variables in the lower right corner. In the user variables, double-click PATH and add after the variable value ;D:repositoryCourseManagementmobile_api_test (note the semicolon at the front, and fill in the path where phpunit.phar is stored). This is configured so that PHPUnit can be used at any location. If not configured, PHPUnit needs to be used under the path of phpunit.phar.

One thing that is not mentioned in the official documentation is that PHP environment variables must also be set. For example, if my php.exe is in E:softwarewampbinphpphp5.5.12, then I also need to add after PATH;E:softwarewampbinphpphp5.5.12. PS: Maybe it’s because I haven’t installed the PHP IDE, so I haven’t configured it. I thought of adding this environment variable by chance.

Press the shortcut key Win R, enter cmd and press Enter. Enter the path where phpunit.phar is stored. Type echo @php "%~dp0phpunit.phar" %* > phpunit.cmd and press Enter. Then enter phpunit --version and press Enter. If you get the output PHPUnit x.y.z by Sebastian Bergmann and contributors., it means the configuration is complete. If there is an error, enter exit and press Enter to try again). As shown below:

2. Use PHPUnit for testing

To use PHPUnit, you must use the class. Taking login.php as an example, the location is D:repositoryCourseManagementmobile_api), our initial version is like this. Welcome to complain about the code in this blog):

<ol class="dp-c"><li class="alt"><span><span><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>) < 1) { </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>
Copy after login

There is no way to test it, so I made some modifications. First, create a folder login.php in the folder where classes is located, and create a new class_login.php in it. The content is login.php. The modified version:

<ol class="dp-j"><li class="alt"><span><span><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>
Copy after login

In addition, the content of the original login.php must also be modified. The modified content is as follows:

<ol class="dp-j"><li class="alt"><span><span><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>
Copy after login

Start writing test files

I put the test files in the D:repositoryCourseManagementmobile_api_test folder. Create a new file `login_test.php’ and write the following code:

<ol class="dp-j"><li class="alt"><span><span><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>
Copy after login

Execute test file

Shortcut key Win R, enter cmd and press Enter. Enter the directory of the test file and enter phpunit login_test.php to execute the test.

A simple test is done.

3. Exploration process

When I first downloaded PHPUnit, I got the .phar file and thought I needed to unzip it, which was embarrassing. After searching for a long time, I found a website that can decompress such files (click here to enter). But it’s of no use...

Following the official documentation, an error occurred during runtime:

'php' is not recognized as an internal or external command or operable program
or batch file.

Google search, Bing search, StackOverFlow search, Baidu search, the answers found are useless.

Mainly because they all assume that you have configured PHP environment variables...

最后想着是不是之前生成的 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>
Copy after login

再进入 phpunit.cmd 所在文件夹,运行 phpunit --version。得到:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

问题解决!

经过这次的探索,以后碰到”找不到xxx”这种问题的时候,首先会想起环境变量的设置。

比如解决这个问题的同一天下午,我想使用Visual Studio Code的Git功能,但是却得到提示:

第一个反应是:我明明安装了msysgit啊。

第二个反应是:会不会是环境变量没配置?打开环境变量配置,果然没有。于是将 git.exe 所在文件夹的路径添加进去。重启visual Studio Code,问题解决!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1077040.htmlTechArticlePHPUnit 在 Windows 下的配置及使用教程 由于我们项目涉及到php,因此需要对php代码进行单元测试。经过一番了解,决定用PHPUnit来测试php。PH...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template