Tester Module Documentation
This Tester documentation is for version 0.9.2.
Overview
The Tester module allows you to easily create tests and run them in the FUEL admin. To create a test, add a test folder within your applications folder. Tester will read that folder to create it's list of tests you are able to run. It will even scan other modules for test directories to include in it's list of tests to run.
Some other important features to point out:
- If you have SQL that you want to include in your test, add it to a tests/sql folder and you can call it in your test class's setup method (see below)
- Test classes should always end with the suffix _test (e.g. my_app_test.php)
- Test class methods should always begin with the prefix test_
- Test database information can be set in the config/tester.php file
- The constant TESTING is created when running a test so you can use this in your application for test specific code
Configuration
- dsn - the database connection information (if it is different then your main FUEL install database)
- db_name - the name of your test database
- session_cookiejar_file - the cookie jar file path used for CURL sessions
- valid_testing_server_names - the server names that are valid for running tests. Default value is array('localhost', '192\.168\.:any')
You must use a database user that has the ability to create databases since a separate database is created for testing.
Example
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class My_site_test extends Tester_base {
public function __construct()
{
parent::__construct();
}
public function setup()
{
$this->load_sql('test_generic_schema.sql');
// load a basic MY_Model to test
require_once('test_custom_records_model.php');
}
public function test_find_by_key()
{
$test_custom_records_model = new Test_custom_records_model();
// test find_by_key
$record = $test_custom_records_model->find_by_key(1);
$test = $record->full_name;
$expected = 'Darth Vader';
$this->run($test, $expected, 'find_by_key custom record object property test');
// test get_full_name() method version
$test = $record->get_full_name();
$this->run($test, $expected, 'find_one custom record object method test');
}
public function test_goto_page()
{
//http://code.google.com/p/phpquery/wiki/Manual
$post['test']= 'test';
$home = $this->load_page('home', $post);
$test = pq("#content")->size();
$expected = 1;
$this->run($test, $expected, 'Test for content node');
$test = pq("#logo")->size();
$expected = 1;
$this->run($test, $expected, 'Test for logo node');
}
}
Function Reference
run(test, expected, '[name]')
Runs a test passing the value and the expected results. The name parameter will help identify it in the results page.
setup()
Placeholder to be overwritten by child classes for test setup (like database table creation etc).
tear_down()
Is called at the end of the test and will remove any test database that has been created.
format_test_name('[name]', test, expected)
Formats the test name to include the test and expected results.
config_item('[key]')
Return a Tester specific configuration item.
db_connect('[dsn]')
Connects to the testing database. The dsn parameter is the database connection information for the test database.
db_exists()
Tests if the database for testing exists. Returns a boolean.
create_db()
Creates the test database.
remove_db()
Removes the test database.
load_sql('file', [module])
Loads the SQL from a file in the {module}/test/sql folder. The default module is tester.
Enter NULL or an empty string '' if you are loading an SQL file from your application directory.
load_page('file', [post])
Loads the results of a page and returns the contents of that page. You can optionally pass in an associative array for post values. Additionally, this function loads the pq() function to query DOM nodes. The pq() function allows you to use jQuery like syntax to query DOM nodes on your page. For more information, visit the phpQuery manual.
page_contains('match', [use_jquery])
Convenience method to test if something exists on a page. The first parameter is a string to match. The second parameter says whether to use jquery syntax to match a specific DOM node (TRUE), or to use regular expression (FALSE).