2012-03-13 63 views
0

在我的Zend Framework項目中,我使用了一些自定義資源類型,這些類型添加到我的應用程序的Bootstrap.php文件中的資源加載器中。在PHPUnit測試期間自動加載自定義資源類型時未找到類

<?php 

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
    { 
     protected function _initAutoloader() 
     {  
      $resourceLoader = $this->getResourceLoader(); 
      $resourceLoader->addResourceTypes(array(
       'infrastructure' => array(
        'namespace' => 'Infrastructure', 
        'path'  => 'infrastructure/', 
       ), 
       'interfaces' => array(
        'namespace' => 'Interface', 
        'path'  => 'interfaces/', 
       ), 
       'default' => array(
        'namespace' => '', 
        'path'  => '/', 
       ), 
      )); 
     } 

     ... 

} 

我可以自動加載運行時,應用程序,但運行PHPUnit的時候,並試圖在測試過程中加載這些類這些資源沒關係,他們無法找到。

PHP Fatal error: Class 'Wbp_Infrastructure_Persistence_InMemory_TagRepository' not found in /var/www/worldsbestprizes/tests/library/Mbe/Validate/TaggedUriTest.php on line 37 

tests/bootstrap.php文件看起來像這樣。

<?php 

// Define path to application directory 
defined('APPLICATION_PATH') 
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); 

// Define application environment 
defined('APPLICATION_ENV') 
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing')); 

// Ensure library/ is on include_path 
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'), 
    get_include_path(), 
))); 

require_once 'Zend/Loader/Autoloader.php'; 
$autoloader = Zend_Loader_Autoloader::getInstance(); 
$autoloader->registerNamespace('Mbe_'); 

$application = new Zend_Application(
      APPLICATION_ENV, 
      APPLICATION_PATH . '/configs/application.ini' 
); 

有什麼我應該添加到它允許那些自定義資源類型被加載?

回答

1

你必須在一個類似的方法來註冊Wbp_命名空間你在測試/ bootstrap.php中的文件做什麼:

$autoloader->registerNamespace('Wbp_'); 
+0

嗨大衛,感謝您的回覆。我已經添加了建議的代碼,但看起來自動加載器現在正在爲類的庫路徑查找,但未成功。有問題的類位於'APPLICATION_PATH/infrastructure/Persistence/InMemory'中,即'application/infrastructure/Persistence/InMemory',而不是庫路徑 – Leirith 2012-03-13 22:28:41

+0

隨着更改,我現在得到以下錯誤。我收到以下錯誤:Warning:include_once():打開'Wbp/Infrastructure/Persistence/Db/TagRepository.php'for include(include_path ='/ var/www/wbp/application /../ library:/ var/www/wbp/library:。/ usr/share/php:/ usr/share/pear:/home/luke/.netbeans/6.9/zend:/usr/share/php/libzend-framework-php')in/usr/share/php/libzend-framework-php/Zend/Loader.php on line 146' – Leirith 2012-03-13 22:34:21

+0

由於你在Linux上運行(假設),文件系統會區分大小寫。如果你可以將文件放在APPLICATION_PATH/Wbp/Infrastructure/Persistence/Db/TagRepository.php中,那麼它會正確加載。如果你不想在路徑中使用Wbp,我仍然建議大寫基礎結構目錄。此外,您需要添加一個名爲Wbp_ *的名稱空間映射,查看APPLICATION_PATH/Infrastructure/....如果您仍希望它位於小寫基礎結構中,則需要添加一個Wbp_Infrastructure_到APPLICATION_PATH /基礎結構。 – 2012-03-15 15:29:51