Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Oct 15, 2023
1 parent d6ade70 commit f9ff28e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ private function putAdded(JobInterface ...$job): void
private function storeReferences(string $job, JobInterface $item): void
{
$action = $item->actionName()->__toString();
// TODO: Support for mixed, void, ParameterAccessInterface
if ($action::acceptResponse() instanceof ParametersAccessInterface) {
foreach ($action::acceptResponse()->parameters() as $key => $parameter) {
$this->references = $this->references
->withPut(
strval(responseKey($job, $key)),
strval(response($job, $key)),
$parameter,
);
}
Expand Down
15 changes: 3 additions & 12 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,16 @@ function async(string $action, mixed ...$argument): JobInterface
}

/**
* Creates a reference to the response for the given job and key.
* Creates a reference to the response for the given job and key (if any).
*
* @param string $job Job
* @param ?string $key Job response key (optional)
*/
function responseKey(string $job, string $key): ResponseReferenceInterface
function response(string $job, ?string $key = null): ResponseReferenceInterface
{
return new ResponseReference($job, $key);
}

/**
* Creates a reference to the response for the given job.
*
* @param string $job Job
*/
function response(string $job): ResponseReferenceInterface
{
return new ResponseReference($job, null);
}

/**
* Creates a workflow variable.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Chevere\Workflow\Job;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function Chevere\Workflow\responseKey;
use function Chevere\Workflow\response;
use function Chevere\Workflow\variable;

final class JobTest extends TestCase
Expand Down Expand Up @@ -104,7 +104,7 @@ public function testReferenceResponseKey(): void
{
$actionName = new ActionName(TestActionParamStringRegex::class);
$success = [
'foo' => responseKey('job1', 'output'),
'foo' => response('job1', 'output'),
];
$job = new Job($actionName, true, ...$success);
$this->assertSame($actionName, $job->actionName());
Expand Down Expand Up @@ -174,7 +174,7 @@ public function testWithRunIfReference(): void
{
$actionName = new ActionName(TestActionNoParams::class);
$job = new Job($actionName);
$reference = responseKey('jobN', 'parameter');
$reference = response('jobN', 'parameter');
$job = $job->withRunIf($reference);
$this->assertSame(
[$reference],
Expand Down
26 changes: 13 additions & 13 deletions tests/JobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Chevere\Workflow\Jobs;
use PHPUnit\Framework\TestCase;
use function Chevere\Workflow\async;
use function Chevere\Workflow\responseKey;
use function Chevere\Workflow\response;
use function Chevere\Workflow\sync;
use function Chevere\Workflow\variable;

Expand Down Expand Up @@ -201,7 +201,7 @@ public function testWithReferenceShouldFail(): void
),
two: async(
TestActionParamFooResponse1::class,
foo: responseKey('one', 'bar')
foo: response('one', 'bar')
)
);
}
Expand All @@ -216,7 +216,7 @@ public function testMissingReference(): void
),
two: async(
TestActionParamFooResponseBar::class,
foo: responseKey('zero', 'key')
foo: response('zero', 'key')
)
);
}
Expand All @@ -231,8 +231,8 @@ public function testWrongReferenceType(): void
),
two: async(
TestActionParams::class,
foo: responseKey('one', 'id'),
bar: responseKey('one', 'id')
foo: response('one', 'id'),
bar: response('one', 'id')
)
);
}
Expand All @@ -243,7 +243,7 @@ public function testWithRunIfUndeclaredJob(): void
new Jobs(
j1: async(TestActionNoParams::class)
->withRunIf(
responseKey('job', 'parameter')
response('job', 'parameter')
),
);
}
Expand All @@ -255,7 +255,7 @@ public function testWithRunIfUndeclaredJobKey(): void
j1: async(TestActionNoParams::class),
j2: async(TestActionNoParams::class)
->withRunIf(
responseKey('j1', 'parameter')
response('j1', 'parameter')
),
);
}
Expand All @@ -268,7 +268,7 @@ public function testWithRunIfInvalidJobKeyType(): void
j1: async(TestActionNoParamsIntegerResponse::class),
j2: async(TestActionNoParams::class)
->withRunIf(
responseKey('j1', 'id')
response('j1', 'id')
),
);
}
Expand Down Expand Up @@ -313,8 +313,8 @@ public function testWithRunIfVariable(): void

public function testWithRunIfReference(): void
{
$true = responseKey('j1', 'true');
$false = responseKey('j1', 'false');
$true = response('j1', 'true');
$false = response('j1', 'false');
$jobs = new Jobs(
j1: async(
TestActionNoParamsBooleanResponses::class,
Expand All @@ -337,7 +337,7 @@ public function testWithRunIfReference(): void
$jobs->references()->has($true->__toString(), $false->__toString())
);
$j4 = async(TestActionNoParams::class)
->withRunIf(responseKey('j5', 'missing'));
->withRunIf(response('j5', 'missing'));
$this->expectException(OutOfBoundsException::class);
$jobs->withAdded(j4: $j4);
}
Expand All @@ -353,7 +353,7 @@ public function testWithMissingReference(): void
),
job2: async(
TestActionParamFooResponseBar::class,
foo: responseKey('job1', 'missing'),
foo: response('job1', 'missing'),
)
);
}
Expand All @@ -369,7 +369,7 @@ public function testWithInvalidTypeReference(): void
),
job2: async(
TestActionParamFooResponseBar::class,
foo: responseKey('job1', 'baz'),
foo: response('job1', 'baz'),
)
);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Chevere\Workflow\Runner;
use PHPUnit\Framework\TestCase;
use function Chevere\Workflow\async;
use function Chevere\Workflow\responseKey;
use function Chevere\Workflow\response;
use function Chevere\Workflow\run;
use function Chevere\Workflow\sync;
use function Chevere\Workflow\variable;
Expand Down Expand Up @@ -163,12 +163,12 @@ public function testRunnerForReferences(): void
);
$job2 = async(
TestActionParamsFooBarResponse2::class,
foo: responseKey('job1', 'response1'),
bar: responseKey('job1', 'response1'),
foo: response('job1', 'response1'),
bar: response('job1', 'response1'),
);
$job3 = sync(
TestActionParamFooResponse1::class,
foo: responseKey('job2', 'response2'),
foo: response('job2', 'response2'),
);
$jobs = [
'job1' => $job1,
Expand Down Expand Up @@ -226,8 +226,8 @@ public function testRunIfReference(): void
$job4 = async(TestActionNoParamsIntegerResponse::class);
$workflow = workflow(
job1: $job1,
job2: $job2->withRunIf(responseKey('job1', 'true')),
job3: $job3->withRunIf(responseKey('job1', 'true')),
job2: $job2->withRunIf(response('job1', 'true')),
job3: $job3->withRunIf(response('job1', 'true')),
job4: $job4->withDepends('job3')
);
$run = new Run($workflow);
Expand All @@ -238,8 +238,8 @@ public function testRunIfReference(): void
}
$workflow = workflow(
job1: $job1,
job2: $job2->withRunIf(responseKey('job1', 'true'), responseKey('job1', 'false')),
job3: $job3->withRunIf(responseKey('job1', 'true'), responseKey('job1', 'false')),
job2: $job2->withRunIf(response('job1', 'true'), response('job1', 'false')),
job3: $job3->withRunIf(response('job1', 'true'), response('job1', 'false')),
job4: $job1->withDepends('job3')
);
$run = new Run($workflow);
Expand Down

0 comments on commit f9ff28e

Please sign in to comment.