How can I stop Zend_Db_Table_Abstract fetchrow returning garbage data when running tests?
I'm fairly new to Zend and am having some difficulties with creating my unit tests. I've stripped it down to a minimal test class that still replicates the problem in the hope that it is something daft that I'm doing:
class UserRegistrationsTest extends PHPUnit_Framework_TestCase
{
protected $db;
public function __construct($name = NULL) { parent::__construct ( $name );
$this->db = Zend_Db::factory('Pdo_Mysql', array( 'host' => '127.0.0.1', 'username' => 'root', 'password' => 'xxxxxxxx', 'dbname' => 'testingdb' ));
Zend_Db_Table_Abstract::setDefaultAdapter($this->db); }
public function testName() { $users = new Users(); $select = $users->select()->where('regCode = ?', 'deadbeef'); $row = $users->fetchRow($select); }
}
So this is the simplified test. I have a db set up (which is connects to correctly) that has data inside it. There is a record with regCode set to 'deadbeef' and it also has fields for email, regDate and affiliate:
CREATE TABLE testingdb.users (
regCode varchar(16) NOT NULL,
email varchar(150) NOT NULL,
regDate datetime NOT NULL,
affiliate int(10) unsigned DEFAULT NULL,
PRIMARY KEY (regCode))
I have a class called Users.php that is as simple as it gets:
class Users extends Zend_Db_Table_Abstract {
protected $_name = 'users';
}
My problem is that I can insert into the database fine, but any queries r开发者_开发技巧eturn odd results. The fetchRow above generates the correct SQL but the returned row is garbage - it returns a row containing regCode, email, no regDate column at all, then the affiliate is there but the collumn is called 'users' instead of 'affiliate'.
I've tried it on other tables and none work - some even return the database name as a column header as well as the table name as a column.
One thing to note is that if I'm not running this in a test it works fine. The exact same code and db in the proper app work great, but running as a phpUnit Test and it gets all screwy :-(
Any advice appreciated
Cheers, Bryn
Do you have any other table or even another database with a table that has a regCode field? You could be using the wrong adapter, that would explain why your query still executes. Prerequisite for your test environment would be at least to have the exact same structure as your production environment (tables + fields). If they differ, some queries may execute well, some others may not.
This was an old Zend bug that is fixed now
精彩评论