Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test doesn't fail when expected amount of arguments on mocked object function call is less then actual #1097

Closed
svscorp opened this issue Jan 7, 2014 · 1 comment

Comments

@svscorp
Copy link

svscorp commented Jan 7, 2014

Hi,

I have an use case:

class A {
    public function B()
    {
         $foo = $userModel->findByIdAndName(123, 'ilia');

         return $foo
    }
}

--------------------------
// Unit Test

public function testA()
{
    $userModelMock->getMock(...);
    $userModelMock->expects($this->any())->method('findByIdAndName')->with(123, 'ilia')->will($this->returnValue(true));

    // some more logic
}

will pass the test.

Case #2: We have removed one argument from findByIdAndName in class A. So we have $userModel->findByIdAndName(123);
Test will failed because of too much parameters was expected, but actually got 1

Case #3: We have added one more argument to findByIdAndName in class A. So we have $userModel->findByIdAndName(123, 'ilia', 'whatdeheg');
Test will pass. So if someone will add one more parameter to a call inside a function he won't notice anything, because test will pass. But 'contract' is broken, right?

How do I make sure method is called with exact amount of arguments I expected? I want to test fail if amount of arguments more or less then expected.

@whatthejeff
Copy link
Contributor

This is a duplicate of sebastianbergmann/phpunit-mock-objects#32.

We don't have any built-in way for doing this at the moment, but you could use something like returnCallback() to throw an exception if the argument count is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants