PHPUnit TeaDownAfterClass 使用 ZendStudio 调用得太快

发布于 2024-10-28 17:16:12 字数 3899 浏览 3 评论 0原文

我在 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',
                '[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;
    }
}

I have a very strange problem with PHPUnit in Zend Studio. I have a unit test with two current test methods. I'm using setUpBeforeClass() and tearDownAfterClass() to initialise a database connection and to destroy it. The problem I have is that tearDownAfterClass() is called after the first test method. When setUp() is called after that there's an error due to the fact that no database connection then exists. I can't work out for the life of me what's going on. I've confirmed the problem by stepping through the test using ZendDebugger. I'm also using the version of PHPUnit embedded in ZendStudio. Here's the test code:

    <?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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-11-04 17:16:12

好的,我认为这是 ZendStudio 或与其捆绑的 PHPUnit 版本的问题。该测试通过 PEAR 在命令行上安装的 PHPUnit 3.5 运行良好。我已将测试重构为测试数据中的硬代码,而不是使用数据提供程序(我知道,一点也不理想),这暂时解决了问题。

OK, I think this is an issue with either ZendStudio or the version of PHPUnit bundled with it. The test runs fine via a PEAR installed PHPUnit 3.5 on the command line. I've refactored the test to hard code in the test data instead of using a data provider (I know, not ideal at all) and that's fixed things for the time being.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文