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.
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.phar
File, 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_test
Note 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.exe
InE:\software\wamp\bin\php\php5.5.12
InPATHAdd;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.cmd
And press enter. Enterphpunit --version
And press enter. If the output is obtainedPHPUnit x.y.z by Sebastian Bergmann and contributors.
If the configuration is correct, enterexit
And 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 ):
-
-
- error_reporting(0);
-
- $workNumber = $_POST["login-user"];
- $password = $_POST["login-password"];
- $tableName = $_POST["ident"];
-
- $con = mysqli_connect("localhost", "root", "", "teacher_class_system");
- if (!$con) {
- die('Could not connect: ' . mysql_error());
- } else {
- mysqli_query($con, "SET NAMES utf8");
-
- $result = mysqli_query($con, "SELECT * FROM $tableName where workNumber = $workNumber and password = $password");
- if (mysqli_num_rows($result) < 1) {
- echo "false";
- } else {
- $result_arr = mysqli_fetch_assoc($result);
- echo json_encode($result_arr, JSON_UNESCAPED_UNICODE);
- }
- }
-
- >
In this way, the test is not feasible. Firstlogin.php
Create a folder in the folderclasses
And createclass_login.php
, Content islogin.php
Modified Version:
-
-
- Class Login {
- // The PHPUnit test tool requires that the default value of the variable be given here, so the default value is null.
- Public function login ($ workNumber = "", $ password = "", $ tableName = ""){
- $ Con = mysqli_connect ("localhost", "root", "", "teacher_class_system ");
- If (! $ Con ){
- Die ('could not connect: '. mysqli_error ());
- } Else {
- Mysqli_query ($ con, "set names utf8 ");
-
- $ Result = mysqli_query ($ con, "SELECT * FROM $ tableName where workNumber = $ workNumber and password = $ password ");
- If (! $ Result | mysqli_num_rows ($ result) = 0 ){
- Return "false ";
- } Else {
- $ Result_arr = mysqli_fetch_assoc ($ result );
- Return json_encode ($ result_arr, JSON_UNESCAPED_UNICODE );
- }
- }
- }
- }
-
- >
In addition, you also need to modify the originallogin.php
The modified content is as follows:
-
- error_reporting(0);
-
- require_once './classes/class_login.php';
-
- $workNumber = $_POST["login-user"];
- $password = $_POST["login-password"];
- $tableName = $_POST["ident"];
-
- $log = new Login;
- $response = $log->login($workNumber,$password,$tableName);
-
- if($response != "false") {
- session_start();
- $_SESSION['id']=$tableName;
- }
-
- echo $response;
-
- >
Start writing test files
I put the test file inD:\repository\CourseManagement\mobile_api_test
In this folder. Create a file 'login _ test. php' and write the following code:
-
- Require_once dirname (_ FILE _). '/../mobile_api/classes/class_login.php ';
-
- Class LoginTest extends PHPUnit_Framework_TestCase {
- Public function testLoginSuccess (){
- $ Expected = '{"workNumber": "00001", "password": "00001", "name": "watermelon", "sex": "male ", "birthday": "20151201", "department": "computer", "telephone": "110", "email": "git@github.com "}';
-
- $ WorkNumber = '000000 ';
- $ Password = '000000 ';
- $ TableName = 'User _ teacher ';
- $ Lg = new Login;
- $ Actual = $ lg-> login ($ workNumber, $ password, $ tableName );
-
- $ This-> assertEquals ($ expected, $ actual );
- }
-
- Function testLoginFail (){
- $ Expected = 'false ';
-
- $ WorkNumber = '000000 ';
- $ Password = '000000 ';
- $ TableName = 'User _ teacher ';
-
- $ Lg = new Login;
- $ Actual = $ lg-> login ($ workNumber, $ password, $ tableName );
- $ This-> assertEquals ($ expected, $ actual );
- }
- }
-
- >
Run the test file
Press Win + R, enter cmd, and press enter. Enter the Directory of the test file and enterphpunit login_test.php
Run the test.
The simple test is complete.
III. exploration process
Download PHPUnit at the beginning..phar
To 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.cmd
Is 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.12
Add to environment variable. Run Againphp --version
Get:
- PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58)
- Copyright (c) 1997-2014 The PHP Group
- Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
- with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
Gophpunit.cmd
Folder, 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.exe
Add 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...