onConsecutiveCalls takes array, or similar function that takes one #79

Closed
MarkRoddy opened this Issue Jan 27, 2012 · 6 comments

Comments

Projects
None yet
6 participants

The onConsecutiveCalls() method takes a variable number of arguments, and it would be useful if it could also take a single argument that was an array of values that should be consecutively returned. This is especially useful if you have a set of similar tests and want to set up a stub to share between them, but they need to return different values. Something like:

    class SomeTest extends PHPUnit_Framework_TestCase {
        public function createStubSomeObj($retvals) {
            $stub = $this->getMockBuilder("SomeObj")
                ->setMethods(array("foo"))
                ->getMock();
            $sut->expects($this->any())
                ->method('foo') 
            ->will($this->onConsecutiveCalls($retvals));
   }

Right now to accomplish this you need to use call_user_func_array on $this->onConsecutiveCalls which is confusing to look and drops the use of the fluent interface.

Collaborator

edorian commented Jan 27, 2012

I'll get around to looking at your issues/feature requests? in the next couple of days. So long can you please give http://github.github.com/github-flavored-markdown/ another look? :) I don't want to guess what you are trying to point out exactly in the code samples when reformatting

Sorry about the formatting!

pscheit commented Sep 22, 2012

would love that, too. Allthough the wanted behavior could be generated with returnCallback but this would be more expressive.
But I have no idea how to don't break the api here

Quick solution by means of PHP's reflection capabilities:

->will(call_user_func_array(array($this, 'onConsecutiveCalls'), $retvals));

spoage commented Mar 21, 2013

Another, cleaner way of doing this is as follows:

->will(new PHPUnit_Framework_MockObject_Stub_ConsecutiveCalls($returnValueArray));
Owner

sebastianbergmann commented Oct 2, 2015

Dear contributor,

let me start by apologizing for not commenting and/or working on the issue you have reported or merging the pull request you have sent sooner.

PHPUnit 5.0 was released today. And today I am closing all open bug reports and pull requests for PHPUnit and its dependencies that I maintain. Please do not interpret the closing of this ticket as an insult or a lack of interest in your problem. I am sorry for any inconvenience this may cause.

If the topic of this ticket is still relevant then please open a new ticket or send a new pull request. If your ticket or pull request is about a defect then please check whether the issue still exists in PHPUnit 4.8 (which will received bug fixes until August 2016). If your ticket or pull request is about a new feature then please port your patch PHPUnit 5.0 before sending a new pull request.

I hope that today's extreme backlog grooming will allow me to respond to bug reports and pull requests in a more timely manner in the future.

Thank you for your understanding,
Sebastian

@sebastianbergmann sebastianbergmann locked and limited conversation to collaborators Oct 2, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.