PHPUnit,使用类常量更改状态

发布于 2024-10-31 06:14:38 字数 1197 浏览 1 评论 0原文

我正在学习 Zend 和 PHPUnit。

正如您在此处看到的


public function changeToIllegalState()
{
    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

/**
 * @dataProvider changeToIllegalState
 * @expectedException IllegalStateChangeException
 */

public function testIllegalStateChangeGeneratesException( $state )
{
    $mapper = new Application_Model_Mapper_SomeModel();
    $model = new Application_Model_SomeModel();

    $model->changeState( $state );

    $mapper->save( $model );

}

,数据提供者提供了一些表示模型中不同状态的常量。

PHPUnit 说它在 dataprovider 方法中找不到 Model 类。但是,如果我尝试在测试方法中使用常量,则一切正常并且没有问题。我正在使用 Zend 自动加载器来加载我的类,到目前为止一切都很顺利。我知道我可以只输入常量本身的值,但我不知道为什么会出现此错误。

我只能假设在调用 setup 方法之前调用 dataprovider 方法,因为我在 setup 方法中完成了所有自动加载业务。

编辑:

我也尝试了以下方法,但它仍然无法与类常量一起使用。



protected $_FAIL;
protected $_model;

public function setUp()
{
    parent::setUp();
    $this->_model = new Application_Model_SomeModel();
    $this->_FAIL = Application_Model_SomeModel::FAIL;
}

现在,当我尝试在提供程序方法中使用 $_FAIL 时,我得到一个 NULL 值,而不是我期望的“失败”字符串。这实在是太奇怪了。

I'm learning Zend and also PHPUnit.

Here is what I have below


public function changeToIllegalState()
{
    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

/**
 * @dataProvider changeToIllegalState
 * @expectedException IllegalStateChangeException
 */

public function testIllegalStateChangeGeneratesException( $state )
{
    $mapper = new Application_Model_Mapper_SomeModel();
    $model = new Application_Model_SomeModel();

    $model->changeState( $state );

    $mapper->save( $model );

}

So as you can see here, the data provider provides some constants that represent different states from the model.

PHPUnit says that it can't find the Model class in the dataprovider method. However, if I try to use the constants within the test methods, it all works and there are no problems. I'm using the Zend autoloader to load my classes and it has all been dandy up till now. I know that I could just put in the values for the constants themselves, but I don't know why I'm getting this error.

I can only assume that the dataprovider methods are called before the setup method is called because I do all the autoloading business in the setup method.

EDIT :

I have also tried the following but it still won't work with the class consts.



protected $_FAIL;
protected $_model;

public function setUp()
{
    parent::setUp();
    $this->_model = new Application_Model_SomeModel();
    $this->_FAIL = Application_Model_SomeModel::FAIL;
}

Now, when I try to use $_FAIL in the provider method I get a NULL value instead of the 'fail' string that I'm expecting. This is really weird.

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

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

发布评论

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

评论(2

请远离我 2024-11-07 06:14:38

PHPUnit 在运行任何测试之前实例化将运行的所有测试用例。

  • 每个测试方法一个,无需数据提供者。
  • 每个数据提供者方法一个。
  • 每个数据提供者返回一个参数数组,因此返回四个数组的提供者获得四个测试用例实例。

假设您在 bootstrap.php 中设置自动加载器,它应该加载包含这些常量的类。但是,我会尝试进行测试来查看:

public function changeToIllegalState()
{
    require_once 'Zend/Loader/Autoloader';
    Zend_Loader_Autoloader::getInstance();

    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

或者 Zend Framework 是否将模型目录添加到测试用例的 setUp() 方法之一的包含路径中?

PHPUnit instantiates all of test cases that will be run before running any tests.

  • One per test method without a data provider.
  • One per data provider method.
  • One per parameter array returned from each data provider, so a provider that returns an array of four arrays gets four test case instances.

Assuming you're setting up the autoloader in your bootstrap.php, it should load the class containing those constants. However, I would try a test to see:

public function changeToIllegalState()
{
    require_once 'Zend/Loader/Autoloader';
    Zend_Loader_Autoloader::getInstance();

    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

Or is Zend Framework adding the models' directory to the include path in one of the test case's setUp() method instead?

梦初启 2024-11-07 06:14:38

我终于明白发生了什么事。

PHPUnit 将在调用任何设置方法之前调用类的所有 dataprovider 方法,这包括静态方法 setUpBeforeClass。

我在数据提供程序方法和 setUp 方法中放入了一堆 echo 语句来确认这一点。所有提供者方法回显总是在设置方法回显之前打印。

此外,仅当使用 dataprovider 注释将方法声明为数据提供者时,才会回显提供者方法的回显。也就是说,任何未通过注释声明为 dataprovider 方法的方法根本不会被调用。

我最初关于 PHPUnit 在我的 setUp 方法之前调用提供者方法的假设是正确的。

为了解决这个问题,我必须在引导文件中而不是在 setUp 方法中实例化我的 Zend_Application 对象。我不想这样做,因为我不需要所有测试用例的 Zend_Application 对象,但我认为这就是我最终要做的,因为它会让我的生活更轻松一些。

@David Harkness,在这种情况下,您在引导程序中设置自动加载器是正确的,但我还需要设置应用程序自动加载(通过创建 Zend_Application 对象),而不仅仅是 Zend 库自动加载。

I figured out what is going on, finally.

PHPUnit will call all the dataprovider methods of a class before it calls any of the setup methods, this includes the static method setUpBeforeClass.

I put in a bunch of echo statements in the data provider methods and setUp methods to confirm this. All the provider method echos always printed before setup method echos.

In addition, the echos of provider methods were only echoed when the methods were declared as data providers with the dataprovider annotation. That is to say, any methods not declared as dataprovider methods with the annotation are not called at all.

My initial assumption about PHPUnit calling the provider methods before my setUp method was right.

To fix this, I have to instantiate my Zend_Application object in the bootstrap file instead of in the setUp method. I didn't want to do this because I didn't need the Zend_Application object for all the test cases but I think that this is what I'll end up doing since it'll make my life a little easier.

@David Harkness you were right about setting up the autoloader in the bootstrap in this case but I also needed to setup the application autoloading (by making the Zend_Application object), not just the Zend library autoloading.

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