在 phpunit 中连接约束

发布于 2024-11-07 00:56:03 字数 356 浏览 4 评论 0原文

我的问题是,如何将子句中的约束与 phpunit 连接起来? 在虚拟示例中:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->greaterThan (0)
     ->will ($this->returnValue (null));

方法 increaseValue 的参数必须大于 0,但如果我需要评估该参数必须小于 10。

如何连接 $this->lessThan (10)

My question is, how I concatenate constrains in the clausule with of phpunit?
In the dummy example:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->greaterThan (0)
     ->will ($this->returnValue (null));

The parameter of the method increaseValue must be greater than 0, but If I need evaluate that this parameter must be less than 10.

How I concatenate $this->lessThan(10)?

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

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

发布评论

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

评论(1

请止步禁区 2024-11-14 00:56:03

您可以使用 ticalAnd 表达式:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
     ->will ($this->returnValue (null));

有关可能的函数列表,请检查以下函数: PHPUnit/Framework/Assert.php 不以“assert”开头

完整示例

<?php

class mockMe {
    public function increaseValue($x) {
    }
}


class fooTest extends PHPUnit_Framework_TestCase {

    public function testMock() {
        $test = $this->getMock('mockMe');
        $test->expects($this->once())
             ->method('increaseValue')
             ->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
             ->will($this->returnValue(null));
        $test->increaseValue(6);
    }

    public function testMockFails() {
        $test = $this->getMock('mockMe');
        $test->expects($this->once())
             ->method('increaseValue')
             ->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
             ->will($this->returnValue(null));
        $test->increaseValue(12);
    }

}

结果

 phpunit blub.php
PHPUnit 3.5.13 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 4.25Mb

There was 1 failure:

1) fooTest::testMockFails
Expectation failed for method name is equal to <string:increaseValue> when invoked 1 time(s)
Parameter 0 for invocation mockMe::increaseValue(<integer:12>) does not match expected value.
Failed asserting that <integer:12> is less than <integer:10>.

/home/.../blub.php:26

You can use the logicalAnd expression:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
     ->will ($this->returnValue (null));

For a list of possible functions check the functions in: PHPUnit/Framework/Assert.php that don't start with "assert"

Complete Example

<?php

class mockMe {
    public function increaseValue($x) {
    }
}


class fooTest extends PHPUnit_Framework_TestCase {

    public function testMock() {
        $test = $this->getMock('mockMe');
        $test->expects($this->once())
             ->method('increaseValue')
             ->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
             ->will($this->returnValue(null));
        $test->increaseValue(6);
    }

    public function testMockFails() {
        $test = $this->getMock('mockMe');
        $test->expects($this->once())
             ->method('increaseValue')
             ->with($this->logicalAnd($this->greaterThan(0), $this->lessThan(10)))
             ->will($this->returnValue(null));
        $test->increaseValue(12);
    }

}

Result

 phpunit blub.php
PHPUnit 3.5.13 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 4.25Mb

There was 1 failure:

1) fooTest::testMockFails
Expectation failed for method name is equal to <string:increaseValue> when invoked 1 time(s)
Parameter 0 for invocation mockMe::increaseValue(<integer:12>) does not match expected value.
Failed asserting that <integer:12> is less than <integer:10>.

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