/** * Get contents of one column of record selected by id and table * * When called, {@link $id} identifies one record in the table * identified by {@link $table}. Fetch from the database the * contents of column $column of this record. * @param string Name of column to retrieve * @return string Column contents * @expected_query */ function send($column) { if ($column != $this->expected_query) { PHPUnit2_Framework_Assert::fail('ActiveRecord::send() called with' . ' "' . $column . '", expected "' . $this->expected_query . '"'); } return $this->expected_result; }
/** * Returns "..." in place of common prefix and "..." in * place of common suffix between expected and actual. * * @return string * @access public */ public function toString() { $end = min(strlen($this->expected), strlen($this->actual)); $i = 0; $j = strlen($this->expected) - 1; $k = strlen($this->actual) - 1; for (; $i < $end; $i++) { if ($this->expected[$i] != $this->actual[$i]) { break; } } for (; $k >= $i && $j >= $i; $k--, $j--) { if ($this->expected[$j] != $this->actual[$k]) { break; } } if ($j < $i && $k < $i) { $expected = $this->expected; $actual = $this->actual; } else { $expected = substr($this->expected, $i, $j + 1 - $i); $actual = substr($this->actual, $i, $k + 1 - $i); if ($i <= $end && $i > 0) { $expected = '...' . $expected; $actual = '...' . $actual; } if ($j < strlen($this->expected) - 1) { $expected .= '...'; } if ($k < strlen($this->actual) - 1) { $actual .= '...'; } } return PHPUnit2_Framework_Assert::format($expected, $actual, parent::getMessage()); }
/** * Test saveSQL() * @todo Figure out problem w/ mysql_real_escape_string() * @todo Figure out how to test with magic quotes either on or off */ public function testSafeSQL() { $rs = mysql_connect(); if ($rs == false) { PHPUnit2_Framework_Assert::fail("InputFilterTest:" . " unable to open a connction to MySQL"); } // Trivial case, nothing to clean $this->assertEquals(InputFilter::safeSQL('foo', $rs), 'foo'); $this->assertEquals(InputFilter::safeSQL(array('foo', 'bar'), $rs), array('foo', 'bar')); if (get_magic_quotes_gpc()) { // verify stripping of magic quotes // FIXME: figure out how to test this case $this->assertEquals(InputFilter::safeSQL('a\\\'b\\"c\\\\d\\\\x00e\\\\nf\\\\rg\\\\x1a', $rs), 'a\\\'b\\"c\\\\d\\\\x00e\\\\nf\\\\rg\\\\x1a'); } else { // verify magic quotes aren't there $pattern = "a'b\"c\\de\nf\rgh"; $non_zero_pattern = "a'b\"c\\de\nf\rgh"; $quoted_pattern = "a\\'b\\\"c\\\\de\\\nf\\\rg\\h"; $quoted_non_zero_pattern = "a\\'b\\\"c\\\\de\\\nf\\\rg\\h"; // echo "\nIf this fails it means mysql_real_escape_string() is broken: "; // $this->assertEquals(mysql_real_escape_string($non_zero_pattern), // $quoted_non_zero_pattern); // echo "\nIf this fails it means mysql_real_escape_string() is broken: "; // $this->assertEquals(mysql_real_escape_string($pattern), // $quoted_pattern); // $this->assertEquals( // InputFilter::safeSQL($pattern,$rs),$quoted_pattern); } // Remove the following line when you complete this test. throw new PHPUnit2_Framework_IncompleteTestError(); }
// temporary directory // Create a temporary directory to receive generated files // @todo <b>FIXME:</b> Is this platform independent? do { $tmpdir = '/tmp/traxtest' . mt_rand(0, 99999999); } while (!mkdir($tmpdir, 0700)); define('TRAX_ROOT', $tmpdir); define('TRAX_ENV', 'test'); // Set up information that normally comes from database.ini Trax::$database_settings['test'] = array('phptype' => 'mysql', 'database' => 'test_development', 'hostspec' => 'localhost', 'username' => 'root', 'password' => '', 'persistent' => true); // Create a DB to test with @ini_set('include_path', './mockDB:' . ini_get('include_path')); require_once "DB.php"; $db =& DB::Connect(Trax::$database_settings[TRAX_ENV], array('persistent' => true)); if (PEAR::isError($db) || is_a($db, 'DB_Error')) { PHPUnit2_Framework_Assert::fail("Unable to create database"); } $db->setFetchMode(DB_FETCHMODE_ASSOC); Trax::$active_record_connections[TRAX_ENV] =& $db; require_once "trax_generator.php"; require_once "action_view/helpers.php"; require_once "action_view/helpers/active_record_helper.php"; require_once "active_record.php"; require_once "trax_exceptions.php"; /** * When a class is referenced, get it from app/models */ function __autoload($class_name) { $file = Inflector::underscore($class_name) . ".php"; $file_org = $class_name . ".php";
/** * Returns information about a table or a result set * * @param object|string $result DB_result object from a query or a * string containing the name of a table. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A DB_Error object on failure. * * @see DB_common::tableInfo() * @todo Implement mock DB_mysql::tableInfo() */ function tableInfo($result, $mode = null) { // We only support the default mode PHPUnit2_Framework_Assert::assertNull($mode); // We only support table name as first argument PHPUnit2_Framework_Assert::assertTrue(is_string($result)); // Look up table name in the mock database foreach (self::$database as $table => $value) { if ($result == $table) { return $value['info']; } } PHPUnit2_Framework_Assert::fail("DB_mysql::tableInfo called" . " with unknown table {$result}"); }
/** * @param boolean $looselyTyped * @access public * @static */ public static function setLooselyTyped($looselyTyped) { if (is_bool($looselyTyped)) { self::$looselyTyped = $looselyTyped; } }