PHP script test

Source: Internet
Author: User
Tags functions header http request include variables php and php code php script

We often experience this by rewriting a few legacy code that has not been tested, even if the code is written with an Object-oriented object. To test this code, my advice is to break the code into chunks so it's easy to test.

However, these legacy code is not so good refactoring, for example: Before the test, you can not rewrite the code, this is to avoid affecting the original program, of course, it is not good for unit testing.

In a PHP program, a portion of the code is usually written in several index.php and script.php files, which are stored in several different folders. If you do not find their entry points, they cannot be accessed directly 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) equals the expected value. The attention here is to simulate a request, to define response and requests, not just the different content, but also their header information (header) is different.

Also, if we want to test a transaction script that operates on data, we want to make sure that it doesn't connect to the real database or the rest of the application.

In reality, no one usually takes the original PHP script to rewrite the test. Because of the fear of the code can not be restored. I recommend using a copy of the PHP script so that we can do some minor surgery on the PHP code.

How to make minimal changes to the code: Delete the Include and require statements (if they are not used), and modify the way the internal functions are invoked, for example, to write the header () as $object->header ().

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

Specific Steps

First, simulate an HTTP request and redefine the variable $_get and $_post, and modify the header of the $_server.

To obtain a request response, the body of the response can be captured by Ob_start () and Ob_get_clean (), which can collect each echo () or

Note: Output buffering supports nesting at multiple levels of PHP, so in most cases it can be captured even if the script is using the ob_* call itself.

Third, the test script should contain the internal method of the transaction script, so the method within the scope of this script can be invoked. For example:

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

2. Not the original PHP built-in functions, should be added to the object to invoke, such as: Header () written $this->header ().

Specific Code

This is the transaction script object we want to test, specifically in the script, we also need to encapsulate:

<?php

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's 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 ']);

}

Conclusions

The test copy is only temporary! It allows us to write tests that will not change. In the end, we'll refactor the PHP script that has been tested to eliminate redundant code.

When our test is complete, we can replace the contents of HandleRequest () with the real logical code. If you want to write a lot of these test scripts, you can write a generic test object to meet your testing needs.



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.