php - PHPUnit - mock callback function -


here function uses zend db / tablegateway :

public function listattestations($ssidx = null, $ssord = null, $ioffset = 0, $ilimit = 0) {     try {         $resultset = $this->tablegateway->select(             function (select $select) use ($ssidx, $ssord, $ioffset, $ilimit) {                 if ($ssidx != null && $ssord != null) {                     $select->order($ssidx.' '.$ssord);                 }                 $select->join(                     'f_travclient',                     'syndic_client_id = f_travclient.travclient_id',                      array('syndic' => 'nom')                 );                 $select->offset($ioffset);                 $select->limit($ilimit);             }         );         return $resultset;     } catch (\exception $e) {         throw new \exception($e);     } } 

i use phpunit unit tests. perhaps, don't know how make function crosses previous method. thought functional :

public function testlistattestations() {     $resultset = new resultset();      $mocktablegateway = $this->getmock('zend\db\tablegateway\tablegateway', array('select'), array(), '', false);      $mocktablegateway->expects($this->once())             ->method('select')             ->with()             ->will($this->returnvalue($resultset));      $attesttable = new fmaiattestationtable($mocktablegateway, $this->adapter, $this->sql);      $this->assertsame($resultset, $attesttable->listattestations('maiattestation_id', 'asc', 0, 30)); } 

but doesn't go further :

function (select $select) use ($ssidx, $ssord, $ioffset, $ilimit) { 

could me ? thanks.

you can fetch , use arguments given mocked method returncallback():

$mocktablegateway->expects($this->once())         ->method('select')         ->with()         ->will($this->returncallback(function($function) use ($resultset) {             // function, example:             if(!is_callable($function)) {                 return null;             }             call_user_func($function, new select());             return $resultset;         })); 

still, might want reconsider current code not necessary write nested this. could, example, instance of select , use selectwith()

public function listattestations($ssidx = null, $ssord = null, $ioffset = 0, $ilimit = 0) {     try {         $select = new select();         if ($ssidx != null && $ssord != null) {             $select->order($ssidx.' '.$ssord);         }         $select->join(             'f_travclient',             'syndic_client_id = f_travclient.travclient_id',             array('syndic' => 'nom')         );         $select->offset($ioffset);         $select->limit($ilimit);         $resultset = $this->tablegateway->selectwith($select);         return $resultset;     } catch (\exception $e) {         throw new \exception($e);     } } 

in case possible test if method puts select way want. can create select object in test way expect be:

    $resultset = new resultset();      $ssidx = 'maiattestation_id';     $ssord = 'asc';     $ioffset = 0;     $ilimit = 30;      $select = new select();     $select->order($ssidx.' '.$ssord);     $select->join(         'f_travclient',         'syndic_client_id = f_travclient.travclient_id',         array('syndic' => 'nom')     );     $select->offset($ioffset);     $select->limit($ilimit);      $mocktablegateway = $this->getmock('zend\db\tablegateway\tablegateway',         array('selectwith'), array(), '', false);      $mocktablegateway->expects($this->once())         ->method('selectwith')         ->with($select)         ->will($this->returnvalue($resultset));      $attesttable = new fmaiattestationtable($mocktablegateway, $this->adapter, $this->sql);      $this->assertequals($resultset, $attesttable->listattestations($ssidx, $ssord, $ioffset, $ilimit)); 

if select object used mocked method selectwith looks different 1 created, phpunit throw error in test.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -