PHP script test

Source: Internet
Author: User
We often encounter this situation: rewrite and test some legacy code that has not passed any tests, and even use object-oriented code. To test such code, I suggest breaking the code into blocks so that it is easy to test. However, these legacy... "> <LINKhref ="

We often encounter this situation: rewrite and test some legacy code that has not passed any tests, and even use object-oriented code. To test such code, I suggest breaking the code into blocks so that it is easy to test.

However, these legacy codes are not so well reconstructed. for example, before testing, you cannot rewrite the code to avoid affecting the original program and, of course, it is difficult to perform unit testing.

In PHP programs, some code is usually written in several index. php and script. PHP files. These. php files are stored in several different folders. If they cannot be found, they cannot be directly accessed by the Web server.

Test copy

To test a PHP script, we need to simulate an HTTP request and check whether the returned response (response) is equal to the expected value. It is worth noting that simulating a request requires defining response and request. this is not only the difference in content, but also the difference in their header information.

In addition, if we want to test a transaction script for data operations, we need to ensure that it is not allowed to connect to the rest of the real database or application.

In reality, no one will directly use the original PHP script for rewrite testing. For fear of making the code unrecoverable. I suggest using a copy of the PHP script so that we can perform minor operations on the PHP code.

How to minimize code modification: delete include and require statements (if they are not used) and modify the calling method of internal functions, such () write it as $ object-> header ().

Finally, let's test the transaction script. After testing, we can extract them from the copy script and put them into the new script file.


1. simulate an HTTP request and redefine the variables $ _ GET and $ _ POST, and modify the header of $ _ SERVER.

2. get the request response. the response body can be captured through ob_start () and ob_get_clean (). it can collect every response using echo () or

Note: the output buffer supports multiple levels of nesting in PHP, so it can be captured in most cases, even if the script is calling itself using ob.

3. the test script should contain internal methods of the transaction script, so all methods within the script scope can be called. For example:

1. variables required by the script can be defined as local variables encapsulated, for example, $ connection as a database connection.

2. it is not a built-in function of the original PHP, and an object should be added for calling. for example, header () is written as $ this-> header ().


This is the transaction script object we want to test. Specifically, we need to encapsulate it in the script:



Class ForumPosting


Private $ headers = array ();

Public function handleRequest ($ postRequest)


$ _ POST = $ postRequest;

$ Connection = $ this-> getAConnection ();

Ob_start ();

Include 'forum/post_new_copy.php ';

$ Content = ob_get_clean ();

Return array (

'Content' => $ content,

'Headers' => $ this-> headers



Private function header ($ headerLine)


$ This-> headers [] = $ headerLine;





Here is our test code:


Public function testANewPostIsCreated ()


$ Action = new ForumPosting ();

$ Response = $ action-> handleRequest (array (

'Id _ thread' => 42,

'Text' => 'Hello, world ',



$ This-> assertEquals ('...', $ response ['content']);

$ This-> assertContains ('content-type: text/html', $ response ['headers']);




The test copy is temporary! It makes the tests we have written remain unchanged. In the end, we need to refactor the tested PHP script to eliminate redundant code.

After the test is complete, we can replace the content of handleRequest () with the real logic code. If you want to write many such test scripts, you can write a common test object to meet your testing needs.

Related Article

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