|  | Posted by Steve on 10/06/06 16:08 
"Bit Byte" <root@yourbox.com> wrote in message news:mcWdnWn5urunJr7YnZ2dnUVZ8qKdnZ2d@bt.com...
 |
 |
 | .:[ ikciu ]:. wrote:
 | > Hmm Bit Byte <root@yourbox.com> wrote:
 | >
 | >>I want to return dummy data from a stub function, this stub function
 | >>should replicate data from a database. Any idea how I can write this
 | >>stub function?
 | >
 | >
 | > what format? arrays objects?
 | >
 | >
 |
 | returning (nested?) array objects will be good
 
 while i don't use pear, i do have my own db abstraction class whose
 interface is always the same no matter the back end. the format i return
 results in is this...which relates to your stubby:
 
 (this is the mysql class that implements my abstract db interface...the
 execute method)
 
 ===========
 
 static function execute($sql, $decode = false, $returnNewId = false)
 {
 self::$_lastStatement = $sql;
 $array                = array();
 $key                  = 0;
 $records              = mysql_query($sql);
 $fieldCount           = @mysql_num_fields($records);
 $translation          = get_html_translation_table(HTML_ENTITIES);
 $translation          = array_flip($translation);
 while ($row = @mysql_fetch_array($records, MYSQL_NUM))
 {
 for ($i = 0; $i < $fieldCount; $i++)
 {
 $value = $row[$i];
 if ($decode){ $value = strtr($value, $translation); }
 $array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
 }
 $key++;
 }
 if ($returnNewId)
 {
 $array = array();
 $array[0]['ID'] = mysql_insert_id();
 }
 @mysql_free_result($records);
 return $array;
 }
 
 =============
 
 this returns all records into the standard form of an indexed array of
 columns that point to their respective values...such that:
 
 =============
 
 $records   = db::execute("select username, password from users");
 $headerRow = array_keys($records[0]);
 echo "<table>\r\n";
 echo "  <th>\r\n" .
 implode("  </th>\r\n  <th>\r\n", $headerRow) .
 "  </th>\r\n";
 foreach ($records as $record)
 {
 echo "  <tr>\r\n    <td>\r\n      " .
 implode("    </td>\r\n  <td>\r\n", $array_values($record)) .
 "    </td>\r\n  </tr>\r\n";
 }
 echo "</table>";
 
 =============
 
 would dump your data nicely. to stub any portion of your data, just create
 in include/require file in which you had set a variable to an array of this
 structure that was populated by hand. then, when the db::execute method was
 called you could simply parse for the table name and return the appropriate
 array. crappy explanation...here's an example:
 
 =============
 
 static function execute($sql, $decode = false, $returnNewId = false)
 {
 $stub     = '';
 $stubData = array(); // array that all stubbed data files build w/ fake
 data
 // need to reset it since previous call to 'execute'
 will
 // have imported a previous tables dataset version
 // this also makes sure to return an empty array if
 no stub
 // data is found for this $table
 $table    = preg_replace('pattern to pull table name', $sql);
 switch ($table)
 {
 case 'foo': $stub = 'foo.stub.data.php'; break;
 case 'bar': $stub = 'bar.stub.data.php'; break;
 default   : return array();
 }
 include $stub;
 return $stubData;
 }
 
 =============
 
 hth,
 
 me
 [Back to original message] |