PHPUnit configuration and usage in Windows tutorial _ PHP Tutorial

Source: Internet
Author: User
PHPUnit configuration and usage tutorial in Windows. PHPUnit configuration and usage tutorials in Windows because our project involves php, we need to perform unit tests on php code. After some understanding, I decided to use PHPUnit to test php. How to configure and use PH PHPUnit in Windows

Because our project involves php, we need to perform unit tests on php code. After some understanding, I decided to use PHPUnit to test php. PHPUnit spent a lot of time exploring how to configure PHPUnit. reading the official documentation is also a tear. However, knowing how to configure it is actually quite simple.

  • System: Windows 10 Professional Edition

  • PHP version: PHP 5.5.12

  • Server tool: WAMPSERVER 2.5

  • PHPUnit: PHPUnit 4.8

1. configure PHPUnit

Click here on the PHPUnit official website to download the corresponding version. We use php 5.5, so we chose PHPUnit 4.8. Get.pharFile, and change the namephpunit.phar.

Place the file in any location. Take myself as an example. I put it under the Directory of our project, that isD:\repository\CourseManagement\mobile_api_test.

Right-clickMy computer, SelectAttribute. ClickAdvanced System Settings. In this caseSystem attributes, SelectAdvancedTab, clickEnvironment variable. In the user variable, double-clickPATH, InVariable valueAdd;D:\repository\CourseManagement\mobile_api_testNote that the first semicolon is used to store phpunit. phar ). To use PHPUnit at any location, you must use phpunit in the PHPUnit. phar path.

The official document does not mention that php environment variables should also be set. For example, myphp.exeInE:\software\wamp\bin\php\php5.5.12InPATHAdd;E:\software\wamp\bin\php\php5.5.12. PS: It may be because I have not installed the php ide, so I have never configured it. I was surprised to add this environment variable.

Press the shortcut keyWin + R, Enter cmd and press enter. Enter the path for storing phpunit. phar. Inputecho @php "%~dp0phpunit.phar" %* > phpunit.cmdAnd press enter. Enterphpunit --versionAnd press enter. If the output is obtainedPHPUnit x.y.z by Sebastian Bergmann and contributors.If the configuration is correct, enterexitAnd press enter ). For example:

2. use PHPUnit for testing

PHPUnit must be usedClass. Take login. php as an example.D:\repository\CourseManagement\mobile_api), Our initial version is as follows ):

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

In this way, the test is not feasible. Firstlogin.phpCreate a folder in the folderclassesAnd createclass_login.php, Content islogin.phpModified Version:

 
 
  1. Class Login {
  2. // The PHPUnit test tool requires that the default value of the variable be given here, so the default value 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, you also need to modify the originallogin.phpThe modified content is 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 file inD:\repository\CourseManagement\mobile_api_testIn this folder. Create a 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": "110", "email": "git@github.com "}';
  5. $ WorkNumber = '000000 ';
  6. $ Password = '000000 ';
  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 = '000000 ';
  15. $ Password = '000000 ';
  16. $ TableName = 'User _ teacher ';
  17. $ Lg = new Login;
  18. $ Actual = $ lg-> login ($ workNumber, $ password, $ tableName );
  19. $ This-> assertEquals ($ expected, $ actual );
  20. }
  21. }
  22. >

Run the test file

Press Win + R, enter cmd, and press enter. Enter the Directory of the test file and enterphpunit login_test.phpRun the test.

The simple test is complete.

III. exploration process

Download PHPUnit at the beginning..pharTo decompress the file. After finding a website that can decompress such files for a long time, click here ). However, it is useless ......

Follow the official documents to see if an error occurs during running:

'Php' is neither an internal or external command nor a program that can be run.
Or batch files.

Google search, Bing search, StackOverFlow search, Baidu search, and search are useless.

The main reason is that you have configured PHP environment variables by default ......

Finally, I thought about whether it was previously generated.phpunit.cmdIs there a problem? Check the content of this file. Why is PHP environment variable not set? Open cmd and enterphp --version. Get:

'Php' is neither an internal or external command nor a program that can be run.
Or batch files.

Same as the above error! This is indeed a problem. Therefore;E:\software\wamp\bin\php\php5.5.12Add to environment variable. Run Againphp --versionGet:

  
  
  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, Copyright (c) 2002-2014, by Derick Rethans

Gophpunit.cmdFolder, runphpunit --version. Get:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

Solve the problem!

After this exploration, when we encounter the problem of "cannot find xxx", we will first remember the setting of environment variables.

For example, on the afternoon of the same day, I want to use the Git feature of Visual Studio Code, but I got a prompt:

First, I installed msysgit.

The second response is: will the environment variable be not configured? Enable environment variable configuration. Thereforegit.exeAdd the path of the folder. Restart visual Studio Code to solve the problem!

Http://www.bkjia.com/PHPjc/1077040.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1077040.htmlTechArticlePHPUnit in Windows configuration and use of the tutorial because our project involves php, so the php code needs to be tested unit. After some understanding, I 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.