-
Notifications
You must be signed in to change notification settings - Fork 10
Unit Tests
This is a job for your command-line/shell.
Unit tests use several PHP extensions that phpBB itself does not use. These need to be installed and enabled for unit tests to work.
Unit tests themselves need:
- ctype
- simplexml
For database tests you need to have pdo and pdo drivers for each of the databases you want to test with (e.g. pdo_pgsql for postgres).
phpunit needs:
- ctype
- dom
You need to install PHPUnit to your server: http://www.phpunit.de/ In order to install PHPUnit correctly, you need to run the following commands:
pear channel-update pear.php.net
pear install PEAR-1.9.1
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear install --alldeps phpunit/PHPUnit
Depending on what versions of phpunit and pear you have installed or had installed earlier, phpunit may silently break itself. If you get errors about missing files when running tests, uninstall phpunit and pear completely and then reinstall the latest versions of pear and phpunit.
You need to switch into the root directory of the git-repo.
And then simply run
Linux:phpunit
Windows:YOUR_DIRECTORY_WHERE_THE_FILE_IS/phpunit.bat
(Exp. C:/xampp/php/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.
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!
<?php
require_once dirname(__FILE__).'/../phpBB/includes/functions.php';
class blog_demo_test extends PHPUnit_Framework_TestCase
{
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($expected, phpbb_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.
<?php
class blog_db_demo_test extends PHPUnit_Extensions_Database_TestCase
{
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:
- All data from the database is truncated first.
- The data from the getDataSet function is loaded into the database. No data from any other test is available!
<?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>