phpBB

Development Wiki

Unit Tests

From phpBB Development Wiki

Revision as of 20:48, 18 June 2011 by Naderman (Talk | contribs)

Unit Tests

Running tests on your system

This is a job for your command-line/shell. First, you need to install PHPUnit to your server: http://www.phpunit.de/

Once that is done: You need to switch into the tests/-directory inside the git-repo. And then simply run

Linux:
phpunit
Windows:
YOUR_DIRECTORY_WHERE_THE_FILE_IS/phpunit.bat

Afterwards you should see something like:

PHPUnit 3.4.11 by Sebastian Bergmann.

............................................................  60 / 688
............................................................ 120 / 688
IIIIIIIIIII................................................. 180 / 688
............................................................ 240 / 688
............................................................ 300 / 688
............................................................ 360 / 688
............................................................ 420 / 688
............................................................ 480 / 688
............................................................ 540 / 688
............................................................ 600 / 688
............................................................ 660 / 688
............................

Time: 5 seconds, Memory: 13.00Mb

OK, but incomplete or skipped tests!
Tests: 688, Assertions: 878, Incomplete: 11.

If there are no errors, your test was successful. Otherwise either a test was wrong, or the functionality itself is wrong. Then you should go and fix it.

Writing your own Tests

Add your test to the system

git-fork/tests/all_tests.php

// exclude the test directory from code coverage reports
PHPUnit_Util_Filter::addDirectoryToFilter('./');

before add:

require_once 'demo/all_tests.php';


Then you need to add your test to the phpbb-TestSuite aswell:

return $suite;

before add:

$suite->addTest(phpbb_demo_all_tests::suite());

demo/all_tests.php file

<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('PHPUnit_MAIN_METHOD'))
{
    
define('PHPUnit_MAIN_METHOD''phpbb_demo_all_tests::main');
}

require_once 
'test_framework/framework.php';
require_once 
'PHPUnit/TextUI/TestRunner.php';

require_once 
'demo/demo_suite.php';
require_once 
'demo/db_demo_suite.php';

class 
phpbb_demo_all_tests
{
    public static function 
main()
    {
        
PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function 
suite()
    {
        
// Short description of your test.
        
$suite = new PHPUnit_Framework_TestSuite('phpBB Your First phpBB-Test');

        
$suite->addTestSuite('phpbb_demo_suite');
        
$suite->addTestSuite('phpbb_db_demo_suite');
        
// You can also split your Test into mutliple files to keep it clear.
        // You should also split it up, if some tests need the database and some do not.

        
return $suite;
    }
}

if (
PHPUnit_MAIN_METHOD == 'phpbb_demo_all_tests::main')
{
    
phpbb_demo_all_tests::main();
}

demo/demo_test.php

There are two cases of tests: one that needs a database and one that doesn't. If a test does not need a database then don't use it!

without database

<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once 'test_framework/framework.php';

require_once 
'../phpBB/includes/functions.php';

class 
phpbb_demo_test extends phpbb_test_case
{
    public static function 
phpbb_email_hash_data()
    {
        return array(
            array(
'nickvergessen@gmx.de'126830126620),
            array(
''0),
        );
    }

    
/**
    * @dataProvider phpbb_email_hash_data
    */
    
public function test_phpbb_email_hash($email$expected)
    {
        
$this->assertEquals($expectedphpbb_email_hash($email));
    }
}

You may have noted, that in the test-function there is actually no data. There's a dataProvider-function stated in the comment, which is used for the test. The test is then run with every array in the dataProvider array, having the values as parameters for the test-function-call.

with database

<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once 'test_framework/framework.php';

class 
phpbb_db_demo_test extends phpbb_database_test_case
{
    public function 
getDataSet()
    {
        return 
$this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml');
    }

    public static function 
fetchrow_data()
    {
        return array(
            array(
'', array(array('username_clean' => 'barfoo'),
                array(
'username_clean' => 'foobar'),
                array(
'username_clean' => 'bertie'))),
            array(
'user_id = 2', array(array('username_clean' => 'foobar'))),
            array(
"username_clean = 'bertie'", array(array('username_clean' => 'bertie'))),
            array(
"username_clean = 'phpBB'", array()),
        );
    }

    
/**
    * @dataProvider fetchrow_data
    */
    
public function test_fetchrow($where$expected)
    {
        
// The function from phpbb_test_case_helpers returns a new db for every test.
        
$db $this->new_dbal();

        
$result $db->sql_query('SELECT username_clean
            FROM phpbb_users
            ' 
. (($where) ? ' WHERE ' $where '') . '
            ORDER BY user_id ASC'
);

        
$ary = array();
        while (
$row $db->sql_fetchrow($result))
        {
            
$ary[] = $row;
        }
        
$db->sql_freeresult($result);

        
$this->assertEquals($expected$ary);
    }
}

Most important to know for db-tests is:

  1. All data from the database is truncated first.
  2. The data from the getDataSet function is loaded into the database. No data from any other test is available!

Code of a DB-DataSet

<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
	<table name="table_name">
		<column>column_name_1</column>
		<column>column_name_2</column>
		<row>
			<value>value for column 1</value>
			<value>value for column 2</value>
		</row>
		<row>
			<value>another row, value for column 1</value>
			<value>another row, value for column 2</value>
		</row>
	</table>
</dataset>