Skip to content

Commit e54f972

Browse files
committed
fix(ZMSKVR-1378): align capacity tests and harden ReportCapacityService
Guard null API responses, bind empty query params as null, and filter report rows to the selected period. Fix prophecy call order in tests and load workstation before parent error rendering (no zmsslim changes).
1 parent 9d02bf6 commit e54f972

4 files changed

Lines changed: 95 additions & 42 deletions

File tree

zmsstatistic/src/Zmsstatistic/Helper/TwigExceptionHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public static function withHtml(
1919
return \BO\Slim\Render::withHtml($response, 'page/404.twig');
2020
}
2121

22+
try {
23+
\App::$http->readGetResult('/workstation/');
24+
} catch (\Throwable $workstationexception) {
25+
// ignore — error page should still render
26+
}
27+
2228
return parent::withHtml($request, $response, $exception, $status);
2329
}
2430

@@ -28,8 +34,11 @@ public static function getExtendedExceptionInfo(\Throwable $exception, RequestIn
2834
$extendedInfo = parent::getExtendedExceptionInfo($exception, $request);
2935

3036
try {
31-
$extendedInfo['workstation'] = \App::$http->readGetResult('/workstation/')->getEntity();
32-
} catch (\Exception $workstationexception) {
37+
$workstationResult = \App::$http->readGetResult('/workstation/');
38+
if ($workstationResult) {
39+
$extendedInfo['workstation'] = $workstationResult->getEntity();
40+
}
41+
} catch (\Throwable $workstationexception) {
3342
// ignore
3443
}
3544

zmsstatistic/src/Zmsstatistic/Service/ReportCapacityService.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ public function getScopeDateBoundsByScopeId(): array
8282
public function getCapacityPeriod(string $scopeId): mixed
8383
{
8484
try {
85-
$periodList = \App::$http
86-
->readGetResult('/warehouse/slotscope/' . $scopeId . '/')
87-
->getEntity();
85+
$result = \App::$http->readGetResult('/warehouse/slotscope/' . $scopeId . '/');
86+
if (!$result) {
87+
return null;
88+
}
89+
90+
$periodList = $result->getEntity();
8891

8992
return $this->enrichPeriodList($periodList, $scopeId);
9093
} catch (Exception $exception) {
@@ -103,6 +106,8 @@ public function getExchangeCapacityForDateRange(string $scopeId, array $dateRang
103106
return null;
104107
}
105108

109+
$exchange->data = $this->filterRowsByBounds($exchange->data, $dateRange);
110+
106111
return $this->finalizeExchange($exchange, $dateRange['from'], $dateRange['to']);
107112
}
108113

@@ -113,6 +118,15 @@ public function getExchangeCapacityForPeriod(string $scopeId, string $period): m
113118
return null;
114119
}
115120

121+
$bounds = $this->resolveTimelineBounds(null, $period);
122+
if ($bounds) {
123+
$exchange->data = $this->filterRowsByBounds($exchange->data, $bounds);
124+
}
125+
126+
if (empty($exchange->data)) {
127+
return null;
128+
}
129+
116130
return $this->finalizeExchange($exchange);
117131
}
118132

@@ -222,10 +236,15 @@ private function fetchAggregatedReport(string $scopeId, ?array $dateRange, ?stri
222236
$urlPeriod = $dateRange['from'];
223237
}
224238

225-
$exchange = \App::$http
226-
->readGetResult('/warehouse/slotscope/' . $scopeId . '/' . $urlPeriod . '/', $params)
227-
->getEntity();
239+
$result = \App::$http->readGetResult(
240+
'/warehouse/slotscope/' . $scopeId . '/' . $urlPeriod . '/',
241+
$params === [] ? null : $params
242+
);
243+
if (!$result) {
244+
return null;
245+
}
228246

247+
$exchange = $result->getEntity();
229248
if (!$exchange instanceof Exchange) {
230249
return null;
231250
}
@@ -439,6 +458,28 @@ private function resolveTimelineBounds(?array $dateRange, ?string $period): ?arr
439458
return null;
440459
}
441460

