Automatic tests and PHPUnit area

Automatic tests and PHPUnit area

Introduction

The main aim of the professional php-developer is to create software in a short space of time that will satisfy the customer in full. Scripts should give expected results. The simplest example is a feedback form. On pressing the button in the case of correct filling of the fields it sends mail and writes data to the database table. If it doesn’t do so, it means that there is an error.

So, testing is an activity directed to searching differences between expected and real results. Revealing differences and incorrect behavior at the stage of development, developer decreases the probability that user can face with these problems. Programs can be tested manually as well. By the example of the feedback form it is clear that it can be tested without any problems. We have to launch the browser, find required URL, fill the fields, and press OK; then we have to enter the database and check the record. But if any script has been changed we can’t be sure that it works properly. If you don’t test it, customer will do this while handing the project or user while exploiting. To escape manual testing you can use testing program or write automatic tests. Testing is an integral part of the software development.

Automatic tests and web-programming

Workdays of the major part of developers is searching of the appropriate server for site deployment. To find the suitable hosting provider is difficult process and moving to another server can be fatal. There can appear warnings in the scripts and some programs can stop working at all.

As a result, if you have testsuite you can escape a lot of programs. You have to rewrite the scripts, launch the testsuite, look through the failed tests, find reasons of the problems, improve and launch the testsuite once again. That’s it. Automatic testing increases the quality of the code.

After that you have to organize the testing process. For example, you can provide the special directory for the test scripts, or create the program that will launch all scripts and depending on the result will show “OK” or “ERROR”.

Tests creating in the phpUnit

Let’s create the TestSuite for Message class that will format and check the message for the next sending via e-mail.

Constructor of that class has three parameters (sender, his e-mail address, message body). format_message() method formats the message before sending; is_valid() method checks whether all fields are filled and e-mail is correct.

The “heart” of the testing is PHPUnit that launches the TestSuite and returns TestResult. If you want to write simple TestSuite using phpUnit you have to:

  • Include the PHPUnit.php library
  • Create the subclass of the TestCase basic class
  • Add testing methods. In our case they are: "test_empty_input","test_email_invalid","test_valid_input". Message class methods will be called there.
  • Create PHPUnit_TestSuite class.
  • Launch the TestSuite and display the result.

Let’s create the TestSuite according to that instruction.

We’ll test the following aspects of the Message class working: its reaction to the empty message and incorrect e-mail, and compare the formatted message with the standard.

Create the message and write wrong e-mail. is_valid() class method should return false:

<?php 
        
function test_email_invalid() { 
            
$m = new Message("name""invalid""body"); 
            
/* expecting that m->is_valid() returns false */ 
            
$this->assertFalse($m->is_valid());  
        }    

Create the message, send the empty strings. is_valid() class method should return false:

<?php 
        
function test_empty_input() { 
            
$m = new Message(""""""); 
            
/* expecting that m->is_valid() returns false */ 
            
$this->assertFalse($m->is_valid());  
        }

Then create the correct message. Class should format it correctly.

<?php 
        
function test_valid_input() { 
            
$m = new Message("name""invalid@mail.com""body"); 
            
$this->assertTrue($m->is_valid()); 
            
$valid_string = <<<EOL 
from
name (invalid@mail.com

body 
EOL

            
$this->assertEquals($valid_string$m->as_string());  
        } 
    }

Then launch the TestSuite and get the following result:

<?php 
TestCase messagetest
->test_email_invalid() passed 
TestCase messagetest
->test_empty_input() passed 
TestCase messagetest
->test_valid_input() passed

All tests are successful.

message.inc.php source code

<?php 

class Message 
    var    
$email
    var    
$name
    var    
$body

    var    
$message

    function 
Message($n$from$b) { 
        
$this->email $from
        
$this->name  $n
        
$this->body  $b

        
$this->message null
    } 
    function 
is_valid() { 
        if (
strpos($this->email"@")===false) return false

        if (!
strlen($this->name)) return false
        if (!
strlen($this->body)) return false
        return 
true
    } 
    function 
as_string() { 
        if (
$this->message==null$this->format_message(); 
        return 
$this->message
    } 
    function 
format_message() { 
        
$this->message "from: ".$this->name
        
$this->message .= " (".$this->email.")\n\n"
        
$this->message .= $this->body
    } 


?>

testmessage.php source code

<?php 
    
require_once "PHPUnit.php"
    
/*  phpUnit and file with the tested class */ 
    
require_once("class_message.inc.php");     

    
/* inherit from PHPUnit_TestCase and add the test methods 
test<...> */ 
    
class MessageTest extends PHPUnit_TestCase {  
            function 
test_email_invalid() { 
            
$m = new Message("name""invalid""body"); 
            
/* expecting that m->is_valid() returns false */ 
            
$this->assertFalse($m->is_valid());  
        } 
        function 
test_empty_input() { 
            
$m = new Message(""""""); 
            
/* expecting that m->is_valid() returns false */
            
$this->assertFalse($m->is_valid());  
        } 
        function 
test_valid_input() { 
            
$m = new Message("name""invalid@mail.com""body"); 
            
$this->assertTrue($m->is_valid()); 
            
$valid_string = <<<EOL 
from
name (invalid@mail.com

body 
EOL

            
            
$this->assertEquals($valid_string$m->as_string());  
        } 
    } 

   
    
$suite = new PHPUnit_TestSuite("MessageTest");  
    
$result PHPUnit::run($suite); 
    echo 
$result->toHTML(); 
?>

 

  • Top