PHPUnit in Windows configuration and use tutorial _php tutorial

Source: Internet
Author: User
Tags vars

PHPUnit Configuration and usage tutorials under Windows


Since 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, crossing Web document is a tear. But know how to configure, in fact, it is very simple.

  • System: Windows 10 Pro

  • PHP Version: PHP 5.5.12

  • Server Tools: Wampserver 2.5

  • PHPUnit Version: PHPUnit 4.8

First, configure the PHPUnit

First, go to the PHPUnit official network to enter) to download the appropriate version. We were using PHP 5.5, so we chose PHPUnit 4.8. Get .phar the document and change the name to phpunit.phar .

Put the file in any position. Take myself for example, I put it in the directory of our project, that is D:\repository\CourseManagement\mobile_api_test .

Right -click My Computer , select Properties . Click Advanced System Settings on the left. This brings up 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 first semicolon after the value of the variable , ;D:\repository\CourseManagement\mobile_api_test and the path fills the path of the Phpunit.phar. Configure this to be able to use phpunit anywhere, without configuration, you need to phpunit.phar the path to use PHPUnit.

One thing that is not mentioned in the official documentation is that the environment variables of PHP are also set. For example php.exe , my in E:\software\wamp\bin\php\php5.5.12 , then also to be added after PATH ;E:\software\wamp\bin\php\php5.5.12 . PS: Probably because I did not install PHP IDE, so I have not configured it, accidentally only thought might want to add this environment variable.

Press the shortcut key Win + R , type cmd and enter. Enter the path where the Phpunit.phar is stored. Enter echo @php "%~dp0phpunit.phar" %* > phpunit.cmd and return. Then enter phpunit --version and return. If you get the output PHPUnit x.y.z by Sebastian Bergmann and contributors. then the configuration is OK if it is wrong, enter exit it and return it again. Such as:

Second, the use of PHPUnit test

With PHPUnit, you must use classes . Take login.php as an example of location in D:\repository\CourseManagement\mobile_api ), our initial version is like this welcome to the code in this blog:

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

It was impossible to test, so a change was made. First login.php Create a folder in your folder classes and create a new one in class_login.php it, with login.php the modified version:

 
 
  1. < span="">
  2. class Login {
  3. //test tool PHPUNIT requirements must be given here to the default value of the variable, so the default is empty.
  4. Public function Login ($workNumber = "", $password = " ", $tableName = "" ) {
  5. $con = Mysqli_connect ("localhost", "root" , " ", "Teacher_class_system" );
  6. if (! $con) {
  7. Die (' Could not connect: ' . Mysqli_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 (! $result | | mysqli_num_rows ($result) = = 0) {
  12. return "false";
  13. } Else {
  14. $result _arr = Mysqli_fetch_assoc ($result);
  15. return Json_encode ($result _arr, json_unescaped_unicode);
  16. }
  17. }
  18. }
  19. }
  20. >

In addition, you have to modify the original login.php content, modified as follows:

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

Start writing test files

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

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

Execute test File

Shortcut key Win + R, input cmd and enter. Enter the directory of the test file to phpunit login_test.php perform the test.

The simple test is done.

Three, the groping process

First download PHPUnit, get the .phar file, thought to unzip, embarrassed. Looking for a long while to find a can unzip this file site point this entry). But it's no use ...

According to the official documentation, there was an error running:

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

Google search, Bing search, StackOverflow Search, Baidu Search, found the answer is no use.

Mainly because they are all default you have already configured the PHP environment variable ...

Did you finally think about the problem that was generated before phpunit.cmd ? Then look at the contents of this file. Suddenly think that is not the PHP environment variable is not set the reason? Open cmd, enter php --version . Get:

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

The same as the error above! Sure enough, it's the problem here. It is then ;E:\software\wamp\bin\php\php5.5.12 added to the environment variable. Run again to php --version get:

  
   
   

Re phpunit.cmd -Enter your folder and run phpunit --version . Get:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

Problem Solving!

After this exploration, after encountering the "can not find xxx" This problem, the first will remember the environment variable settings.

For example, in the same afternoon, I wanted to use Visual Studio Code 's git functionality, but I got a hint:

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

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

http://www.bkjia.com/PHPjc/1077040.html www.bkjia.com true http://www.bkjia.com/PHPjc/1077040.html techarticle PHPUnit Configuration and Usage tutorial under Windows because our project involves PHP, we need to unit-test the PHP code. After some understanding, decided to use PHPUnit to test PHP. PH ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.