Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate constraints in phpunit

Tags:

php

phpunit

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)?

like image 672
doctore Avatar asked May 13 '11 11:05

doctore


1 Answers

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
like image 78
edorian Avatar answered Sep 23 '22 17:09

edorian