Skip to content

Commit c500533

Browse files
authored
Merge pull request #1857 from ddm14159/#1817-dto-check-result
Replace CheckResult with CheckResultData DTO
2 parents 29dc3db + 5fb6686 commit c500533

File tree

6 files changed

+60
-73
lines changed

6 files changed

+60
-73
lines changed

app/DTO/CheckResultData.php

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 App\DTO;
6+
7+
use Spatie\LaravelData\Data;
8+
9+
class CheckResultData extends Data
10+
{
11+
public const int SUCCESS_EXIT_CODE = 0;
12+
public const int FAILED_TESTS_EXIT_CODE = 1;
13+
14+
private const string SUCCESS_STATUS = 'success';
15+
private const string TESTS_FAILED_STATUS = 'tests_failed';
16+
private const string CHECK_EXECUTION_ERROR_STATUS = 'check_error';
17+
18+
public function __construct(
19+
public int $exitCode,
20+
public string $output = '',
21+
) {
22+
}
23+
24+
public function getResultStatus(): string
25+
{
26+
return match ($this->exitCode) {
27+
self::SUCCESS_EXIT_CODE => self::SUCCESS_STATUS,
28+
self::FAILED_TESTS_EXIT_CODE => self::TESTS_FAILED_STATUS,
29+
default => self::CHECK_EXECUTION_ERROR_STATUS,
30+
};
31+
}
32+
33+
public function isSuccess(): bool
34+
{
35+
return $this->getResultStatus() === self::SUCCESS_STATUS;
36+
}
37+
38+
public function isFailedTests(): bool
39+
{
40+
return $this->getResultStatus() === self::TESTS_FAILED_STATUS;
41+
}
42+
43+
public function isCheckExecutionError(): bool
44+
{
45+
return $this->getResultStatus() === self::CHECK_EXECUTION_ERROR_STATUS;
46+
}
47+
}

app/Http/Resources/Api/Exercise/CheckResultResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace App\Http\Resources\Api\Exercise;
44

5+
use App\DTO\CheckResultData;
56
use Illuminate\Http\Request;
67
use Illuminate\Http\Resources\Json\JsonResource;
78

89
/**
9-
* @property \App\Services\CheckResult $resource
10+
* @property CheckResultData $resource
1011
*/
1112
class CheckResultResource extends JsonResource
1213
{
1314
public function toArray(Request $request): array
1415
{
1516
return [
16-
/** @var int */
1717
'exit_code' => $this->resource->exitCode,
1818
'result_status' => $this->resource->getResultStatus(),
19-
'output' => $this->resource->getOutput(),
19+
'output' => $this->resource->output,
2020
];
2121
}
2222
}

app/Services/CheckResult.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

app/Services/ExerciseService.php

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

55
namespace App\Services;
66

7+
use App\DTO\CheckResultData;
78
use App\Models\User;
89
use App\Models\Exercise;
910
use App\Models\ExerciseMember;
@@ -13,13 +14,13 @@
1314

1415
class ExerciseService
1516
{
16-
private const POINTS_PER_EXERCISE = 3;
17+
private const int POINTS_PER_EXERCISE = 3;
1718

1819
public function __construct(private SolutionChecker $checker, private ActivityService $activityService)
1920
{
2021
}
2122

22-
public function check(User $user, Exercise $exercise, string $solutionCode): CheckResult
23+
public function check(User $user, Exercise $exercise, string $solutionCode): CheckResultData
2324
{
2425
$checkResult = $this->checker->check($exercise, $solutionCode);
2526

app/Services/SolutionChecker.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace App\Services;
44

5+
use App\DTO\CheckResultData;
56
use App\Models\Exercise;
67
use Illuminate\Support\Facades\Storage;
78
use mikehaertl\shellcommand\Command;
89

910
class SolutionChecker
1011
{
11-
public function check(Exercise $exercise, string $solutionCode): CheckResult
12+
public function check(Exercise $exercise, string $solutionCode): CheckResultData
1213
{
1314
if (!$exercise->hasTests()) {
14-
return new CheckResult(CheckResult::SUCCESS_EXIT_CODE);
15+
return new CheckResultData(CheckResultData::SUCCESS_EXIT_CODE);
1516
}
1617

1718
$tests = $exercise->getExerciseTests();
@@ -38,7 +39,7 @@ public function check(Exercise $exercise, string $solutionCode): CheckResult
3839
? $command->getOutput()
3940
: $command->getError();
4041

41-
return new CheckResult($exitCode, $output);
42+
return new CheckResultData($exitCode, $output);
4243
}
4344

4445
private function prettifyCode(string $code): string

tests/Feature/Http/Controllers/Api/Exercise/CheckControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Tests\Feature\Http\Controllers\Api\Exercise;
44

5+
use App\DTO\CheckResultData;
56
use App\Models\Exercise;
6-
use App\Services\CheckResult;
77
use Database\Seeders\ChaptersTableSeeder;
88
use Database\Seeders\ExercisesTableSeeder;
99
use Tests\ControllerTestCase;
@@ -39,7 +39,7 @@ public function testCheck(): void
3939

4040
$responseBody = $response->decodeResponseJson();
4141

42-
$this->assertTrue(array_get($responseBody, 'check_result.exit_code') === CheckResult::SUCCESS_EXIT_CODE);
42+
$this->assertTrue(array_get($responseBody, 'check_result.exit_code') === CheckResultData::SUCCESS_EXIT_CODE);
4343

4444
$this->assertDatabaseHas('activity_log', [
4545
'causer_id' => $this->user->id,
@@ -69,6 +69,6 @@ public function testCheckByGuest(): void
6969
$response->assertCreated();
7070
$responseBody = $response->decodeResponseJson();
7171

72-
$this->assertTrue(array_get($responseBody, 'check_result.exit_code') === CheckResult::SUCCESS_EXIT_CODE);
72+
$this->assertTrue(array_get($responseBody, 'check_result.exit_code') === CheckResultData::SUCCESS_EXIT_CODE);
7373
}
7474
}

0 commit comments

Comments
 (0)