2010年5月1日星期六

[fw-db] Unexpected behavior from Zend_Db_Table_Row_Abstract::setFromArray()

I'm getting some unexpected behavior from
Zend_Db_Table_Row_Abstract::setFromArray(). If I instantiate a new row
without passing any data and subsequently call setFromArray() to load data,
Zend_Db_Table_Row::toArray() returns an empty array.

I've never tried to instantiate a row in that manner in an application. I
ran into this while writing some unit tests. I couldn't find a test in ZF
that covered this use case, so I whipped one up to demonstrate the issue.

Line 663 in Zend_Db_Table_Row_Abstract seems to be causing the issue (unless
it's my improper use of the class that's the issue) when it calls
array_intersect with an empty array as the second argument.

Thoughts?

<?php

/**
* Zend_Db_Table_Row test case.
*/
class Kendall_Db_Table_Row_ZendRowTest extends PHPUnit_Framework_TestCase {

/**
* @var Zend_Db_Table_Row
*/
private $_row;

/**
* @var array Initial dataset
*/
private $_data = array();

/**
* Prepares the environment before running a test.
*/
protected function setUp() {

parent::setUp();

$this->_data = array(
'row_one' => 1,
'row_two' => 2,
'row_three' => 3
);

$this->_row = new Zend_Db_Table_Row();

}

/**
* Cleans up the environment after running a test.
*/
protected function tearDown() {
// TODO Auto-generated Kendall_Db_Table_Row_ZendRowTest::tearDown()
$this->_row = null;
parent::tearDown();
}

/**
* Tests Zend_Db_Table_Row->setFromArray()
*
* Test fails with
* 1) Kendall_Db_Table_Row_ZendRowTest::testSetFromArray
* Failed asserting that <boolean:false> is true.
*/
public function testSetFromArrayFailsToSetData() {
$this->_row->setFromArray($this->_data);
$this->assertTrue(array_key_exists('row_one',
$this->_row->toArray()));
}

/**
* Tests Zend_Db_Table_Row->setFromArray()
*
* This test passes.
*
* I think the problem is on Zend_Db_Table_Row_Abstract->setFromArray()
line 663.
* Calling array intersect on ($array, $emptyArray) always returns an
empty array
*/
public function testSetFromArrayReturnsEmptyArray() {
$this->_row->setFromArray($this->_data);
$this->assertEquals(0, count($this->_row->toArray()));
}

}

--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/Unexpected-behavior-from-Zend-Db-Table-Row-Abstract-setFromArray-tp2122159p2122159.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: