如何在 PHPUnit 中模拟返回新类的参数?

发布于 11-14 09:12 字数 1417 浏览 2 评论 0原文

我遇到这样的代码行问题:

$user->has('roles', ORM::factory('role', array('name' => 'unverified')))

我可以模拟第一个参数,但只能断言第二个参数返回一个类。在某些课程中,我多次使用 has 并且需要能够正确测试它们。为了清楚起见,我需要确认“未经验证”已传递到第二个参数的工厂方法中。非常感谢任何帮助。

我正在测试的类:

<?php
/**
 * Policy class to determine if a user can upload a file.
 */
class Policy_File_Upload extends Policy
{
    const NOT_LOGGED_IN = 1;
    const NOT_VERIFIED = 2;

    public function execute(Model_ACL_User $user, array $extra = NULL)
    {
        if ($user->can('login'))
        {
            return self::NOT_LOGGED_IN;
        }
        elseif ($user->has('roles', ORM::factory('role', array('name' => 'unverified'))))
        {
            return self::NOT_VERIFIED;
        }

        return TRUE;
    }
}

以及相应的测试:

public function test_guest()
{
    // User mock
    $user = $this->getMock('Model_User', array('can', 'has'), array(), '', FALSE);
    $user->expects($this->any())->method('can')->with('login')->will($this->returnValue(TRUE));
    $user->expects($this->any())->method('has')->with('roles', $this->logicalOr
    (

    ))
    ->will($this->returnCallback(array($this, 'roles')));


    $policy = new Policy_File_Upload;

    $this->assertSame(Policy_File_Upload::NOT_VERIFIED, $policy->execute($user));
}

I'm having a problem with a line of code like:

$user->has('roles', ORM::factory('role', array('name' => 'unverified')))

I can mock the first argument, but can only assert that the 2nd argument returns a class. In some classes I make multiple uses of has and need to be able to test them properly. To be clear, I need to confirm that "unverified" was passed into the factory method of the 2nd argument. Any help is much appreciated.

The class I'm testing:

<?php
/**
 * Policy class to determine if a user can upload a file.
 */
class Policy_File_Upload extends Policy
{
    const NOT_LOGGED_IN = 1;
    const NOT_VERIFIED = 2;

    public function execute(Model_ACL_User $user, array $extra = NULL)
    {
        if ($user->can('login'))
        {
            return self::NOT_LOGGED_IN;
        }
        elseif ($user->has('roles', ORM::factory('role', array('name' => 'unverified'))))
        {
            return self::NOT_VERIFIED;
        }

        return TRUE;
    }
}

and the corresponding test:

public function test_guest()
{
    // User mock
    $user = $this->getMock('Model_User', array('can', 'has'), array(), '', FALSE);
    $user->expects($this->any())->method('can')->with('login')->will($this->returnValue(TRUE));
    $user->expects($this->any())->method('has')->with('roles', $this->logicalOr
    (

    ))
    ->will($this->returnCallback(array($this, 'roles')));


    $policy = new Policy_File_Upload;

    $this->assertSame(Policy_File_Upload::NOT_VERIFIED, $policy->execute($user));
}

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

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

发布评论

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

