2011-04-01 67 views
0

在Zend Studio中,我對PHPUnit有一個非常奇怪的問題。我有兩種當前的測試方法進行單元測試。我使用setUpBeforeClass()和tearDownAfterClass()來初始化數據庫連接並銷燬它。我遇到的問題是在第一個測試方法之後調用tearDownAfterClass()。當之後調用setUp()時,由於沒有數據庫連接,因此存在錯誤。我無法解決我的生活發生了什麼事。我通過使用ZendDebugger進行測試來確認問題。我也使用ZendStudio中嵌入的PHPUnit版本。下面是測試代碼:PHPUnit tearDownAfterClass在ZendStudio中調用得太快

<?php 
require_once '../Code\includes\classes\datamapper\basedatamapper.class.php'; 
require_once '../Code\includes\classes\datamapper\userdatamapper.php'; 
require_once '../Code\includes\classes\users\user.class.php'; 
require_once 'PHPUnit\Framework\TestCase.php'; 

use DataMapper\UserDataMapper, Users\User; 

/** 
* UserDataMapper test case. 
*/ 
class UserDataMapperTest extends PHPUnit_Framework_TestCase { 
    /** 
    * @var UserDataMapper 
    */ 
    private $UserDataMapper; 

    protected static $db; 

    static public function setUpBeforeClass() { 
     self::$db = new PDO('mysql:host=localhost;dbname=cfoundtest', 'root', 'Andrea30Cook'); 
     self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
    /** 
    * Prepares the environment before running a test. 
    */ 
    protected function setUp() { 
     parent::setUp(); 
     $this->UserDataMapper = new UserDataMapper(self::$db, simplexml_load_file(dirname(__DIR__) . '/Code/mapping/userMapping.xml')); 
    } 
    /** 
    * @dataProvider UserGenerator 
    */ 
    public function testSaveNewUser($firstname, $lastname, $password, $username, $userType, $email) { 
     $user = new User(); 
     $user->setFirstname($firstname) 
       ->setLastname($lastname) 
       ->setPassword($password) 
       ->setUsername($username) 
       ->setUserType($userType) 
       ->setEmail($email); 
     $this->assertTrue($this->UserDataMapper->save($user)); 
     $this->assertType('int', $user->getId()); 
     $this->assertType('string', $user->getConfString()); 
     $this->assertSame(40, strlen($user->getConfString())); 
    } 
    /** 
    * @dataProvider UserGenerator 
    */ 
    public function testAddingUserException($firstname, $lastname, $password, $username, $userType, $email) { 
     $user = new User(); 
     $user->setFirstname($firstname) 
       ->setLastname($lastname) 
       ->setPassword($password) 
       ->setUsername($username) 
       ->setUserType($userType) 
       ->setEmail($email); 
     $this->setExpectedException('InvalidArgumentException', sprintf('Username "%s" is already taken in DataMapper\UserDataMapper::insert', $user->getUsername())); 
     $this->UserDataMapper->save($user); 
    } 

    public static function UserGenerator() { 
     return array(
      array(
       'Foo', 
       'Bar', 
       'Secret Test', 
       'Foobar', 
       'Authorizer', 
       '[email protected]' 
      ), 
      array(
       'Fred', 
       'Flintstone', 
       'Secret Test', 
       'BarBaz', 
       'Admin', 
       '[email protected]' 
      ) 
     ); 
    } 
    /** 
    * Cleans up the environment after running a test. 
    */ 
    protected function tearDown() { 
     // TODO Auto-generated UserDataMapperTest::tearDown() 


     $this->UserDataMapper = null; 

     parent::tearDown(); 
    } 

    static public function tearDownAfterClass() { 
     self::$db->exec('DELETE FROM failedlogins'); 
     self::$db->exec('DELETE FROM users'); 
     self::$db = NULL; 
    } 
} 

回答

0

OK,我想這是任何ZendStudio或與它捆綁PHPUnit的版本的問題。該測試通過在命令行上安裝的PHPUnit 3.5的PEAR運行良好。我已經將測試重構爲測試數據中的硬代碼,而不是使用數據提供者(我知道,並不理想),並且這是暫時的問題。