Codeception/Stub

Expected::exactly should have ability to change callback method per times called

Opened this issue · 0 comments

What are you trying to achieve?

Http client send method is called 2 times on a test
I would like to make different response objects for each of them
on one of them I would also like to have asserts

What do you get instead?

RN it is impossible to have different callbacks for consecutive calls

Provide console output if related. Use -vvv mode for more details.

Provide test source code if related

$oauth2Provider->setHttpClient($this->construct(Client::class, [], [
    'send' => Expected::exactly(2,
        function (RequestInterface $request) {
            return new Response(200, [
                'content-type' => 'application/json'
            ], '{"access_token": "access", "expires_in": 3600, "refresh_token": "refresh"}');
        },
        function (RequestInterface $request) {
            //assertions go here

            return new Response(200, [
            ], 'response body goes here');
        }
    ]))
]));

Details

  • Codeception version:
  • PHP Version: 7.4.6
  • Operating System: Ubuntu
  • Installation type: Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here

Addittional info
RN I do this

$oauth2Provider->setHttpClient($this->construct(Client::class, [], [
    'send' => Expected::exactly(2, Closure::fromCallable(new MultiCallable([
        function (RequestInterface $request) {
            return new Response(200, [
                'content-type' => 'application/json'
            ], '{"access_token": "access", "expires_in": 3600, "refresh_token": "refresh"}');
        },
        function (RequestInterface $request) {
            //asserts go here

            return new Response(200, [
            ], 'response body goes here');
        }
    ])))
]));
<?php

class MultiCallable
{
    private $methods;

    public function __construct($methods)
    {
        $this->methods = $methods;
    }

    public function __invoke(...$args)
    {
        $method = array_shift($this->methods);

        return call_user_func_array($method, $args);
    }
}