Documenation

Unit tests

Swiftlet can perform automated tests to ensure everything keeps working as expected during plugin development. Unit test can be executed by going to /unit_tests/.

Each plugin can include a number of tests, using the unit_test hook in the main plugin file.

Example

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
if ( !isset($model) ) die('Direct access to this file is not allowed');

switch ( 
$hook )
{
    case 
'info':
        
$info = array(
            
'name'       => 'example',
            
'version'    => '1.0.0',
            
'compatible' => array('from' => '1.2.0''to' => '1.2.*'),
            
'hooks'      => array('unit_test' => 1)
            );

        break;
    case 
'unit_test':
        
// Form values to be send
        
$post = array(
            
'foo'         => 'bar',
            
'form-submit' => 'Submit',
            
'auth_token'  => $model->authToken
            
);

        
// Simulate submitting a form with a POST request        
        
$r post_request('http://' $_SERVER['SERVER_NAME'] . $contr->absPath '/example/'$post);

        
// Check if the form was handled correctly and the value has been saved
        
$model->db->sql('
            SELECT
                *
            FROM `' 
$model->db->prefix 'example`
            WHERE
                `foo` = "bar"
            LIMIT 1
            ;'
FALSE);

        
// Return the test result
        
$params[] = array(
            
'test' => 'Saving a value in <code>/example/</code>.',
            
'pass' => ( bool ) $model->db->result
            
);

        break;
}
?>
This test simulates a form being submitted on the page /example/. If the page handled the request correctly the value "bar" was saved, which can be confirmed by checking the database.