[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/ -> simpletestlib.php (summary)

Utility functions to make unit testing easier. These functions, particularly the the database ones, are quick and dirty methods for getting things done in test cases. None of these methods should be used outside test code.

Author: T.J.Hunt@open.ac.uk
Copyright: © 2006 The Open University
License: http://www.gnu.org/copyleft/gpl.html GNU Public License
Version: $Id$
File Size: 391 lines (15 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 4 classes

IgnoreWhitespaceExpectation:: (4 methods):
  IgnoreWhitespaceExpectation()
  test()
  normalise()
  testMessage()

ArraysHaveSameValuesExpectation:: (4 methods):
  ArraysHaveSameValuesExpectation()
  test()
  normalise()
  testMessage()

CheckSpecifiedFieldsExpectation:: (12 methods):
  CheckSpecifiedFieldsExpectation()
  test()
  testMessage()
  load_test_table()
  load_test_data()
  make_test_tables_like_real_one()
  make_test_table_like_real_one()
  remove_test_table()
  wipe_tables()
  wipe_sequences()
  _private_has_id_column()
  _private_execute_sql()

prefix_changing_test_case:: (4 methods):
  change_prefix()
  change_prefix_back()
  setUp()
  tearDown()

Defines 4 functions

  recurseFolders()

Class: IgnoreWhitespaceExpectation  - X-Ref

An expectation for comparing strings ignoring whitespace.

IgnoreWhitespaceExpectation($content, $message = '%s')   X-Ref
No description

test($ip)   X-Ref
No description

normalise($text)   X-Ref
No description

testMessage($ip)   X-Ref
No description

Class: ArraysHaveSameValuesExpectation  - X-Ref

An Expectation that two arrays contain the same list of values.

ArraysHaveSameValuesExpectation($expected, $message = '%s')   X-Ref
No description

test($actual)   X-Ref
No description

normalise($array)   X-Ref
No description

testMessage($actual)   X-Ref
No description

Class: CheckSpecifiedFieldsExpectation  - X-Ref

An Expectation that compares to objects, and ensures that for every field in the
expected object, there is a key of the same name in the actual object, with
the same value. (The actual object may have other fields to, but we ignore them.)

CheckSpecifiedFieldsExpectation($expected, $message = '%s')   X-Ref
No description

test($actual)   X-Ref
No description

testMessage($actual)   X-Ref
No description

load_test_table($tablename, $data, $db = null, $strlen = 255)   X-Ref
Given a table name, a two-dimensional array of data, and a database connection,
creates a table in the database. The array of data should look something like this.

$testdata = array(
array('id', 'username', 'firstname', 'lastname', 'email'),
array(1,    'u1',       'user',      'one',      'u1@example.com'),
array(2,    'u2',       'user',      'two',      'u2@example.com'),
array(3,    'u3',       'user',      'three',    'u3@example.com'),
array(4,    'u4',       'user',      'four',     'u4@example.com'),
array(5,    'u5',       'user',      'five',     'u5@example.com'),
);

The first 'row' of the test data gives the column names. The type of each column
is set to either INT or VARCHAR($strlen), guessed by inspecting the first row of
data. Unless the col name is 'id' in which case the col type will be SERIAL.
The remaining 'rows' of the data array are values loaded into the table. All columns
are created with a default of 0xdefa or 'Default' as appropriate.

This function should not be used in real code. Only for testing and debugging.

param: string $tablename the name of the table to create. E.g. 'mdl_unittest_user'.
param: array $data a two-dimensional array of data, in the format described above.
param: object $db an AdoDB database connection.
param: int $strlen the width to use for string fields.

load_test_data($tablename, $data, $localdb = null)   X-Ref
Given a table name, a two-dimensional array of data, and a database connection,
adds data to the database table. The array should have the same format as for
load_test_table(), with the first 'row' giving column names.

This function should not be used in real code. Only for testing and debugging.

param: string $tablename the name of the table to populate. E.g. 'mdl_unittest_user'.
param: array $data a two-dimensional array of data, in the format described.
param: object $localdb an AdoDB database connection.

make_test_tables_like_real_one($tablenames, $realprefix, $testprefix, $db,$dropconstraints=false)   X-Ref
Make multiple tables that are the same as a real table but empty.

This function should not be used in real code. Only for testing and debugging.

param: mixed $tablename Array of strings containing the names of the table to populate (without prefix).
param: string $realprefix the prefix used for real tables. E.g. 'mdl_'.
param: string $testprefix the prefix used for test tables. E.g. 'mdl_unittest_'.
param: object $db an AdoDB database connection.

make_test_table_like_real_one($tablename, $realprefix, $testprefix, $db, $dropconstraints=false)   X-Ref
Make a test table that has all the same columns as a real moodle table,
but which is empty.

This function should not be used in real code. Only for testing and debugging.

param: string $tablename Name of the table to populate. E.g. 'user'.
param: string $realprefix the prefix used for real tables. E.g. 'mdl_'.
param: string $testprefix the prefix used for test tables. E.g. 'mdl_unittest_'.
param: object $db an AdoDB database connection.

remove_test_table($tablename, $db, $cascade = false)   X-Ref
Drops a table from the database pointed to by the database connection.
This undoes the create performed by load_test_table().

This function should not be used in real code. Only for testing and debugging.

param: string $tablename the name of the table to populate. E.g. 'mdl_unittest_user'.
param: object $db an AdoDB database connection.
param: bool $cascade If true, also drop tables that depend on this one, e.g. through

wipe_tables($prefix, $db)   X-Ref
Drops all the tables with a particular prefix from the database pointed to by the database connection.
Useful for cleaning up after a unit test run has crashed leaving the DB full of junk.

This function should not be used in real code. Only for testing and debugging.

param: string $prefix the prfix of tables to drop 'mdl_unittest_'.
param: object $db an AdoDB database connection.

wipe_sequences($prefix, $db)   X-Ref
Drops all the sequences with a particular prefix from the database pointed to by the database connection.
Useful for cleaning up after a unit test run has crashed leaving the DB full of junk.

This function should not be used in real code. Only for testing and debugging.

param: string $prefix the prfix of sequences to drop 'mdl_unittest_'.
param: object $db an AdoDB database connection.

_private_has_id_column($table, $db)   X-Ref
No description

_private_execute_sql($sql, $localdb = null)   X-Ref
No description

Class: prefix_changing_test_case  - X-Ref

Base class for testcases that want a different DB prefix.

That is, when you need to load test data into the database for
unit testing, instead of messing with the real mdl_course table,
we will temporarily change $CFG->prefix from (say) mdl_ to mdl_unittest_
and create a table called mdl_unittest_course to hold the test data.
change_prefix()   X-Ref
No description

change_prefix_back()   X-Ref
No description

setUp()   X-Ref
No description

tearDown()   X-Ref
No description

Functions
Functions that are not part of a class:

recurseFolders($path, $callback, $fileregexp = '/.*/', $exclude = false, $ignorefolders = array()   X-Ref
Recursively visit all the files in the source tree. Calls the callback
function with the pathname of each file found.

param: $path the folder to start searching from.
param: $callback the function to call with the name of each file found.
param: $fileregexp a regexp used to filter the search (optional).
param: $exclude If true, pathnames that match the regexp will be ingored. If false,
param: array $ignorefolders will not go into any of these folders (optional).



Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7