Codeception / Stub

Flexible Stub wrapper for PHPUnit's Mock Builder
MIT License
296 stars 17 forks source link

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

Open mikk150 opened 4 years ago

mikk150 commented 4 years ago

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

# 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);
    }
}