cvs: pear /MDB2/tests MDB2_function_testcase.php
| From: | Lukas Smith | Date: | Mon, 17 Oct 2005 08:08:30 +0000 |
| Subject: | cvs: pear /MDB2/tests MDB2_function_testcase.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-35635@lists.php.net to get a copy of this message | ||
lsmith Mon Oct 17 04:08:30 2005 EDT
Modified files:
/pear/MDB2/tests MDB2_function_testcase.php
Log:
- refactored test cases to use an MDB2_testcase base class
- added testcase for now() and functionTable()
- changed substring() testcause to use the users table (we only assume the users and files table
exist .. all others have to be created and cleaned up during the testing)
http://cvs.php.net/diff.php/pear/MDB2/tests/MDB2_function_testcase.php?r1=1.2&r2=1.3&ty=u
Index: pear/MDB2/tests/MDB2_function_testcase.php
diff -u pear/MDB2/tests/MDB2_function_testcase.php:1.2
pear/MDB2/tests/MDB2_function_testcase.php:1.3
--- pear/MDB2/tests/MDB2_function_testcase.php:1.2 Sun Oct 16 18:29:53 2005
+++ pear/MDB2/tests/MDB2_function_testcase.php Mon Oct 17 04:08:30 2005
@@ -41,54 +41,52 @@
// | Author: Lorenzo Alberton <l dot alberton at quipo dot it> |
// +----------------------------------------------------------------------+
//
-// $Id: MDB2_function_testcase.php,v 1.2 2005/10/16 22:29:53 quipo Exp $
+// $Id: MDB2_function_testcase.php,v 1.3 2005/10/17 08:08:30 lsmith Exp $
-class MDB2_Function_TestCase extends PHPUnit_TestCase
+class MDB2_Function_TestCase extends MDB2_TestCase
{
- //contains the dsn of the database we are testing
- var $dsn;
- //contains the options that should be used during testing
- var $options;
- //contains the name of the database we are testing
- var $database;
- //contains the MDB2 object of the db once we have connected
- var $db;
-
- function MDB2_Function_TestCase($name) {
- $this->PHPUnit_TestCase($name);
- }
-
function setUp() {
- $this->dsn = $GLOBALS['dsn'];
- $this->options = $GLOBALS['options'];
- $this->database = $GLOBALS['database'];
- $this->db =& MDB2::factory($this->dsn, $this->options);
- if (PEAR::isError($this->db)) {
- $this->assertTrue(false, 'Could not connect to database in setUp - '
.$this->db->getMessage() . ' - ' .$this->db->getUserInfo());
- exit;
- }
- $this->db->setDatabase($this->database);
- $this->db->expectError(MDB2_ERROR_UNSUPPORTED);
+ parent::setUp();
$this->db->loadModule('Function');
}
- function tearDown() {
- $this->db->popExpect();
- unset($this->dsn);
- if (!PEAR::isError($this->db)) {
- $this->db->disconnect();
+ /**
+ * Test now()
+ */
+ function testFunctionTable()
+ {
+ if (!$this->methodExists($this->db->function, 'functionTable')) {
+ return;
+ }
+
+ $functionTable_clause = $this->db->function->functionTable();
+ $query = 'SELECT 1 '.$functionTable_clause;
+ $result = $this->db->queryOne($query);
+ if (PEAR::isError($result)) {
+ $this->assertFalse(true, 'Error getting substring');
+ } else {
+ $this->assertEquals('1', $result, 'Error: not a proper
timestamp');
}
- unset($this->db);
}
- function methodExists(&$class, $name) {
- if (is_object($class)
- && array_key_exists(strtolower($name),
array_change_key_case(array_flip(get_class_methods($class)), CASE_LOWER))
- ) {
- return true;
+ /**
+ * Test now()
+ */
+ function testNow()
+ {
+ if (!$this->methodExists($this->db->function, 'now')) {
+ return;
+ }
+
+ $functionTable_clause = $this->db->function->functionTable();
+ $now_clause = $this->db->function->now('timestamp');
+ $query = 'SELECT '.$now_clause . $functionTable_clause;
+ $result = $this->db->queryOne($query);
+ if (PEAR::isError($result)) {
+ $this->assertFalse(true, 'Error getting substring');
+ } else {
+ $this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $result,
'Error: not a proper timestamp');
}
- $this->assertTrue(false, 'method '. $name.' not implemented in
'.get_class($class));
- return false;
}
/**
@@ -99,47 +97,42 @@
if (!$this->methodExists($this->db->function, 'substring')) {
return;
}
- $record = array(
- 'number' => 1,
- 'trans_en' => 'number_one',
- 'trans_fr' => 'blahblah',
- );
-
- $result = $this->db->query('DELETE FROM numbers');
- $this->assertFalse(PEAR::isError($result), 'Error truncating table');
- $query = 'INSERT INTO numbers (number, trans_en, trans_fr) VALUES ('
- . $record['number'] . ','
- . $this->db->quote($record['trans_en']) . ','
- . $this->db->quote($record['trans_fr']) . ')';
- $result = $this->db->query($query);
- $this->assertFalse(PEAR::isError($result), 'Error inserting sample record');
+ $data = $this->getSampleData(1234);
+
+ $stmt = $this->db->prepare('INSERT INTO users (' . implode(', ',
array_keys($this->fields)) . ') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
array_values($this->fields));
+ $result = $stmt->execute(array_values($data));
+ $stmt->free();
+
+ if (PEAR::isError($result)) {
+ $this->assertTrue(false, 'Error executing prepared
query'.$result->getMessage());
+ }
- $substring_clause = $this->db->function->substring('trans_en', 1, 6);
- $query = 'SELECT '.$substring_clause .' FROM numbers';
+ $substring_clause = $this->db->function->substring('user_name', 1, 4);
+ $query = 'SELECT '.$substring_clause .' FROM users';
$result = $this->db->queryOne($query);
if (PEAR::isError($result)) {
$this->assertFalse(true, 'Error getting substring');
} else {
- $this->assertEquals('number', $result, 'Error: substrings not
equals');
+ $this->assertEquals('user', $result, 'Error: substrings not
equals');
}
- $substring_clause = $this->db->function->substring('trans_en', 5, 3);
- $query = 'SELECT '.$substring_clause .' FROM numbers';
+ $substring_clause = $this->db->function->substring('user_name', 5, 1);
+ $query = 'SELECT '.$substring_clause .' FROM users';
$result = $this->db->queryOne($query);
if (PEAR::isError($result)) {
$this->assertFalse(true, 'Error getting substring');
} else {
- $this->assertEquals('er_', $result, 'Error: substrings not
equals');
+ $this->assertEquals('_', $result, 'Error: substrings not
equals');
}
//test NULL 2nd parameter
- $substring_clause = $this->db->function->substring('trans_en', 8);
- $query = 'SELECT '.$substring_clause .' FROM numbers';
+ $substring_clause = $this->db->function->substring('user_name', 6);
+ $query = 'SELECT '.$substring_clause .' FROM users';
$result = $this->db->queryOne($query);
if (PEAR::isError($result)) {
$this->assertFalse(true, 'Error getting substring');
} else {
- $this->assertEquals('one', $result, 'Error: substrings not
equals');
+ $this->assertEquals('1234', $result, 'Error: substrings not
equals');
}
}
}