Source for file ActiveRecordTest.php
Documentation is available at ActiveRecordTest.php
* File for the ActiveRecordTest class
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright (c) Walter O. Haas 2006
* @version $Id: ActiveRecordTest.php 208 2006-05-28 17:59:55Z john $
* @author Walt Haas <haas@xmission.com>
echo "testing ActiveRecord\n";
require_once 'testenv.php';
// We need to load a mock DB class to test ActiveRecord.
// Change the include path to put the mockDB/ directory first, so
// that when ActiveRecord loads it will pick up the mock class.
require_once "active_record.php";
// Call ActiveRecordTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
define("PHPUnit2_MAIN_METHOD", "ActiveRecordTest::main");
require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";
* Require classes that are too trivial to bother making mocks
require_once 'trax_exceptions.php';
require_once 'inflector.php';
// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";
// Set Trax operating mode
define("TRAX_ENV", "development");
* Regression tester for the ActiveRecord class
* This class is used only in regression testing of the ActiveRecord
* class, but you might find some useful examples by reading it.
// Function to validate prefix attribute
if ($this->prefix == '') {
return array(false, "prefix empty");
// Create another copy of this class
return $this->create($attributes);
// Data to be returned by fetchMode
private $data = array(array('id' => '17',
'first_name' => 'Eileen',
* Test class for {@link ActiveRecord}
* Runs the test methods of this class.
public static function main() {
require_once "PHPUnit2/TextUI/TestRunner.php";
$suite = new PHPUnit2_Framework_TestSuite("ActiveRecordTest");
$result = PHPUnit2_TextUI_TestRunner::run($suite);
* Set the environment ActiveRecord expects
protected function setUp() {
// Force constructor to get a connection
Trax::$active_record_connections = array();
// Set up information that normally comes from database.ini
Trax::$database_settings[TRAX_ENV]
= array('phptype' => 'mysql',
'database' => 'database_development',
'hostspec' => 'localhost',
* This method is called after a test is executed.
$this->assertEquals(get_class($p), 'PersonName');
$this->assertEquals($p->table_name, 'person_names');
$this->assertTrue(Trax::$active_record_connections[TRAX_ENV]->options['persistent']);
// We don't completely check content_columns
$this->assertTrue(is_array($p->content_columns));
$this->assertEquals(count($p->content_columns),6);
// There are a lot of notice level error messages in normal
// operation. We know about them and don't want to confuse
$p = new PersonName(array('id' => '17', 'first_name' => 'Boris',
'last_name' => 'Tudeth'));
$this->assertEquals($p->first_name,'Boris');
$this->assertEquals($p->last_name,'Tudeth');
* Test the get_attributes() method
// Constructor initializes all attributes to null
// There are a lot of notice level error messages in normal
// operation. We know about them and don't want to confuse
$attrs = $p->get_attributes();
$this->assertEquals($attrs,array('id' => null,
// Assign some attribute values
// This shouldn't produce notice level messages
// get_attributes() should return the same values
$attrs = $p->get_attributes();
$this->assertEquals($attrs,array('id' => 17,
* Test the update_attributes() method
* @todo Figure out the datetime thing and how to test it
$p->update_attributes(array('id' => 17,
$attrs = $p->get_attributes();
$this->assertEquals($attrs,array('id' => 17,
// Remove the following line when you complete this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the quoted_attributes() method
* @todo Figure out how to test timestamp updating
// Constructor initializes all attributes to null.
// quoted_attributes() returns null as null string
$attrs = $p->quoted_attributes();
$this->assertEquals($attrs,array('id' => "''",
// Assign some attribute values
$p->first_name = 'Nobody';
$p->last_name = 'O\'Reilly';
$p->suffix = 'Back\\slash';
// Get attributes with quotes
$attrs = $p->quoted_attributes();
$this->assertEquals($attrs,array('id' => "'17'",
'prefix' => "'\\\"Dr.\\\"'",
'first_name' => "'Nobody'",
'last_name' => "'O\'Reilly'",
'suffix' => "'Back\\\\slash'"));
// Test the optional argument
$attrs = $p->quoted_attributes(
'first_name' => 'Nobody',
'last_name' => 'O\'Reilly',
'suffix' => 'Back\\slash'));
$this->assertEquals($attrs,array('id' => "'17'",
'prefix' => "'\\\"Dr.\\\"'",
'first_name' => "'Nobody'",
'last_name' => "'O\'Reilly'",
'suffix' => "'Back\\\\slash'"));
* Test the validate_model_attributes() method
$p->update_attributes(array('id' => 17,
// With failing validation, should return false and error msg
$result = $p->validate_model_attributes();
$this->assertFalse($result);
$this->assertEquals(count($p->errors),1);
$this->assertEquals($p->errors['prefix'], 'prefix empty');
* Test the query() method
// Test normal case: send query, get result
Trax::$active_record_connections[TRAX_ENV]->expect_query('foo','bar');
$result = $p->query('foo');
$this->assertEquals($result,'bar');
* Test the get_insert_id() method
Trax::$active_record_connections[TRAX_ENV]->expect_query("SELECT LAST_INSERT_ID();",
$result = & $p->get_insert_id();
$this->assertEquals($result,'17');
* Test the is_error() method
// Create a new harmless object, test it's not an error
$obj = new PHPUnit2_Framework_Assert;
$this->assertFalse($p->is_error($obj));
// Create a PHP 4 error, test it is detected
$obj = new PEAR_Error('testing');
$this->assertTrue($p->is_error($obj));
// Create a DB error, test it is detected
$this->assertTrue($p->is_error($obj));
* Test the get_primary_key_conditions() method
// Default is primary key is 'id', no value
$result = $p->get_primary_key_conditions();
$this->assertEquals($result,"id = ''");
// Now give the primary key a value
$result = $p->get_primary_key_conditions();
$this->assertEquals($result,"id = '11'");
// Try a different column as primary key
$p->primary_keys= array('last_name');
$result = $p->get_primary_key_conditions();
$this->assertEquals($result,"last_name = ''");
$result = $p->get_primary_key_conditions();
$this->assertEquals($result,"last_name = 'Smith'");
// Try two columns as primary key
$p->primary_keys= array('id', 'last_name');
$result = $p->get_primary_key_conditions();
$this->assertEquals($result,"id = '11' AND last_name = 'Smith'");
* Test the get_updates_sql() method
$result = $p->get_updates_sql();
$this->assertEquals($result,
"prefix = 'Dr.', first_name = 'Anon',"
. " mi = 'E', last_name = 'Moose', suffix = 'Ph.D.'");
// Assign some attribute values that need to be quoted
$p->first_name = 'Nobody';
$p->last_name = 'O\'Reilly';
$p->suffix = 'Back\\slash';
$result = $p->get_updates_sql();
$this->assertEquals($result,
"prefix = '\\\"Dr.\\\"', first_name = 'Nobody',"
. " mi = 'X', last_name = 'O\'Reilly', suffix = 'Back\\\\slash'");
* @todo Write test of save() of existing row
// A valid new row should be inserted
Trax::$active_record_connections[TRAX_ENV]->expect_queries(array(
array('query' => "INSERT INTO person_names"
. " (id, prefix, first_name, mi, last_name, suffix)"
. " VALUES ('', 'Dr.', 'Anon', 'E', 'Moose', 'Ph.D.')",
array('query' => "SELECT LAST_INSERT_ID();",
$result = $p->save(array('prefix' => 'Dr.',
$this->assertTrue($result);
// Verify DB received all expected queries
Trax::$active_record_connections[TRAX_ENV]->tally_queries();
// An invalid row should fail immediately
$result = $p->save(array('first_name' => 'Anon',
$this->assertFalse($result);
$this->assertEquals(count($p->errors),1);
$this->assertEquals($p->errors['prefix'], 'prefix empty');
// An invalid new row with validation disabled should be inserted
Trax::$active_record_connections[TRAX_ENV]->expect_queries(array(
array('query' => "INSERT INTO person_names"
. " (id, prefix, first_name, mi, last_name, suffix)"
. " VALUES ('', '', 'Anon', 'E', 'Moose', 'Ph.D.')",
array('query' => "SELECT LAST_INSERT_ID();",
$result = $p->save(array('first_name' => 'Anon',
'suffix' => 'Ph.D.'),true);
$this->assertTrue($result);
// Verify DB received all expected queries
Trax::$active_record_connections[TRAX_ENV]->tally_queries();
// Remove the following line when you complete this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the add_error() method
$this->assertTrue(is_array($p->errors));
$this->assertEquals(count($p->errors),0);
$p->add_error('mumble is scrogged','mumble');
$this->assertEquals($p->errors,
array('mumble' => 'mumble is scrogged'));
$p->add_error('veeblefitzer foobar');
$this->assertEquals($p->errors,
array('mumble' => 'mumble is scrogged',
'0' => 'veeblefitzer foobar'));
* Test the find_all() method
* @todo Tests for limit, joins parameters
// Test return of the entire table
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names ",
$result = $p->find_all();
$this->assertEquals(count($result),2);
$this->assertEquals(get_class($result[17]),'PersonName');
$this->assertEquals($result[17]->first_name,'Ben');
$this->assertEquals(get_class($result[23]),'PersonName');
$this->assertEquals($result[23]->first_name,'Eileen');
// Conditions including "SELECT" should pass thru unedited
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT mumble,foo FROM person_names",
$result = $p->find_all("SELECT mumble,foo FROM person_names");
// Conditions without "SELECT" should appear in WHERE clause
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE last_name = 'Dover' ",
$result = $p->find_all("last_name = 'Dover'");
// Orderings should appear in ORDER BY clause
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names ORDER BY last_name ",
$result = $p->find_all(null, "last_name");
//Trax::$active_record_connections[TRAX_ENV]->expect_query(
// "SELECT * FROM person_names WHERE last_name = 'Dover' "
// . "ORDER BY last_name ",
// new DB_find_all_result);
//$result = $p->find_all("last_name = 'Dover'", "last_name",
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the find_first() method
* @todo Tests for joins parameter
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE last_name = 'Dover' ",
$result = $p->find_first("last_name = 'Dover'");
$this->assertEquals(get_class($result),'PersonName');
$this->assertEquals($result->id,'17');
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* @todo Tests for limit, joins parameters
// Find by a single id value
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE id='17' ",
$this->assertEquals(get_class($result),'PersonName');
$this->assertEquals($result->id,'17');
// Find by an array of id values
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE id IN(17,23) ",
$result = $p->find(array(17,23));
$this->assertEquals(count($result),2);
$this->assertEquals(get_class($result[17]),'PersonName');
$this->assertEquals($result[17]->first_name,'Ben');
$this->assertEquals(get_class($result[23]),'PersonName');
$this->assertEquals($result[23]->first_name,'Eileen');
// Find by WHERE clause expression
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE last_name='Dover' ",
$result = $p->find("last_name='Dover'");
// First matching row should come back
$this->assertEquals(get_class($result),'PersonName');
$this->assertEquals($result->id,'17');
// Remove the following line when you complete this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_create() method
* @todo Implement testAfter_create()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_delete() method
* @todo Implement testAfter_delete()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_save() method
* @todo Implement testAfter_save()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_update() method
* @todo Implement testAfter_update()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_validation() method
* @todo Implement testAfter_validation()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_validation_on_create() method
* @todo Implement testAfter_validation_on_create()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the after_validation_on_update() method
* @todo Implement testAfter_validation_on_update()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the avg_all() method
* @todo Implement testAvg_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_create() method
* @todo Implement testBefore_create()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_delete() method
* @todo Implement testBefore_delete()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_save() method
* @todo Implement testBefore_save()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_update() method
* @todo Implement testBefore_update()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_validation() method
* @todo Implement testBefore_validation()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_validation_on_create() method
* @todo Implement testBefore_validation_on_create()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the before_validation_on_update() method
* @todo Implement testBefore_validation_on_update()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the begin() method
* @todo Implement testBegin()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the column_attribute_exists() method
* @todo Implement testColumn_attribute_exists()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the column_for_attribute() method
* @todo Implement testColumn_for_attribute()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the commit() method
* @todo Implement testCommit()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the count_all() method
* @todo Implement testCount_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the create() method
* @todo Implement testCreate()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the delete() method
* @todo Implement testDelete()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the delete_all() method
* @todo Implement testDelete_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the establish_connection() method
* @todo Implement testEstablish_connection()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the find_by_*() and find_all_by_*() methods
// Test find_by_first_name()
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE first_name='Ben' ",
$result = $p->find_by_first_name('Ben');
// Test find_by_first_name_and_last_name()
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names"
. " WHERE first_name='Ben' AND last_name='Dover' ",
$result = $p->find_by_first_name_and_last_name('Ben','Dover');
// Test find_all_by_last_name()
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names WHERE last_name='Dover' ",
$result = $p->find_all_by_last_name('Dover');
// Test find_all_by_first_name_and_last_name()
Trax::$active_record_connections[TRAX_ENV]->expect_query(
"SELECT * FROM person_names"
. " WHERE first_name='Ben' AND last_name='Dover' ",
$result = $p->find_all_by_first_name_and_last_name('Ben','Dover');
* Test the find_by_sql() method
* @todo Implement testFind_by_sql()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the get_association() method
* @todo Implement testGet_association()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the get_errors() method
* @todo Implement testGet_errors()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the get_errors_as_string() method
* @todo Implement testGet_errors_as_string()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the is_new_record() method
* @todo Implement testIs_new_record()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the limit_select() method
* @todo Implement testLimit_select()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* @todo Implement testLoad()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the log_query() method
* @todo Implement testLog_query()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the max_all() method
* @todo Implement testMax_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the min_all() method
* @todo Implement testMin_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the page_list() method
* @todo Implement testPage_list()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the raise() method
* @todo Implement testRaise()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the reload() method
* @todo Implement testReload()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the rollback() method
* @todo Implement testRollback()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the save_without_validation() method
* @todo Implement testSave_without_validation()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* @todo Implement testSend()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the set_content_columns() method
* @todo Implement testSet_content_columns()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the set_table_name_using_class_name() method
* @todo Implement testSet_table_name_using_class_name()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the sum_all() method
* @todo Implement testSum_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the update() method
* @todo Implement testUpdate()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the update_all() method
* @todo Implement testUpdate_all()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the valid() method
* @todo Implement testValid()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the validate() method
* @todo Implement testValidate()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the validate_on_create() method
* @todo Implement testValidate_on_create()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the validate_on_update() method
* @todo Implement testValidate_on_update()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the __call() method
* @todo Implement test__call()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the __get() method
* @todo Implement test__get()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
* Test the __set() method
* @todo Implement test__set()
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
// Call ActiveRecordTest::main() if this source file is executed directly.
// -- set Emacs parameters --
// c-hanging-comment-ender-p: nil
|