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.
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:
- < span="">
- 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 PASSW Ord = $password ");
- if (Mysqli_num_rows ($result) < 1) {
- Echo "false";
- } Else {
- $result _arr = Mysqli_fetch_assoc ($result);
- Echo Json_encode ($result _arr, json_unescaped_unicode);
- }
- }
- >
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:
- < span="">
- class Login {
- //test tool PHPUNIT requirements must be given here to the default value of the variable, so the default is empty.
- 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 have to modify the original login.php
content, modified as follows:
- < span="">
- 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 in D:\repository\CourseManagement\mobile_api_test
this folder. Create a new file ' login_test.php ' and write the following code:
- < span="">
- 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 ":" The "," E-mail ":" git@github.com "};
- $workNumber = ' 00001 ';
- $password = ' 00001 ';
- $tableName = ' user_teacher ';
- $LG = new Login;
- $actual = $lg->login ($workNumber, $password, $tableName);
- $this->assertequals ($expected, $actual);
- }
- function Testloginfail () {
- $expected = ' false ';
- $workNumber = ' 11111 ';
- $password = ' 11111 ';
- $tableName = ' user_teacher ';
- $LG = new Login;
- $actual = $lg->login ($workNumber, $password, $tableName);
- $this->assertequals ($expected, $actual);
- }
- }
- >
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 ...