power-media / prado3

Automatically exported from code.google.com/p/prado3
Other
0 stars 0 forks source link

Mocking the AR gateway #420

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
To facilitate unit testing of AR code, I would like to mock the 
ActiveRecordGateway. We've just upgraded to Prado 3.2.0, and I see part of the 
needed infrastructure added: TActiveRecordManager::setGatewayClass() now 
enables injection of a custom AR gateway. We're not completely there though...

When changing the gateway class, the old gateway stays intact because it is 
memoized in getRecordGateway(). I would like to propose to modify 
setGatewayClass to forget the gateway, e.g. by setting $this->_gateway to null:

In TActiveRecordManager.php:
    /**
     * Set implementation class of ActiveRecordGateway
     * @param string $value
     */
    public function setGatewayClass($value)
    {
        $this->_gateway = null;
        $this->_gatewayClass = (string)$value;
    }

To use this, I would do:

$className = 
$this->getMockClass('TActiveRecordGateway',array('findRecordsByCriteria'));
TActiveRecordManager::getInstance()->setGatewayClass($className);
$gw = TActiveRecordManager::getInstance()->getRecordGateway();

// In test:
$gw->expects($this->at(0))
    ->method('findRecordsByCriteria')
    ->with(new MyARDerivedClass(), new TActiveRecordCriteria('ORDER BY displayname asc'), true)
    ->will($this->returnValue(array(new MyARDerivedClass(array('id' => 42,'displayname' => 'dispname')))));
// ...
$this->assertEquals(42, $result[0]->id);

Original issue reported on code.google.com by mvsch...@gmail.com on 6 Aug 2012 at 8:07

GoogleCodeExporter commented 8 years ago
Patched in r3196, thank you!

Original comment by ctrlal...@gmail.com on 6 Aug 2012 at 4:24