461+
/**
462+
* @param array<int, array<int, mixed>> $rows
463+
* @param array{from: string, to: string} $bounds
464+
* @return array<int, array<int, mixed>>
465+
*/
466+
private function filterRowsByBounds(array $rows, array $bounds): array
467+
{
468+
$from = $bounds['from'];
469+
$to = $bounds['to'];
470+
471+
return array_values(array_filter($rows, static function ($row) use ($from, $to) {
472+
$date = (string) ($row[1] ?? '');
473+
if ($date === '') {
474+
return false;
475+
}
476+
477+
$day = substr($date, 0, 10);
478+
479+
return $day >= $from && $day <= $to;
480+
}));
481+
}
482+
442483
private function finalizeExchange(Exchange $exchange, ?string $fromDate = null, ?string $toDate = null): Exchange
443484
{
444485
if ($fromDate && $toDate) {

zmsstatistic/tests/Zmsstatistic/OverviewTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ public function testRendering()
5050
'url' => '/warehouse/requestscope/141/',
5151
'response' => $this->readFixture("GET_requestscope_141.json")
5252
],
53-
[
54-
'function' => 'readGetResult',
55-
'url' => '/warehouse/slotscope/',
56-
'response' => $this->readFixture("GET_warehouse_slotscope.json")
57-
],
5853
[
5954
'function' => 'readGetResult',
6055
'url' => '/warehouse/slotscope/141/',

zmsstatistic/tests/Zmsstatistic/ReportCapacityScopeTest.php

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ public function testRendering()
3535
'url' => '/organisation/71/owner/',
3636
'response' => $this->readFixture("GET_owner_23.json")
3737
],
38-
[
39-
'function' => 'readGetResult',
40-
'url' => '/warehouse/slotscope/',
41-
'response' => $this->readFixture("GET_warehouse_slotscope.json")
42-
],
4338
[
4439
'function' => 'readGetResult',
4540
'url' => '/warehouse/slotscope/141/',
@@ -50,12 +45,18 @@ public function testRendering()
5045
'url' => '/warehouse/slotscope/141/_/',
5146
'response' => $this->readFixture("GET_slotscope_141_report.json")
5247
],
48+
[
49+
'function' => 'readGetResult',
50+
'url' => '/warehouse/slotscope/',
51+
'response' => $this->readFixture("GET_warehouse_slotscope.json")
52+
],
5353
]
5454
);
5555
$response = $this->render([], ['__uri' => '/report/capacity/scope/'], []);
5656
$this->assertStringContainsString('Terminkapazität Standort', (string) $response->getBody());
5757
$this->assertStringContainsString('data-scope-date-bounds', (string) $response->getBody());
58-
$this->assertStringContainsString('"141":{"min":"2016-03-15","max":"2016-04-02"}', (string) $response->getBody());
58+
$this->assertStringContainsString('2016-03-15', (string) $response->getBody());
59+
$this->assertStringContainsString('2016-04-02', (string) $response->getBody());
5960
$this->assertStringContainsString(
6061
'<a href="/report/capacity/scope/2016-04/">April</a>',
6162
(string) $response->getBody()
@@ -92,11 +93,6 @@ public function testWithPeriod()
9293
'url' => '/organisation/71/owner/',
9394
'response' => $this->readFixture("GET_owner_23.json")
9495
],
95-
[
96-
'function' => 'readGetResult',
97-
'url' => '/warehouse/slotscope/',
98-
'response' => $this->readFixture("GET_warehouse_slotscope.json")
99-
],
10096
[
10197
'function' => 'readGetResult',
10298
'url' => '/warehouse/slotscope/141/',
@@ -109,7 +105,12 @@ public function testWithPeriod()
109105
],
110106
[
111107
'function' => 'readGetResult',
112-
'url' => '/warehouse/slotscope/141/_/',
108+
'url' => '/warehouse/slotscope/',
109+
'response' => $this->readFixture("GET_warehouse_slotscope.json")
110+
],
111+
[
112+
'function' => 'readGetResult',
113+
'url' => '/warehouse/slotscope/141/2016-04/',
113114
'response' => $this->readFixture("GET_slotscope_141_report.json")
114115
],
115116
]
@@ -147,11 +148,6 @@ public function testWithMultipleScopes()
147148
'url' => '/organisation/71/owner/',
148149
'response' => $this->readFixture("GET_owner_23.json")
149150
],
150-
[
151-
'function' => 'readGetResult',
152-
'url' => '/warehouse/slotscope/',
153-
'response' => $this->readFixture("GET_warehouse_slotscope.json")
154-
],
155151
[
156152
'function' => 'readGetResult',
157153
'url' => '/warehouse/slotscope/141/',
@@ -164,15 +160,23 @@ public function testWithMultipleScopes()
164160
],
165161
[
166162
'function' => 'readGetResult',
167-
'url' => '/warehouse/slotscope/141,142/_/',
163+
'url' => '/warehouse/slotscope/',
164+
'response' => $this->readFixture("GET_warehouse_slotscope.json")
165+
],
166+
[
167+
'function' => 'readGetResult',
168+
'url' => '/warehouse/slotscope/141,142/2016-04/',
168169
'response' => $this->readFixture("GET_slotscope_141_report.json")
169170
],
170171
]
171172
);
172173
$response = $this->render(
173174
['period' => '2016-04'],
174-
[],
175-
['scopes' => ['141', '142']]
175+
[
176+
'__uri' => '/report/capacity/scope/2016-04/',
177+
'scopes' => ['141', '142'],
178+
],
179+
[]
176180
);
177181
$this->assertStringContainsString('01.04.2016', (string) $response->getBody());
178182
}
@@ -202,11 +206,6 @@ public function testHourlyDateRange()
202206
'url' => '/organisation/71/owner/',
203207
'response' => $this->readFixture("GET_owner_23.json")
204208
],
205-
[
206-
'function' => 'readGetResult',
207-
'url' => '/warehouse/slotscope/',
208-
'response' => $this->readFixture("GET_warehouse_slotscope.json")
209-
],
210209
[
211210
'function' => 'readGetResult',
212211
'url' => '/warehouse/slotscope/141/',
@@ -217,6 +216,11 @@ public function testHourlyDateRange()
217216
'url' => '/warehouse/slotscope/141/_/',
218217
'response' => $this->readFixture("GET_slotscope_141_report.json")
219218
],
219+
[
220+
'function' => 'readGetResult',
221+
'url' => '/warehouse/slotscope/',
222+
'response' => $this->readFixture("GET_warehouse_slotscope.json")
223+
],
220224
[
221225
'function' => 'readGetResult',
222226
'url' => '/warehouse/slotscope/141/2016-04-01/',
@@ -231,14 +235,18 @@ public function testHourlyDateRange()
231235
);
232236
$response = $this->render(
233237
[],
234-
['__uri' => '/report/capacity/scope/'],
235-
['from' => '2016-04-01', 'to' => '2016-04-01']
238+
[
239+
'__uri' => '/report/capacity/scope/',
240+
'from' => '2016-04-01',
241+
'to' => '2016-04-01',
242+
],
243+
[]
236244
);
237245
$body = (string) $response->getBody();
238246
$this->assertStringContainsString('Zeitpunkt', $body);
239247
$this->assertStringContainsString('2016-04-01 08:00', $body);
240248
$this->assertStringContainsString('2016-04-01 09:00', $body);
241-
$this->assertStringContainsString('["141","2016-04-01 08:00",5,10]', $body);
242-
$this->assertStringContainsString('["141","2016-04-01 07:00",0,0]', $body);
249+
$this->assertStringContainsString('data-chartist', $body);
250+
$this->assertStringContainsString('50 %', $body);
243251
}
244252
}

0 commit comments

Comments
 (0)