2013-01-05 46 views
0

我正在進入Zend和單元測試階段,所以對我一無所知。我使用PHP 5.3.4,Zend 1.11.11和PHPUnit 3.7.1運行Wamp Server。我的項目結構是默認的Zend佈局,組織成這樣的測試文件夾:當基本「測試」文件夾之外時,PHPUnit找不到Zend_Test_PHPUnit_ControllerTestCase

tests/ 
    application/ 
    library/ 
    bootstrap.php 
    phpunit.xml 
    IndexControllerTest.php 

如果雙方application/library/是空的。這個基本情況與我目前的設置正常工作,在那裏我可以從終端運行phpunit IndexControllerTest,它運行測試沒有錯誤。一旦將IndexControllerTest.php移動到不同的位置(例如在application/目錄中),就會出現問題。我再從PHPUnit的收到一個致命錯誤:

PHP Fatal error: Class 'Zend_Test_PHPUnit_ControllerTestCase' not found in 
X:\Program Files (x86)\Wamp\www\Local_site\Zend\login\tests\application\IndexControllerTest.php 
on line 3 

現在,除非我錯了,磁帶自動加載機應該採取這種require()的照顧,但我改變我的測試文件的位置,什麼是錯的,一旦去。我試過反覆調整phpunit.xml文件,但我不確定這是我的問題所在,還是在我的bootstrap.php或我的代碼中。任何有識之士將不勝感激。即使是在正確的方向推動。以下是相關文件:

phpunit.xml

<phpunit bootstrap="./bootstrap.php"> 
<testsuites> 
<testsuite name="My Project"> 
    <directory>./tests</directory> 
</testsuite> 

</testsuites> 

<filter> 
    <!-- If Zend Framework is inside your project's library, uncomment this filter --> 

    <whitelist> 
     <directory suffix=".php">/library/Zend</directory> 
    </whitelist> 

</filter> 
</phpunit> 

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'; 
Zend_Loader_Autoloader::getInstance(); 

IndexControllerTest.php

<?php 
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase 
{ 
    protected $application; 

    public function setUp() 
    { 
     // Assign and instantiate in one step: 
     $this->bootstrap = new Zend_Application(
      'testing', 
      APPLICATION_PATH . '/configs/application.ini' 
      ); 
     parent::setUp(); 
    } 

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

按照從金互聯網其他的建議,我也改變了我的PHP/php.ini中的include_path是:

include_path=".;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear\PHPUnit" 

我需要明確包括Zend/Test/PHPUnit/ControllerTestCase.php?我已經嘗試了數以百計的解決方案,但我一直在盲注,所以我本可以非常接近,甚至不知道它。

+0

從哪裏運行phpunit當你移動* Test.php文件?在這種情況下配置文件是否被加載? – cweiske

+0

如在,我運行'phpunit應用程序/ IndexControllerTest'如果IndexControllerTest.php位於tests/application/IndexControllerTest.php?是的,我做了(和同樣的錯誤),並沒有產生任何錯誤的配置文件未被加載。即使我將配置文件(我假設是phpunit.xml)移動到測試/應用程序中,我仍然收到相同的錯誤。 –

回答

0

設法找到一個解決方案(儘可能簡單......)來解決這個問題。無論出於何種原因,phpunit.xml不會引導我的bootstrap.php文件。所有有人需要我的引導要求之前,我宣佈我的IndexControllerTest.php類:

IndexControllerTest

require_once '/path/to/bootstrap.php'; 

IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase 
{ 
     public function testTrue() 
     { 
       $this->assertTrue(true); 
     } 
} 

這似乎很奇怪有要求這在每個測試文件,我相信我會找到一個更永久的解決方案很快希望這有助於某人。

編輯

稍微好一點的解決方案發現,我只是複製我的phpunit.xml文件在同一目錄IndexControllerTest,並在新phpunit.xml文件更改bootstrap="bootstrap.php"bootstrap="../bootstrap.php"。現在,該目錄中的所有測試文件都不需要require_once。仍然看起來像一個粗略的修復,但總比沒有好。

0

我一直在爲此而努力,在過去幾個小時,使用我設法想出這是我加入到我的應用程序/ bootstrap.php中

require_once 'Zend/Loader/Autoloader/Resource.php'; 

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath'  => APPLICATION_PATH, 
'namespace'  => '', 
'resourceTypes' => array(
    'cplib' => array(
     'path'  => 'library/CP', 
     'namespace' => 'CP_' 
    ), 
), 
)); 

一旦我有這個在下面的答案我可以自動加載正確的Zend庫文件,而無需手動指定它們。

0

你不應該在你的測試用例中明確包含引導文件。

您需要在包含路徑中包含庫文件夾(其中包含Zend庫)。 bootstrap.php文件應該對此負責。

此錯誤消息有兩個可能的原因:

  1. 的phpunit.xml不能指向正確的bootstrap.php。

  2. 引導文件不引用正確的庫路徑(我認爲這是這裏的情況)

可以呼應的bootstrap.php裏面包含路徑,以確保其設置正確。

基本上,加上下面幾行放在require_once

$includePath = get_include_path(); 
echo "appPath = ".APPLICATION_PATH. "\n"; 
echo "includePath = $includePath \n"; 

的INCLUDEPATH應該有一個應用程序/庫文件夾 (注意條目之前bootstrap.php中有2個應用程序文件夾,一個持有類另一個是測試,你應該指的是測試目錄外的那個)

here是典型的文件夾結構。