Functions of testing in PHP-projects

Improvement of a code should not affect on his  initial (after the lead{carried out} completions) functionality. At least, after processing the application should not contain more mistakes on unit of lines of a code, than prior to the beginning of this process. To reach{achieve} it it is possible with the help of the procedures introduced into the application on check of correctness of job of functions of the program. If to accept contents of function for a black box (having the functionality determined by you) on reaction of function to a set of entrance values it is possible to judge correctness of its{her} job. On this principle it is possible to construct simple, but the function chart of testing of a code suffices.


Let's disassemble the elementary case of the organization of procedure of testing of quality of a code by the example of introduction of testing of serviceability of only one function, carrying out, for example, simple transformations of the text... We Shall assume, in your program a certain function which increases length of line performance of an integer up to the specified size with the help of addition at the left symbols "0" was necessary.


Here is how function which will check subsequently a class into which she will enter looks:

<? 

function addspace ($int _, $ len) 

$i=0; 

$str _ = trim ($int _); 

while (strlen ($str _) <$ len) 

    $str _ = "0." $str _; 

    if ($i ++> $len) break; 

return $str _; 

?>



This code is meaningly written a little bit incorrectly that you could track as methods of catching of such situations in practice function. The incorrectness will consist in incorrect processing negative values.

If at a stage of creation of new function at once to create a code checking its{her} serviceability by the analysis of conformity of files of entrance values with the days off at the further completion of this function it is possible to not worry about its{her} serviceability - to start this testing code and then it will be possible to draw a conclusion enough, that function works correctly or does not work. Further the simple example of such testing code for our function of text processing will be shown.


The basic problem at construction of the mechanism of check of functions is a realization of development of a code for the control of the functionality necessary to you. The greatest complexity in the organization of process of manufacture of software product thus will be to create testing code in process of addition of new functions in your application.


Good form will be creation " samotestirujuhhegosja a code " since the beginning of development of a class (function) on PHP. After entering some changes into a code of function it is enough to developer to start testing code which will give out results of comparison of the reference data with current. It is possible to argue for a long time how it is better to make out results of such testing, but, in that case if you had not time to introduce yet any methods of testing in your technological process of development of the software, any variant will be good. For simplicity of realization the variant with a broad gully - file in which results of testing of functions will be brought in the best way will approach. Under condition of an arrangement of your program modules on a platform *nix it is possible to use safely also opportunities of a demon syslogd, this class built - in operational systems.

Actually the variant of accommodation of results of testing has the greater value for good safety operational system, than for results of the testing. In case of a conclusion of results of testing in the user application you can directly open the data about accommodation of your scripts on the server that is invalid for safety reasons. In view of it the most simple variant of function logirovanija mistakes (accumulation of debugging messages) can look as follows:

<? 

class text_process 

... 

function err_log ($str) 

    $fp=fopen ("./text_process.log", " a + "); 

    fwrite ($fp, date (" Y-m-d H:i:s ") ". error: " .trim ($str). "n"); 

    fclose ($fp); 

... 

?>



To simplify process the testings necessary to function it is possible to bear{take out} in a class. He will contain except for our function also a certain code for its{her} testing. In view of above mentioned reasonings function of the class necessary for the organization of testing, can look as follows:

<? 

class text_process 

... 

function test () 

    if (($res = $ this-> addspace (0,3))! == "000") 

      $this-> err_log (" add_space (0,3) ()! == "000" is: $res "); 

    if (($res = $ this-> addspace (1,3))! == "001") 

      $this-> err_log (" add_space (1,3) ()! == "001" is: $res "); 

    if (($res = $ this-> addspace (500,1))! == "500") 

      $this-> err_log (" add_space (500,1) ()! == "500" is: $res "); 

... 

?>



Certainly, still it is not enough creation of a testing code for the organization of procedure of the control over quality of functions. It is necessary to organize constant start of these procedures. In a case with webs - scripts one of the best ways of a call of functions of testing will be start of such code from the designer of a class. Thus the developer should think of ways of reduction of "overhead charge" which are inevitable by such call of the designer. For this purpose it is necessary to reduce quantity{amount} of start of testings of a code, for example by performance of periodic testing. Thus quite adequate measure of restriction of additional expenses for a call of a testing code can be creation of a local variable of a class, the manager of start of the necessary procedures. The sense will be what to carry out necessary procedures follows only at installation of the certain value of this variable. The designer of a class containing start of testing in the designer here is how can look:

<? 

class text_process 

... 

var $testing; 

function text_process ($test=FALSE) 

    if ($test === TRUE) 

      $this-> test (); 

... 

?>



For performance of procedural testing it is possible to execute consistently creation of all classes of your project with the help of specially created service code. A script created for these purposes, it will be possible to start, for example, after performance of the next completion of program system and the termination{ending} of catching of syntactic mistakes.

It is necessary to note as well that fact, that introduction of methods of testing in structures of your programs will lead to to some complication of a code. Besides creation of new opportunities results in inevitable complication of testing functions.


The example resulted in clause{article} is trivial. Real applications will contain more complex  functions and sets of the entrance data. But one is doubtless - application of such methods of testing will help you to make process of development of the software more protected from mistakes. And it only will increase a level of trust to your applications on the part of users.


During development of the module function has changed. By results of testing I have defined{determined} character of mistakes and a place of their occurrence. As already it has been noticed earlier, function incorrectly fulfilled negative entrance parameters. This mistake became visible for a design stage due to some change in testing function:

<? 

class text_process 

... 

function test ($name) 

... 

    if (($res = $ this-> addspace (-5,6))! == "-00005") 

      $this-> err_log (" add_space (-5,6) ()! == "-00005 " is: $res "); 

... 

?>



The specified method of testing of the software in him is only strategic course which will not give the developer of momentary benefit and can tighten{delay} development of the project. But in due course during long development of an industrial variant (successfully functioning sufficient time in the target environment of the application) you thus can receive significant economy of means at stages of debugging of the application.