phpBB

Development Wiki

Difference between revisions of "Writing Unit Tests"

From phpBB Development Wiki

(Split from Unit_Tests)
 
Line 1: Line 1:
 
== Writing your own Tests ==
 
== Writing your own Tests ==
  
=== demo/all_tests.php file ===
+
=== tests/demo/demo_test.php ===
<php><?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();
+
}
+
</php>
+
=== demo/demo_test.php ===
+
 
There are two cases of tests: one that needs a database and one that doesn't.
 
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!'''
 
'''If a test does not need a database then don't use it!'''
Line 62: Line 15:
 
*/
 
*/
  
require_once 'test_framework/framework.php';
+
require_once dirname(__FILE__).'/../phpBB/includes/functions.php';
  
require_once '../phpBB/includes/functions.php';
+
class phpbb_demo_demo_test extends phpbb_test_case
 
+
class phpbb_demo_test extends phpbb_test_case
+
 
{
 
{
 
public static function phpbb_email_hash_data()
 
public static function phpbb_email_hash_data()
Line 99: Line 50:
 
*/
 
*/
  
require_once 'test_framework/framework.php';
+
class phpbb_demo_db_demo_test extends phpbb_database_test_case
 
+
class phpbb_db_demo_test extends phpbb_database_test_case
+
 
{
 
{
 
public function getDataSet()
 
public function getDataSet()

Revision as of 16:00, 14 October 2011

Writing your own Tests

tests/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 dirname(__FILE__).'/../phpBB/includes/functions.php';

class 
phpbb_demo_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
*
*/

class phpbb_demo_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>