我在Zend Studio中的PHPUnit有一个非常奇怪的问题。我有一个单元测试,有两个当前的测试方法。我正在使用setUpBeforeClass()和tearDownAfterClass()初始化数据库连接并销毁它。我遇到的问题是在第一个测试方法之后调用tearDownAfterClass()。之后调用setUp()时会出现错误,因为当时不存在数据库连接。我无法为我的生活做些什么。我已经通过使用ZendDebugger逐步完成测试来证实了这个问题。我也在使用ZendStudio中嵌入的PHPUnit版本。这是测试代码:
<?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',
'example@example.com'
),
array(
'Fred',
'Flintstone',
'Secret Test',
'BarBaz',
'Admin',
'example2@example.com'
)
);
}
/**
* 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 :(得分:0)
好的,我认为这是ZendStudio或与之捆绑的PHPUnit版本的问题。通过在命令行上安装PHPUnit 3.5的PEAR,测试运行良好。我已经对测试数据中的硬代码进行了重构测试,而不是使用数据提供程序(我知道,根本不理想)并且暂时修复了这些问题。