评论(3

忘羡2024-11-21 09:12:19

您始终可以采用数据驱动的路线,为每种情况准备测试数据并验证调用函数的结果。最后,您不太关心 'unverified' 是否传递给 ORM::factory() ,而是关心 得出正确的结果根据输入执行()

测试实现细节会使测试变得脆弱且难以编写。如果您可以测试结果,那么当您更改得出结果的方式时,您的测试就不会中断。为所有用例整理数据集可能需要更长的时间,但通常可以在测试之间共享。

You can always go the data-driven route of preparing test data for each case and verify the result of calling the function. In the end, you don't so much care that 'unverified' gets passed to ORM::factory() but rather that the correct result comes out of execute() based on the input.

Testing the implementation details makes the test brittle and difficult to write. If you can test the results instead, your tests won't break when you change how you arrive at the result. It may take longer to put together a data set for all your use cases, but it can generally be shared among tests.

梦断已成空2024-11-21 09:12:19

为了清楚起见,我需要确认“未经验证”已传递到第二个参数的工厂方法中。

<?php

public function execute(Model_ACL_User $user, array $extra = NULL)
    [...] ORM::factory('role', array('name' => 'unverified')) [...]

对不起,伙计,你不能这样做,因为没有(合理的)方法来模拟静态方法调用。

关于为什么这是一个问题的论点与 Sebastian Bergmann 的一篇博客文章类似:Testing-Code-That-Uses-Singletons

有诸如 runkit 您可以选择替换“ORM”类在运行时使用模拟版本,但这真的很痛苦。

因此有几个选项:

您可以创建类似 Policy_File_Upload_Data_Provider 的内容,其中包含 ->getRoles放置 ORM 访问权限的方法。这将允许您很好地测试您的业务逻辑,并且测试数据访问类应该更容易一些,因为它并没有那么多(只是访问 orm)。

你可以注入你的 ORM 类(或者如果这不起作用,也许只是它的名字)。
根据类本身,您应该能够创建一个实例并调用 ->factory,就像它是一个普通方法一样。你可以嘲笑这一点。

如果你不想这样做,那么把它留在里面也是一个不错的选择。

希望这至少给了您一个想法/概述。


为什么很难测试静态的其他链接:

Misko Hevery - 缺陷:脆弱的全局状态&单例

Kore Nordmann - 静态被认为有害

To be clear, I need to confirm that "unverified" was passed into the factory method of the 2nd argument.

<?php

public function execute(Model_ACL_User $user, array $extra = NULL)
    [...] ORM::factory('role', array('name' => 'unverified')) [...]

I'm sorry mate, you can't do that as there is no (sane) way to mock out a static method call.

The argument to why this is a problem are similar to one of Sebastian Bergmanns blog post: Testing-Code-That-Uses-Singletons

There are options like runkit that you could choose to replace the "ORM" class at runtime with a mocked version but thats really painfull.

So there are a couple of options:

You could create something like a Policy_File_Upload_Data_Provider that has a ->getRoles method where you put your ORM access. That would allow you to test your business logic nicely and testing that data access class should be a little easier as it doesn't to that much (just accessing the orm).

You could Inject your ORM class (or if that doesn't work out maybe just its name).
Depending on the class its self you should be able to create an instance and call ->factory like it would be a normal method. You could mock that.

If you don't want that just leaving that in there is also a not SO bad option.

Hope that at least gave you an idea/overview.


Additional Links why it's hard to test statics:

Misko Hevery - Flaw: Brittle Global State & Singletons

Kore Nordmann - Static considered harmful

冷情2024-11-21 09:12:19

实际上,您可以利用 参数捕获 对传递给模拟方法的参数进行验证。 a href="http://github.com/mlively/Phake" rel="nofollow" title="Phake Github Page">PHP 的 Phake 模拟框架。

public function test_guest()
{
    // User mock
    $user = Phake::mock('Model_User');
    when($user)->can('login')->thenReturn(FALSE);
    when($user)->has('roles', $this->anything())->thenGetReturnByLambda(array($this, 'roles'));

    $policy = new Policy_File_Upload;

    $this->assertSame(Policy_File_Upload::NOT_VERIFIED, $policy->execute($user));

    Phake::verify($user)->has('roles', Phake::capture($factoriedRole));

    $this->assertEquals('unverified', $factoriedRole->get('name'));
}

此示例假设您能够检查 $factoriedRole 上的名称,显然该部分需要根据 ORM 堆栈的工作方式进行更改。

一个小注释,我同意 edorian 关于静力学的观点。如果可能的话,我总是更喜欢重新设计代码而不需要静态,但有时这是不可能的,在这种情况下,这是一个可行的解决方案。

Phake Github 页面

Phake 参数捕获文档

You can actually do verification on parameters passed to mock methods utilizing parameter capturing in the Phake mocking framework for PHP.

public function test_guest()
{
    // User mock
    $user = Phake::mock('Model_User');
    when($user)->can('login')->thenReturn(FALSE);
    when($user)->has('roles', $this->anything())->thenGetReturnByLambda(array($this, 'roles'));

    $policy = new Policy_File_Upload;

    $this->assertSame(Policy_File_Upload::NOT_VERIFIED, $policy->execute($user));

    Phake::verify($user)->has('roles', Phake::capture($factoriedRole));

    $this->assertEquals('unverified', $factoriedRole->get('name'));
}

This example is assuming that you have the ability to check the name on $factoriedRole, obviously that piece needs to be changed depending on how that ORM stack works.

A small note, I agree with edorian regarding statics. If possible I would always prefer redesigning your code to not require statics, but sometimes that isn't possible, in which case this is a workable solution.

Phake Github Page

Phake Documentation on Parameter Capturing

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