Конкатенация ограничений в phpunit - PullRequest
7 голосов
/ 13 мая 2011

Мой вопрос, как я объединяю ограничения в разделе с phpunit? В фиктивном примере:

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

Параметр метода incrementValue должен быть больше 0, но если мне нужно оценить, этот параметр должен быть меньше 10.

Как я объединяю $this->lessThan(10)?

1 Ответ

9 голосов
/ 13 мая 2011

Вы можете использовать выражение logicalAnd:

$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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...