I want to test if the following method writes ( or deletes ) the data in the expected way:
Method:
protected function saveInstanceRelations( Mage_Core_Model_Abstract $object )
{
$oldInstances = $this->lookupRelationIds( $object->getId(), $this->getTable('slider/slide_instance'), 'instance_id' );
$newInstances = (array) $object->getInstances();
$table = $this->getTable('slider/slide_instance');
$insert = array_diff($newInstances, $oldInstances);
$delete = array_diff($oldInstances, $newInstances);
if ( $delete )
{
$where = array(
'slide_id = ?' => (int) $object->getId(),
'instance_id IN (?)' => $delete
);
$this->_getWriteAdapter()->delete($table, $where);
}
if ( $insert )
{
$data = array();
foreach ($insert as $instanceId) {
$data[] = array(
'slide_id' => (int) $object->getId(),
'instance_id' => (int) $instanceId
);
}
$this->_getWriteAdapter()->insertMultiple($table, $data);
}
}
Test for that method:
/**
* @test
* @loadFixture
* @doNotIndexAll
*/
public function testSaveInstanceRelations()
{
$mock = $this->getMock('Mage_Core_Model_Abstract');
$mock->expects($this->any())
->method('getId')
->will( $this->returnValue( 1 ));
$mock->expects($this->any())
->method('__call')
->with('getInstances')
->will( $this->returnValue( array(1,2)));
EcomDev_Utils_Reflection::invokeRestrictedMethod(
Mage::getResourceModel('slider/slides'),
'saveInstanceRelations',
array( $mock )
);
I could write a simple db-query to check if the data gets written correct, but i don`t think this is a clever way to do that.