2013-04-30 39 views
2

嗯,這個人讓我的頭髮掉光了......ZF2模型單元測試,沒有發現類

我做了一些有用的東西在ZF1,現在我掙扎切換到ZF2,以及我們該做的對,我想把東西做成TDD風格。

我設置了Skeleton應用程序,然後創建了另外兩個模塊,名爲「Weather」和「Airport」。我比WeatherController做了一個很好的測試用例。比我做了一個試驗案例機場模塊中的模型和它失敗:

Fatal error: Class 'Airport\Model\Airport' not found in C:\xampp\htdocs... 

,並在此觸發錯誤(AirportTableTest.php):

<?php 

namespace AirportTest\Model; 

use Airport\Model\Airport; 
use Airport\Model\AirportTable; 
use PHPUnit_Framework_TestCase; 

class AirportTableTest extends PHPUnit_Framework_TestCase { 

    public function testExample() { 
     $airport = new Airport(); // - this is not getting loaded and throws the fatal error :(
    } 

} 

的代碼是基於相冊ZF2教程中的模塊示例。 AirportTable模型應該用於連接數據庫中的SQL表格,機場模型的寫法與教程中編寫的相冊模型相似。的目錄結構(abbrevated):

/module 
    /Airport 
    /src 
     /Airport 
     /Controller 
     /Model 
      AirportTable.php 
      Airport.php 
    /Application 
    /Weather 
/public 
/tests 
    /module 
    /Airport 
     /src 
     /Airport 
      /Controller 
      /Model 
      AirportTableTest.php 
      AirportTest.php 
    /Application 
    /Weather 
    bootstrap.php 
    phpunit.xml 
/vendor 

bootstrap.php中從測試目錄:

<?php 
chdir(dirname(__DIR__)); 
error_reporting(E_ALL | E_STRICT); 
include __DIR__.'/../init_autoloader.php'; 

的Airport.php與未裝載的類:

<?php 
namespace Airport\Model; 

class Airport 
{ 
    public $icao; 
    public $lat; 
    public $lng; 
    public $metar; 

    public function exchangeArray($data){ 
     $this->icao = (isset($data['id'])) ? $data['icao'] : null; 
     $this->lat = (isset($data['lat'])) ? $data['lat'] : null; 
     $this->lng = (isset($data['lng'])) ? $data['lng'] : null; 
     $this->metar = (isset($data['metar'])) ? $data['metar'] : null; 
    } 
} 
?> 

的機場模塊Module.php:

<?php 
namespace Airport; 

use Airport\Model\Airport; 
use Airport\Model\AirportTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module 
{ 
    public function getConfig() 
    { 
     return include __DIR__ . '/config/module.config.php'; 
    } 

    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Airport\Model\AirportTable' => function($sm) { 
        $tableGateway = $sm->get('AirportTableGateway'); 
        $table = new AirportTable($tableGateway); 
        return $table; 
       }, 
       'AirportTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Airport()); 
        return new TableGateway('airport', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 

} 

所以我可能錯過了一些非常明顯的東西,比如自動加載器的東西可能相關?所以,嗯...幫助也許(漂亮)?

+0

順便說一句,「Weather」模塊的模型和測試集以相同的方式工作得很好,這更加令人困惑... – 2013-04-30 12:17:29

回答

1

Sooo,我想出了一個工作解決方案,雖然我不太確定它的智能還是完全智能化。

基於PHPUnit with a Zend Framework 2 module我增加了行

Zend\Mvc\Application::init(include '/../config/application.config.php'); 

到測試套件的bootstrap.php中,現在一切正常,但我不知道自己爲什麼會沒有這條線的工作「天氣」模塊,而不是「機場」模塊...

0

你可能想看看ZF2入門教程的方式奠定了測試。我已完成教程並對my own fork of the ZF2 Skeleton Application source進行了更改。

基本上,每個模塊都有它自己的測試套件,用配置的專用引導文件和phpunit.xml運行時會告訴PHPUnit的,當您運行測試以加載這一切(只要你在測試目錄裏phpunit)。這有助於保持測試的模塊化。

+0

嗨,我正在使用您的測試回購,但已經發現跟隨錯誤。 $ vendor/phpunit/phpunit/composer/bin/phpunit模塊/應用程序/測試 Sebastian Bergmann的PHPUnit 3.7.29。 致命錯誤:在第24行的my-dir-path \ module \ Application \ test \ ApplicationTest \ Controller \ IndexControllerTest.php中找不到類'ApplicationTest \ Bootstrap' – 2014-01-19 00:17:20