2011-01-09 4 views
3

Wenn ich meinen PHP-Unit-Test laufen erhalte ich:(PHP): Die Testmodelle mit Zend_Test_PHPUnit_DatabaseTestCase

1) Test_Model_Mapper_TestTest::testTest 
Argument 1 passed to PHPUnit_Extensions_Database_DataSet_DefaultTableIterator::__construct() must be an array, null given, called in /usr/share/php/PHPUnit/Extensions/Database/DataSet/AbstractXmlDataSet.php on line 134 and defined 

/var/www/kosheroven/library/Zend/Test/PHPUnit/Db/Operation/Truncate.php:73 
/var/www/kosheroven/tests/ModelTestCase.php:79 
/var/www/kosheroven/tests/application/models/mappers/TestTest.php:33 

Erwartetes Ergebnis ist für den Test, offensichtlich zu passieren. Indem ich ein paar Echos besprenkelte, entdeckte ich, dass dies aus dem Aufruf von parent :: setUp() stammt, aber ich habe keine Ahnung warum. Ich bin völlig festgefahren. Jede Hilfe würde sehr geschätzt werden.

// /tests/ModelTestCase.php 

abstract class Test_ModelTestCase extends Zend_Test_PHPUnit_DatabaseTestCase 
{ 
    public $application; 
    protected $_db; 
    protected $_model; 
    protected $_modelClass; 
    protected $_filesDir; 

    public function setUp() 
    { 
     $this->application = new Zend_Application(
      APPLICATION_ENV, 
      APPLICATION_PATH . '/configs/application.ini' 
     ); 

     $this->bootstrap = array($this, 'appBootstrap'); 

     $this->_filesDir = dirname(__FILE__) . '/files/'; 
     $this->_filesDir .= str_replace('_', '/', get_class($this)); 
     $this->_model  = new $this->_modelClass(); 
     // echo '123'; is printed 
     parent::setUp(); 
     // echo '456'; is not 
    } 

    public function appBootstrap() 
    { 
     $this->application->bootstrap(); 
    } 

    protected function getConnection() 
    { 
     if(empty($this->_db)) 
     { 
      $options = $this->application->getOptions(); 
      $schema = $options['resources']['db']['params']['dbname']; 
      $db = $this->application->getBootstrap()->getPluginResource('db') 
        ->getDbAdapter(); 

      $this->_db = $this->createZendDbConnection($db, $schema); 
     } 

     return $this->_db; 
    } 

    protected function getDataSet() 
    { 
     return $this->createXmlDataSet(dirname(__FILE__) . '/files/seed.xml'); 
    } 
} 

// /tests/Model/Mapper/TestTest.php 

class Test_Model_Mapper_TestTest extends Test_ModelTestCase 
{ 
    protected $_modelClass = 'Application_Model_Mapper_Ingredients'; 

    public function testTest() 
    { 
     $this->assertTrue(true); 
    } 
} 

Antwort

1

Nicht sicher, ob es Ihnen helfen, aber das Problem sein kann, mit: $this->bootstrap = array($this, 'appBootstrap');

Sie $ Bootstrap-Eigenschaft sehen nicht in Zend_Test_PHPUnit_DatabaseTestCase existiert, anders als bei Zend_Test_PHPUnit_ControllerTestCase. Also ich denke, dass deine Bootstrap-Methode nicht aufgerufen wird.

So könnten Sie versuchen, Zeile $this->bootstrap = array($this, 'appBootstrap'); durch $this->appBootstrap(); zu ersetzen.

0

Voll Lösung und Code bei http://www.unexpectedit.com/zend-php/testing-database-model-with-phpunit-on-zend-studio

bearbeiten application/configs/application.ini

[testing : production] 
resources.db.adapter = "pdo_mysql" 
resources.db.params.host = 127.0.0.1 
resources.db.params.port = 8889 
resources.db.params.username = root 
resources.db.params.password = root 
resources.db.params.dbname = "test_myproject_com" 
xmlseeds.folder = APPLICATION_PATH "/../tests/xmlseeds/" 

Ordner erstellen und MyProject/Library/Application/Test/PHPUnit/DatabaseTestCase/Abstract einzureichen. php

Ordner und Datei erstellen MyProject/tests/application/models/ProjectTest.php

Erstellen von Dateien /tests/xmlseeds/*.xml

I. Pascual www.unexpectedit.com

1

ich heute das gleiche Problem hatte. Der Grund dafür war, dass das XML-Fixture von MySQLDump erzeugt wurde und der <database name="xyz"> Knoten fehlte. Dies hat $ this-> tables in PHPUnit in NULL anstelle von Array verwandelt.