2013-02-02 10 views
6

Ho utilizzato l'esempio di album dalla documentazione di Zend Framework 2 e creato un'applicazione.Zend Framework 2 phpunit testare una tabella con un join

Ora mentre lo si sta disattivando utilizzando phpunit Ho riscontrato un problema durante il test di una tabella che sta avendo un join con la tabella di esempio Account_Type.

Ecco il codice per questo.

funzione

fetchAll è

function fetachAll() 
{ 
    $sql = new Sql($this->tableGateway->getAdapter()); 

    $select = $sql->select(); 

    $select->from('Album') 
      ->columns(array('id', 'name', 'account_type_id', 'managing_account_id')) 
     ->join(array('AT' => 'account_type'), 'album.account_type_id = AT.account_type_id'); 

    $resultSet = $this->tableGateway->selectWith($select); 

    return $resultSet; 
} 

codice di prova Unità per la tabella sopra è.

public function testFetchAllReturnsAllAlbums() 
{ 
    $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)); 

    $albumTable = new AlbumTable($mockTableGateway); 

    $this->assertSame($resultSet, $albumTable->fetchAll()); 
} 

sto errore ottenendo questo errore

Argument 1 passed to Zend\Db\Sql\Sql::__construct() must be an instance of 
Zend\Db\Adapter\Adapter, null given, 

per questa linea $this->assertSame($resultSet, $albumTable->fetchAll()); in testFetchAllReturnsAllAlbums metodo.

Se qualcuno ha eseguito test di phpunit per partecipare, fornire un esempio per lo stesso.

+2

Si sta chiamando '$ this-> tableGateway-> getAdapter()' sul proprio oggetto mock, che restituisce NULL, e 'Zend \ Db \ Sql \ Sql' si aspetta un'istanza di' Zend \ Db \ Adapter \ Adapter' . Non ho ancora molta familiarità con gli oggetti finti, ma non stai prendendo in giro il metodo 'select' su' TableGateway', ma nel tuo 'AlbumTable', stai chiamando il metodo 'select' sull'oggetto' Sql'? – Andy0708

risposta

1

Si potrebbe voler prendere in giro il metodo getAdapter dell'oggetto Zend\Db\TableGateway\TableGateway. Questo metodo viene chiamato e il suo valore restituito è passato al costruttore Zend\Db\Sql\Sql.

Problemi correlati