Skip to content

Commit 2eeabd0

Browse files
committed
[php][divergent_change-01_base] Platform returns a CSV
1 parent 18cb518 commit 2eeabd0

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,56 @@ public function __construct(Platform $platform)
1717

1818
public function get(string $courseId): string
1919
{
20-
$csvSteps = $this->platform->findCourseSteps($courseId);
20+
$csv = $this->platform->findCourseSteps($courseId);
2121

2222
$results = '[';
2323

24-
foreach ($csvSteps as $index => $row) {
25-
$type = $row['type'];
24+
$csvLines = explode(PHP_EOL, $csv);
25+
26+
foreach ($csvLines as $index => $row) {
27+
$row = str_getcsv($row);
28+
29+
if (empty($csv)) {
30+
continue;
31+
}
32+
33+
$type = $row[1];
2634
$duration = 0;
2735
$points = 0;
2836

2937
if ($type === 'video') {
30-
$duration = $row['duration'] * 1.1; // 1.1 = due to video pauses
38+
$duration = $row[3] * 1.1; // 1.1 = due to video pauses
3139
}
3240

3341
if ($type === 'quiz') {
34-
$duration = $row['questions'] * 0.5; // 0.5 = time in minutes per question
42+
$duration = $row[2] * 0.5; // 0.5 = time in minutes per question
3543
}
3644

3745
if ($type !== 'video' && $type !== 'quiz') {
3846
continue;
3947
}
4048

4149
if ($type === 'video') {
42-
$points = $row['duration'] * 1.1 * 100;
50+
$points = $row[3] * 1.1 * 100;
4351
}
4452

4553
if ($type === 'quiz') {
46-
$points = $row['questions'] * 0.5 * 10;
54+
$points = $row[2] * 0.5 * 10;
4755
}
4856

4957
$results .= json_encode(
5058
[
51-
'id' => $row['id'],
52-
'type' => $row['type'],
59+
'id' => $row[0],
60+
'type' => $row[1],
5361
'duration' => $duration,
5462
'points' => $points
5563
],
5664
JSON_THROW_ON_ERROR
5765
);
5866

59-
if ($index !== count($csvSteps) - 1) {
67+
if ($index !== count($csvLines) - 1) {
6068
$results .= ',';
6169
}
62-
6370
}
6471

6572
$results .= ']';

examples/php/php-divergent_change-01_base/src/Platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Platform
88
{
9-
public function findCourseSteps(string $courseId): array;
9+
public function findCourseSteps(string $courseId): string;
1010
}

examples/php/php-divergent_change-01_base/tests/Controller/CourseStepsGetTest.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function shouldReturnEmptyStepList(): void
1616
$platform = $this->createMock(Platform::class);
1717
$platform
1818
->method('findCourseSteps')
19-
->willReturn([]);
19+
->willReturn('');
2020

2121
$sut = new CourseStepsGetController($platform);
2222

@@ -32,26 +32,16 @@ public function shouldReturnExistingCourseSteps(): void
3232
$platform
3333
->method('findCourseSteps')
3434
->with('73D74817-CC25-477D-BF3E-36130087293F')
35-
->willReturn([
36-
[
37-
'id' => 'uuid',
38-
'type' => 'video',
39-
'questions' => null,
40-
'duration' => 13
41-
],
42-
[
43-
'id' => 'uuid',
44-
'type' => 'quiz',
45-
'questions' => 5,
46-
'duration' => null
47-
],
48-
]);
35+
->willReturn(
36+
'"1","video","","13"
37+
"2","quiz","5",""'
38+
);
4939

5040
$sut = new CourseStepsGetController($platform);
5141

5242
$results = $sut->get('73D74817-CC25-477D-BF3E-36130087293F');
5343

54-
$expected = '[{"id":"uuid","type":"video","duration":14.3,"points":1430},{"id":"uuid","type":"quiz","duration":2.5,"points":25}]';
44+
$expected = '[{"id":"1","type":"video","duration":14.3,"points":1430},{"id":"2","type":"quiz","duration":2.5,"points":25}]';
5545
self::assertSame($expected, $results);
5646
}
5747

@@ -62,14 +52,9 @@ public function shouldIgnoreStepsWithInvalidType(): void
6252
$platform
6353
->method('findCourseSteps')
6454
->with('73D74817-CC25-477D-BF3E-36130087293F')
65-
->willReturn([
66-
[
67-
'id' => 'uuid',
68-
'type' => 'survey',
69-
'questions' => null,
70-
'duration' => 13
71-
]
72-
]);
55+
->willReturn(
56+
'"1","survey","","13"'
57+
);
7358

7459
$sut = new CourseStepsGetController($platform);
7560

0 commit comments

Comments
 (0)