Reputation: 10908
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?
Upvotes: 2
Views: 866
Reputation: 4266
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.
Upvotes: 1
Reputation: 6538
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
Upvotes: 2