2012-04-10 65 views
3

我正在開發一個PHP項目並使用PHPUnit和Doctrine 2.我使用最新版本。我寫了很多測試來測試所有的類,函數和行爲。測試總是在運行。一切正常。由於我使用Doctrine,我已經應用了最佳實踐技巧並在構造函數中初始化ArrayCollection。PHPUnit不再運行在Netbeans的ArrayCollection(Doctrine 2)初始化後

public function __construct($username, $eMail, $password1, $password2) { 
    $this->quiz = new ArrayCollection(); 
    $this->sessions = new ArrayCollection(); 
    $this->setUsername($username); 
    $this->setEMail($eMail); 
    $this->setPassword('', $password1, $password2); 
} 

的包括ArrayCollection的是:

use Doctrine\Common\Collections\ArrayCollection; 

這點之後,PHPUnit測試不再運行。

當我用初始化註釋行時,所有的測試再次運行。成員測驗和會議是私人的。該應用程序通常有效。

我是新的Doctrine 2和PHPUnit。到現在爲止,我已經嘗試了很多東西,像測試用例中的不同包含等,但沒有任何幫助。 也許我在測試用例中忘記了一些東西。在ArrayCollection的步驟初始化和沒有初始化之間,我在測試用例中沒有改變任何內容。

測試用例看起來是這樣的:

namespace Model; 

require_once dirname(__FILE__) . '/../../Model/User.php'; 

/** 
* Test class for User. 
* Generated by PHPUnit on 2012-04-03 at 14:48:39. 
*/ 
class UserTest extends \PHPUnit_Framework_TestCase { 

    /** 
    * @var User 
    */ 
    protected $user; 

    // constructor of the test suite 
    function UserTest($name) { 
     $this->PHPUnit_TestCase($name); 
    } 

    /** 
    * Sets up the fixture, for example, opens a network connection. 
    * This method is called before a test is executed. 
    */ 
    protected function setUp() { 
     $username = 'musteruser'; 
     $eMail = '[email protected]'; 
     $pw = 'muster.muster'; 
     $this->user = new User($username, $eMail, $pw, $pw); 
    } 

    /** 
    * Tears down the fixture, for example, closes a network connection. 
    * This method is called after a test is executed. 
    */ 
    protected function tearDown() { 
     unset($this->user); 
    } 

現在我真的不知道可能是什麼問題。也許有人已經經歷過相同的事情,或者有什麼想法可能是什麼問題。

感謝您的任何幫助。

回答

0

我找到了解決方案。我創建了一個名爲bootstrap.php中有以下內容的文件:

// Import the ClassLoader 
use Doctrine\Common\ClassLoader; 

// Autoloader for Doctrine 
require '/../../php/PEAR/Doctrine/Common/ClassLoader.php'; 

// Autoloading for Doctrine 
$doctrineClassLoader = new ClassLoader('Doctrine', realpath(__DIR__ . '/../../php/PEAR')); 
$doctrineClassLoader->register(); 

該文件被放置在測試文件,在NetBeans項目的一部分,單元測試的主目錄。重要的是,這是測試用例中沒有的東西。

0

當您調用PHPUnit時,您可能需要確保Doctrine \ Common文件夾位於測試用例的包含路徑中。

你是否爲你的應用程序定製了任何類加載器,因爲你需要爲你的測試做同樣的事情。