在phpunit中连接约束

doc*_*ore 7 php phpunit

我的问题是,我如何连接phpunit的clausule中的约束?在虚拟示例中:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->greaterThan (0)
     ->will ($this->returnValue (null));
Run Code Online (Sandbox Code Playgroud)

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

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

edo*_*ian 9

您可以使用以下logicalAnd表达式:

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

有关可能的函数列表,请检查以下函数: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);
    }

}
Run Code Online (Sandbox Code Playgroud)

结果

 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
Run Code Online (Sandbox Code Playgroud)