Skip to content

Commit 01f21f5

Browse files
committed
refactor: use DateTimeInterface for job start & end time and accept null times
1 parent 1182e1c commit 01f21f5

6 files changed

Lines changed: 103 additions & 74 deletions

File tree

src/Requests/PostEvent/JobData.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace Keboola\NotificationClient\Requests\PostEvent;
66

7+
use DateTimeInterface;
78
use JsonSerializable;
89

910
class JobData implements JsonSerializable
1011
{
1112
private string $jobId;
1213
private string $jobUrl;
13-
private string $jobStartTime;
14-
private string $jobEndTime;
14+
private ?DateTimeInterface $jobStartTime;
15+
private ?DateTimeInterface $jobEndTime;
1516
private string $componentId;
1617
private string $componentName;
1718
private ?string $configurationId;
@@ -20,8 +21,8 @@ class JobData implements JsonSerializable
2021
public function __construct(
2122
string $jobId,
2223
string $jobUrl,
23-
string $jobStartTime,
24-
string $jobEndTime,
24+
?DateTimeInterface $jobStartTime,
25+
?DateTimeInterface $jobEndTime,
2526
string $componentId,
2627
string $componentName,
2728
?string $configurationId,
@@ -45,14 +46,19 @@ public function jsonSerialize()
4546
$result = [
4647
'id' => $this->jobId,
4748
'url' => $this->jobUrl,
48-
'startTime' => $this->jobStartTime,
49-
'endTime' => $this->jobEndTime,
5049
'component' => [
5150
'id' => $this->componentId,
5251
'name' => $this->componentName,
5352
],
5453
'tasks' => [],
5554
];
55+
if (!is_null($this->jobStartTime)) {
56+
$result['startTime'] = $this->jobStartTime->format(DateTimeInterface::ATOM);
57+
}
58+
if (!is_null($this->jobEndTime)) {
59+
$result['endTime'] = $this->jobEndTime->format(DateTimeInterface::ATOM);
60+
}
61+
5662
if ($this->configurationId && $this->configurationName) {
5763
$result['configuration'] = [
5864
'id' => $this->configurationId,

tests/ClientTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\NotificationClient\Tests;
66

7+
use DateTimeImmutable;
78
use GuzzleHttp\Handler\MockHandler;
89
use GuzzleHttp\HandlerStack;
910
use GuzzleHttp\Middleware;
@@ -105,8 +106,8 @@ private function getPostEventData(): Event
105106
new JobData(
106107
'23456',
107108
'http://someUrl',
108-
'2020-01-01',
109-
'2020-02-02',
109+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
110+
new DateTimeImmutable('2020-01-01T11:12:00+00:00'),
110111
'keboola.orchestrator',
111112
'Orchestrator',
112113
'my-configuration',

tests/EventsClientFunctionalTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\NotificationClient\Tests;
66

7+
use DateTimeImmutable;
78
use Keboola\NotificationClient\EventsClient;
89
use Keboola\NotificationClient\Requests\Event;
910
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
@@ -31,8 +32,8 @@ public function testPostEvent(): void
3132
new JobData(
3233
'23456',
3334
'http://someUrl',
34-
'2020-01-01T12:00:01Z',
35-
'2020-02-02T15:15:15Z',
35+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
36+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
3637
'keboola.orchestrator',
3738
'Orchestrator',
3839
'my-configuration',

tests/Requests/EventTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\NotificationClient\Tests\Requests;
66

7+
use DateTimeImmutable;
78
use Keboola\NotificationClient\Requests\Event;
89
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
910
use Keboola\NotificationClient\Requests\PostEvent\JobData;
@@ -21,8 +22,8 @@ public function testJsonSerialize(): void
2122
new JobData(
2223
'23456',
2324
'http://someUrl',
24-
'2020-01-01',
25-
'2020-02-02',
25+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
26+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
2627
'keboola.orchestrator',
2728
'Orchestrator',
2829
'my-configuration',
@@ -36,17 +37,17 @@ public function testJsonSerialize(): void
3637
'job' => [
3738
'id' => '23456',
3839
'url' => 'http://someUrl',
39-
'startTime' => '2020-01-01',
40-
'endTime' => '2020-02-02',
4140
'component' => [
4241
'id' => 'keboola.orchestrator',
4342
'name' => 'Orchestrator',
4443
],
44+
'tasks' => [],
45+
'startTime' => '2020-01-01T11:11:00+00:00',
46+
'endTime' => '2020-01-01T12:11:00+00:00',
4547
'configuration' => [
4648
'id' => 'my-configuration',
4749
'name' => 'My configuration',
4850
],
49-
'tasks' => [],
5051
],
5152
'project' => [
5253
'id' => '1234',

tests/Requests/PostEvent/FailedJobEventDataTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Keboola\NotificationClient\Tests\Requests\PostEvent;
66

7+
use DateTimeImmutable;
78
use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData;
89
use Keboola\NotificationClient\Requests\PostEvent\JobData;
910
use PHPUnit\Framework\TestCase;
@@ -15,8 +16,8 @@ public function testJsonSerialize(): void
1516
$jobData = new JobData(
1617
'23456',
1718
'http://someUrl',
18-
'2020-01-01',
19-
'2020-02-02',
19+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
20+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
2021
'keboola.orchestrator',
2122
'Orchestrator',
2223
'my-configuration',
@@ -29,17 +30,17 @@ public function testJsonSerialize(): void
2930
'job' => [
3031
'id' => '23456',
3132
'url' => 'http://someUrl',
32-
'startTime' => '2020-01-01',
33-
'endTime' => '2020-02-02',
3433
'component' => [
3534
'id' => 'keboola.orchestrator',
3635
'name' => 'Orchestrator',
3736
],
37+
'tasks' => [],
38+
'startTime' => '2020-01-01T11:11:00+00:00',
39+
'endTime' => '2020-01-01T12:11:00+00:00',
3840
'configuration' => [
3941
'id' => 'my-configuration',
4042
'name' => 'My configuration',
4143
],
42-
'tasks' => [],
4344
],
4445
'project' => [
4546
'id' => '1234',

tests/Requests/PostEvent/JobDataTest.php

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,115 @@
44

55
namespace Keboola\NotificationClient\Tests\Requests\PostEvent;
66

7+
use DateTimeImmutable;
78
use Keboola\NotificationClient\Requests\PostEvent\JobData;
89
use PHPUnit\Framework\TestCase;
910

1011
class JobDataTest extends TestCase
1112
{
12-
public function testJsonSerialize(): void
13+
/**
14+
* @dataProvider jobDataProvider
15+
*/
16+
public function testJsonSerialize(JobData $jobData, array $expected): void
1317
{
14-
$jobData = new JobData(
15-
'23456',
16-
'http://someUrl',
17-
'2020-01-01',
18-
'2020-02-02',
19-
'keboola.orchestrator',
20-
'Orchestrator',
21-
'my-configuration',
22-
'My configuration'
23-
);
24-
self::assertSame(
18+
self::assertSame($expected, $jobData->jsonSerialize());
19+
}
20+
21+
public function jobDataProvider()
22+
{
23+
yield 'full data' => [
24+
new JobData(
25+
'23456',
26+
'http://someUrl',
27+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
28+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
29+
'keboola.orchestrator',
30+
'Orchestrator',
31+
'my-configuration',
32+
'My configuration'
33+
),
2534
[
2635
'id' => '23456',
2736
'url' => 'http://someUrl',
28-
'startTime' => '2020-01-01',
29-
'endTime' => '2020-02-02',
3037
'component' => [
3138
'id' => 'keboola.orchestrator',
3239
'name' => 'Orchestrator',
3340
],
41+
'tasks' => [],
42+
'startTime' => '2020-01-01T11:11:00+00:00',
43+
'endTime' => '2020-01-01T12:11:00+00:00',
3444
'configuration' => [
3545
'id' => 'my-configuration',
3646
'name' => 'My configuration',
3747
],
48+
]
49+
];
50+
yield 'null configuration id' => [
51+
new JobData(
52+
'23456',
53+
'http://someUrl',
54+
new DateTimeImmutable('2020-01-01T11:11:00+00:00'),
55+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
56+
'keboola.orchestrator',
57+
'Orchestrator',
58+
null,
59+
'My configuration'
60+
),
61+
[
62+
'id' => '23456',
63+
'url' => 'http://someUrl',
64+
'component' => [
65+
'id' => 'keboola.orchestrator',
66+
'name' => 'Orchestrator',
67+
],
3868
'tasks' => [],
69+
'startTime' => '2020-01-01T11:11:00+00:00',
70+
'endTime' => '2020-01-01T12:11:00+00:00',
3971
],
40-
$jobData->jsonSerialize()
41-
);
42-
}
43-
44-
public function testJsonSerializeEmptyConfigurationId(): void
45-
{
46-
$jobData = new JobData(
47-
'23456',
48-
'http://someUrl',
49-
'2020-01-01',
50-
'2020-02-02',
51-
'keboola.orchestrator',
52-
'Orchestrator',
53-
null,
54-
'My configuration'
55-
);
56-
self::assertSame(
72+
];
73+
yield 'null configuration name' => [
74+
new JobData(
75+
'23456',
76+
'http://someUrl',
77+
new DateTimeImmutable('2020-01-01T12:11:00+00:00'),
78+
new DateTimeImmutable('2020-01-01T13:11:00+00:00'),
79+
'keboola.orchestrator',
80+
'Orchestrator',
81+
'1234',
82+
null
83+
),
5784
[
5885
'id' => '23456',
5986
'url' => 'http://someUrl',
60-
'startTime' => '2020-01-01',
61-
'endTime' => '2020-02-02',
6287
'component' => [
6388
'id' => 'keboola.orchestrator',
6489
'name' => 'Orchestrator',
6590
],
6691
'tasks' => [],
92+
'startTime' => '2020-01-01T12:11:00+00:00',
93+
'endTime' => '2020-01-01T13:11:00+00:00',
6794
],
68-
$jobData->jsonSerialize()
69-
);
70-
}
71-
72-
public function testJsonSerializeEmptyConfigurationName(): void
73-
{
74-
$jobData = new JobData(
75-
'23456',
76-
'http://someUrl',
77-
'2020-01-01',
78-
'2020-02-02',
79-
'keboola.orchestrator',
80-
'Orchestrator',
81-
'1234',
82-
null
83-
);
84-
self::assertSame(
95+
];
96+
yield 'empty start and end time' => [
97+
new JobData(
98+
'23456',
99+
'http://someUrl',
100+
null,
101+
null,
102+
'keboola.orchestrator',
103+
'Orchestrator',
104+
'1234',
105+
null
106+
),
85107
[
86108
'id' => '23456',
87109
'url' => 'http://someUrl',
88-
'startTime' => '2020-01-01',
89-
'endTime' => '2020-02-02',
90110
'component' => [
91111
'id' => 'keboola.orchestrator',
92112
'name' => 'Orchestrator',
93113
],
94114
'tasks' => [],
95115
],
96-
$jobData->jsonSerialize()
97-
);
116+
];
98117
}
99118
}

0 commit comments

Comments
 (0)