سؤال

I have a method that is supposed to use an array it gets somehow, and then call another method (lets call it fooBar) for each element, passing the element as argument to the method.

This is the code I currently I have, and works:

    foreach ( $names as $index => $name ) {
        $parser->expects( $this->at( $index ) )
            ->method( 'setFunctionHook' )
            ->with(
                $this->equalTo( $name ),
                $this->isType( 'callable' )
            );
    }

This is quite verbose though. Is there a shorter way of doing this?

هل كانت مفيدة؟

المحلول

Assuming you know what values to expect in your test:

foreach($expected_values as $value){
    $myMock->expects( $this->once())
        ->method('fooBar')
        ->with($value);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top