Skip to content

Commit db39a89

Browse files
authored
Merge pull request #348 from keboola/erik-SOX-391
JobResult supports variables
2 parents db88433 + a92f21f commit db39a89

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

src/Result/JobResult.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use JsonSerializable;
88
use Keboola\JobQueueInternalClient\Exception\ClientException;
99
use Keboola\JobQueueInternalClient\Result\InputOutput\TableCollection;
10+
use Keboola\JobQueueInternalClient\Result\Variable\Variable;
11+
use Keboola\JobQueueInternalClient\Result\Variable\VariableCollection;
1012

1113
class JobResult implements JsonSerializable
1214
{
@@ -23,6 +25,7 @@ class JobResult implements JsonSerializable
2325
private ?TableCollection $outputTables = null;
2426

2527
private ?JobArtifacts $artifacts = null;
28+
private ?VariableCollection $variables = null;
2629

2730
public function jsonSerialize(): array
2831
{
@@ -46,6 +49,9 @@ public function jsonSerialize(): array
4649
if ($this->artifacts) {
4750
$result['artifacts'] = $this->artifacts->jsonSerialize();
4851
}
52+
if ($this->variables) {
53+
$result['variables'] = $this->variables->jsonSerialize();
54+
}
4955
if ($this->errorType) {
5056
$result['error']['type'] = $this->errorType;
5157
}
@@ -151,4 +157,15 @@ public function getArtifacts(): ?JobArtifacts
151157
{
152158
return $this->artifacts;
153159
}
160+
161+
public function setVariables(VariableCollection $variables): JobResult
162+
{
163+
$this->variables = $variables;
164+
return $this;
165+
}
166+
167+
public function getVariables(): ?VariableCollection
168+
{
169+
return $this->variables;
170+
}
154171
}

src/Result/Variable/Variable.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Result\Variable;
6+
7+
use JsonSerializable;
8+
9+
class Variable implements JsonSerializable
10+
{
11+
public function __construct(
12+
private readonly string $name,
13+
private readonly string $value,
14+
) {
15+
}
16+
17+
public function getName(): string
18+
{
19+
return $this->name;
20+
}
21+
22+
public function getValue(): string
23+
{
24+
return $this->value;
25+
}
26+
27+
public function jsonSerialize(): array
28+
{
29+
return [
30+
'name' => $this->name,
31+
'value' => $this->value,
32+
];
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Result\Variable;
6+
7+
use Countable;
8+
use Generator;
9+
use IteratorAggregate;
10+
use JsonSerializable;
11+
12+
/**
13+
* @implements IteratorAggregate<Variable>
14+
*/
15+
class VariableCollection implements Countable, IteratorAggregate, JsonSerializable
16+
{
17+
/** @var Variable[] */
18+
private array $items = [];
19+
20+
/**
21+
* @return Generator<int, Variable>
22+
*/
23+
public function getIterator(): Generator
24+
{
25+
foreach ($this->items as $item) {
26+
yield $item;
27+
}
28+
}
29+
30+
public function count(): int
31+
{
32+
return count($this->items);
33+
}
34+
35+
public function jsonSerialize(): array
36+
{
37+
return array_map(function (Variable $variable) {
38+
return $variable->jsonSerialize();
39+
}, $this->items);
40+
}
41+
42+
public function addVariable(Variable $variable): self
43+
{
44+
$this->items[] = $variable;
45+
return $this;
46+
}
47+
}

tests/Result/JobResultTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Keboola\JobQueueInternalClient\Result\InputOutput\TableCollection;
1313
use Keboola\JobQueueInternalClient\Result\JobArtifacts;
1414
use Keboola\JobQueueInternalClient\Result\JobResult;
15+
use Keboola\JobQueueInternalClient\Result\Variable\Variable;
16+
use Keboola\JobQueueInternalClient\Result\Variable\VariableCollection;
1517
use PHPUnit\Framework\TestCase;
1618

1719
class JobResultTest extends TestCase
@@ -36,6 +38,9 @@ public function testAccessors(): void
3638

3739
$output = (new TableCollection())->addTable($outputTable);
3840

41+
$variable = new Variable('vault.foo', 'bar');
42+
$variables = (new VariableCollection())->addVariable($variable);
43+
3944
$jobResult = new JobResult();
4045
$jobResult
4146
->setConfigVersion('123')
@@ -45,6 +50,7 @@ public function testAccessors(): void
4550
->setExceptionId('exception-12345')
4651
->setInputTables($input)
4752
->setOutputTables($output)
53+
->setVariables($variables)
4854
->setArtifacts(
4955
(new JobArtifacts())
5056
->setUploaded([
@@ -65,6 +71,7 @@ public function testAccessors(): void
6571
self::assertSame('exception-12345', $jobResult->getExceptionId());
6672
self::assertSame($input, $jobResult->getInputTables());
6773
self::assertSame($output, $jobResult->getOutputTables());
74+
self::assertSame($variables, $jobResult->getVariables());
6875
self::assertSame(
6976
[
7077
'message' => 'test',
@@ -119,6 +126,12 @@ public function testAccessors(): void
119126
],
120127
],
121128
],
129+
'variables' => [
130+
[
131+
'name' => 'vault.foo',
132+
'value' => 'bar',
133+
],
134+
],
122135
'error' => [
123136
'type' => 'application',
124137
'exceptionId' => 'exception-12345',
@@ -139,6 +152,7 @@ public function testEmptyResult(): void
139152
self::assertNull($result->getMessage());
140153
self::assertNull($result->getInputTables());
141154
self::assertNull($result->getOutputTables());
155+
self::assertNull($result->getVariables());
142156

143157
self::assertSame([
144158
'message' => null,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Tests\Result\InputOutput;
6+
7+
use Keboola\JobQueueInternalClient\Result\Variable\Variable;
8+
use Keboola\JobQueueInternalClient\Result\Variable\VariableCollection;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class VariableCollectionTest extends TestCase
12+
{
13+
public function testCreate(): void
14+
{
15+
$collection = new VariableCollection();
16+
17+
self::assertSame(0, $collection->count());
18+
19+
$variable = new Variable('foo', 'bar');
20+
$collection->addVariable($variable);
21+
22+
self::assertSame(1, $collection->count());
23+
self::assertSame(
24+
[
25+
$variable,
26+
],
27+
iterator_to_array($collection->getIterator()),
28+
);
29+
self::assertSame([
30+
[
31+
'name' => 'foo',
32+
'value' => 'bar',
33+
],
34+
], $collection->jsonSerialize());
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\JobQueueInternalClient\Tests\Result\InputOutput;
6+
7+
use Keboola\JobQueueInternalClient\Result\InputOutput\Column;
8+
use Keboola\JobQueueInternalClient\Result\InputOutput\ColumnCollection;
9+
use Keboola\JobQueueInternalClient\Result\InputOutput\Table;
10+
use Keboola\JobQueueInternalClient\Result\Variable\Variable;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class VariableTest extends TestCase
14+
{
15+
public function testCreate(): void
16+
{
17+
$table = new Variable('vault.foo', 'bar');
18+
19+
self::assertSame('vault.foo', $table->getName());
20+
self::assertSame('bar', $table->getValue());
21+
22+
self::assertSame([
23+
'name' => 'vault.foo',
24+
'value' => 'bar',
25+
], $table->jsonSerialize());
26+
}
27+
}

0 commit comments

Comments
 (0)