PHPUnit Configuration and use tutorial in Windows

Source: Internet
Author: User
Tags add connect variables php code version variable zend visual studio

Because our project involves PHP, we need to unit test the PHP code. After some understanding, decided to use PHPUnit to test PHP. PHPUnit spent a lot of time groping how to configure PHPUnit, Reader Web document is a tear. But know how to configure, in fact, it is very simple.

  • System: Windows 10 Professional Edition

  • PHP Version: PHP 5.5.12

  • Server Tools: Wampserver 2.5

  • PHPUnit Version: PHPUnit 4.8

First, configure PHPUnit

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

Put the file in any location. For my own example, I put it in the directory of our project, that is D:\repository\CourseManagement\mobile_api_test .

Right click on my Computer and select properties . Click on the Advanced system settings on the left. Now eject the system Properties , select the Advanced tab, and click on the environment variable in the lower right corner. In the user variable, double-click Path, add the value after the variable ;D:\repository\CourseManagement\mobile_api_test (note the first semicolon, the path fills the Phpunit.phar path). Configure this is to be able to use PHPUnit in any location, do not configure, you need to phpunit.phar the path to use the PHPUnit.

One thing that is not mentioned in the official document is that PHP's environment variables are set. For example, I am php.exe in E:\software\wamp\bin\php\php5.5.12 , then also to add after the PATH ;E:\software\wamp\bin\php\php5.5.12 . PS: Probably because I do not have the IDE installed PHP, so I have not configured it, accidentally just think of the possibility to add this environment variable.

Press the shortcut key and Win + R enter CMD and return. Enter the path to store the Phpunit.phar. Enter echo @php "%~dp0phpunit.phar" %* > phpunit.cmd and return. Then enter phpunit --version and return. If the output PHPUnit x.y.z by Sebastian Bergmann and contributors. is configured (if wrong, enter exit and return and start over again). The following figure:

Second, the use of phpunit for testing

Using PHPUnit, you must use a class . Take login.php for example (location D:\repository\CourseManagement\mobile_api ), our initial version is this (Welcome to the code in this blog):

 
 
  1. error_reporting (0);
  2. $workNumber = $_post["Login-user"];
  3. $password = $_post["Login-password"];
  4. $tableName = $_post["ident"];
  5. $con = Mysqli_connect ("localhost", "root", "" "," Teacher_class_system ");
  6. if (! $con) {
  7. Die (' Could not connect: '. Mysql_error ());
  8. } else {
  9. Mysqli_query ($con, "SET NAMES UTF8");
  10. $result = Mysqli_query ($con, "SELECT * from $tableName where worknumber = $workNumber and password = $password");
  11. if (Mysqli_num_rows ($result) < 1) {
  12. echo "false";
  13. } else {
  14. $result _arr = Mysqli_fetch_assoc ($result);
  15. Echo Json_encode ($result _arr, Json_unescaped_unicode);
  16. }
  17. }
  18. >

This is impossible to test, so make a change. First login.php Create a folder in the folder and classes create a new one in class_login.php it, with the login.php modified version:

 
 
  1. Class Login {
  2. The test tool PHPUnit requirements must give the variable default value here, so the default is null.
  3. Public Function Login ($workNumber = "", $password = "", $tableName = "") {
  4. $con = Mysqli_connect ("localhost", "root", "" "," Teacher_class_system ");
  5. if (! $con) {
  6. Die (' Could not connect: '. Mysqli_error ());
  7. } else {
  8. Mysqli_query ($con, "SET NAMES UTF8");
  9. $result = Mysqli_query ($con, "SELECT * from $tableName where worknumber = $workNumber and password = $password");
  10. if (! $result mysqli_num_rows ($result) = = 0) {
  11. Return "false";
  12. } else {
  13. $result _arr = Mysqli_fetch_assoc ($result);
  14. Return Json_encode ($result _arr, Json_unescaped_unicode);
  15. }
  16. }
  17. }
  18. }
  19. >

In addition, the original content should also be modified login.php , as follows:

 
 
  1. error_reporting (0);
  2. Require_once './classes/class_login.php ';
  3. $workNumber = $_post["Login-user"];
  4. $password = $_post["Login-password"];
  5. $tableName = $_post["ident"];
  6. $log = new Login;
  7. $response = $log->login ($workNumber, $password, $tableName);
  8. if ($response!= "false") {
  9. Session_Start ();
  10. $_session[' id ']= $tableName;
  11. }
  12. Echo $response;
  13. >

Start writing test files

I put the test files in D:\repository\CourseManagement\mobile_api_test this folder. Create a new file ' login_test.php ' and write the following code:

 
 
  1. Require_once dirname (__file__). ' /.. /mobile_api/classes/class_login.php ';
  2. Class Logintest extends Phpunit_framework_testcase {
  3. Public Function testloginsuccess () {
  4. $expected = ' {"Worknumber": "00001", "Password": "00001", "name": "Watermelon", "sex": "Male", "Birthday": "20151201", "department": " Computer "," Telephone ":" The "," email ":" git@github.com "};
  5. $workNumber = ' 00001 ';
  6. $password = ' 00001 ';
  7. $tableName = ' User_teacher ';
  8. $LG = new Login;
  9. $actual = $lg->login ($workNumber, $password, $tableName);
  10. $this->assertequals ($expected, $actual);
  11. }
  12. function Testloginfail () {
  13. $expected = ' false ';
  14. $workNumber = ' 11111 ';
  15. $password = ' 11111 ';
  16. $tableName = ' User_teacher ';
  17. $LG = new Login;
  18. $actual = $lg->login ($workNumber, $password, $tableName);
  19. $this->assertequals ($expected, $actual);
  20. }
  21. }
  22. >

Executing test files

Shortcut key Win + R, input cmd and enter. Enter the directory where the test file is to be phpunit login_test.php executed and run the test.

The simple test is done.

Iii. the process of exploration

First download phpunit, get .phar files, think to unpack, embarrassed. Looking for a long time to find that there is a can extract such a file of the site (click here to enter). However, there is no use ...

Follow the official documentation and run with an error:

' PHP ' is not an internal or external command, nor is it a running program
or batch files.

Google search, Bing search, StackOverflow Search, Baidu Search, found that the answer is not used.

Mainly because they all default you have configured the PHP environment variables ...

Finally think about whether the previous generation of the phpunit.cmd problem? So look at the contents of this file. Suddenly think of the PHP environment variable is not set the reason? Open cmd and enter php --version . Get:

' PHP ' is not an internal or external command, nor is it a running program
or batch files.

Same as the above error! Sure enough, it's the problem here. So ;E:\software\wamp\bin\php\php5.5.12 add it to the environment variable. To run it again php --version :

  
   
   
  1. PHP 5.5.12 (CLI) (BUILT:APR 30 2014 11:20:58)
  2. Copyright (c) 1997-2014 the PHP Group
  3. Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
  4. With Xdebug v2.2.5, the Copyright (c) 2002-2014, by Derick Rethans

Then go phpunit.cmd to the folder where you are running phpunit --version . Get:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

Problem solved!

After this exploration, later encountered "Find XXX" This problem, the first will be reminded of the setting of environment variables.

For example, on the same day in the afternoon, I wanted to use the Git feature of Visual Studio code, but I was prompted:

The first reaction is: I clearly installed the msysgit AH.

The second response is: Will the environment variable not be configured? Open environment variable configuration, sure enough. Then git.exe add the path to the folder where you are located. Restart visual Studio Code, problem Resolution!

"Responsible Editor: Wangxueyan TEL: (010) 68476606"


Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.