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 ",
|