sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

[Feature Request] Set the expectCompletion result for each Activity call inside the loop

Open num213 opened this issue 2 years ago • 2 comments

Is your feature request related to a problem? Please describe.

For PHP-SDK we can use ActivityMocker and expectCompletion() to mock an activity call result. And it works great if we don't call the activity inside the loop.

If we call it inside the loop than we can't set expectCompletion() for each of the Activity call inside the loop.

For example consider we have the following phpunit test:

public function testWorkflowSend()
{
    $workflow = $this->workflowClient->newWorkflowStub(SomeWorkflow::class);
    $run = $this->workflowClient->start($workflow, ['value1', 'value2', 'value3']);
    $this->assertSame(['someResult1', 'someResult2', 'someResult3'], $run->getResult('array'));
}

The workflow method:

public function send(array $values)
{
    $results = [];
    foreach ($values as $value) {
        $results[] = yield $this->activity->doSomething($value);
    }
    return $results;
}

And we want to mock the activity calls like this:

$this->activityMocks->expectCompletion(
    'SomeActivity.doSomething', ['someResult1', 'someResult2', 'someResult3']
);

The result of running the workflow inside this test will be:

array(3) {
[0] => array(3) {
    [0] => string(11) "someResult1",
    [1] => string(11) "someResult2",
    [2] => string(11) "someResult3",
}, 
[1] => array(3) {
    [0] => string(11) "someResult1",
    [1] => string(11) "someResult2",
    [2] => string(11) "someResult3",
}, 
[2] => array(3) {
    [0] => string(11) "someResult1",
    [1] => string(11) "someResult2",
    [2] => string(11) "someResult3",
}

Instead of:

array(9) {
    [0] => string(11) "someResult1",
    [1] => string(11) "someResult2",
    [2] => string(11) "someResult3",
}

Even if we mock the activity calls like this:

$this->activityMocks->expectCompletion('SomeActivity.doSomething', 'someResult1');
$this->activityMocks->expectCompletion('SomeActivity.doSomething', 'someResult2');
$this->activityMocks->expectCompletion('SomeActivity.doSomething', 'someResult3');

The result will be:

array(9) {
    [0] => string(11) "someResult3",
    [1] => string(11) "someResult3",
    [2] => string(11) "someResult3",
}

Describe the solution you'd like

It will be great if we have a choice to set expectCompletion result for each call of the Activity inside the loop. For example:

$this->activityMocks->expectCompletion(
    'SomeActivity.doSomething', 'someResult1', 1
); // for the first call

$this->activityMocks->expectCompletion(
    'SomeActivity.doSomething', 'someResult2', 2
); // for the second

$this->activityMocks->expectCompletion(
    'SomeActivity.doSomething', 'someResult3', 3
); // for the third

And the same with expectFailure():

$this->activityMocks->expectCompletion(
    'SomeActivity.doSomething', 'someResult1', 1
); // for the first call
$this->activityMocks->expectFailure(
    'SomeActivity.doSomething', new Error(), 2
); // except error for the second call

num213 avatar Apr 27 '23 11:04 num213

any news about this?

rafaelemi avatar Apr 29 '24 11:04 rafaelemi

Hello. There are no updates yet, but the task of improving testing tools is included in the roadmap.

roxblnfk avatar Apr 29 '24 14:04 roxblnfk