2

I have an adapter that has a method taking a variable list of arguments and forwards it to a method that takes those same arguments in a framework I am using. I want to test that my adapter correctly forwards the arguments. I however do not want my test to know about which kind of arguments the framework supports.

I have a working expects as follows:

$context->expects( $this->once() )
    ->method( 'msg' )
    ->with(
        $this->equalTo( $someMessageArguments[0] ),
        $this->equalTo( $someMessageArguments[1] ),
        $this->equalTo( $someMessageArguments[2] )
    );

This is clearly not good as it assumes the length of the variable list is 3. I want to use a data provider and test with different lengths as well, in which case this code won't cut it.

Is there a sane way to do this via the PHPUnit API? I hacked this up, which also works, though seems quite evil as well:

$invocationMocker = $context->expects( $this->once() )
    ->method( 'msg' );

$invocationMocker->getMatcher()->parametersMatcher
    = new PHPUnit_Framework_MockObject_Matcher_Parameters( $someMessageArguments );

In case there is no way to do this nicely via the PHPUnit API, is there an approach that is better then the two listed here?

2 Answers 2

2

I haven't found the way to do it with PHPUnit mocks.

Using Mockery, you can archieve that:

$mock->shouldReceive('msg')
     ->once()
     ->withArgs($someMessageArguments);

Personally I normally prefer Mockery over PHPUnit mocks.

https://github.com/padraic/mockery

Sign up to request clarification or add additional context in comments.

Comments

1

I needed to do something similar with withConsecutive.

After building an array of my arguments for consecutive calls, the following was used.

$invocationMocker->getMatcher()->setParametersMatcher(new Matcher\ConsecutiveParameters($arguments));

The PHPUnit interface and namespace has changed a little since the original post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.