Skip to content

Commit d32e8e9

Browse files
committed
test: add unit tests for ProgramStorage
1 parent 0b1a65e commit d32e8e9

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/ProgramStorageTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BOA\Programs\Tests;
6+
7+
use BOA\Programs\ProgramStorage;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @author shimomo
12+
*/
13+
final class ProgramStorageTest extends TestCase
14+
{
15+
/**
16+
* @var non-empty-string
17+
*/
18+
private string $tempDir;
19+
20+
/**
21+
* @return void
22+
*/
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->tempDir = sys_get_temp_dir() . '/program_storage_test_' . uniqid();
27+
mkdir($this->tempDir, 0777, true);
28+
}
29+
30+
/**
31+
* @return void
32+
*/
33+
protected function tearDown(): void
34+
{
35+
if (is_dir($this->tempDir)) {
36+
$files = new \RecursiveIteratorIterator(
37+
new \RecursiveDirectoryIterator($this->tempDir, \FilesystemIterator::SKIP_DOTS),
38+
\RecursiveIteratorIterator::CHILD_FIRST
39+
);
40+
41+
foreach ($files as $file) {
42+
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
43+
}
44+
45+
rmdir($this->tempDir);
46+
}
47+
}
48+
49+
/**
50+
* @return void
51+
*/
52+
public function testSave(): void
53+
{
54+
$storage = new ProgramStorage();
55+
$path = $this->tempDir . '/programs.json';
56+
57+
$programs = [
58+
[
59+
'race_date' => '2025-07-15',
60+
'race_stadium_number' => 1,
61+
'race_number' => 1,
62+
'race_closed_at' => '2025-07-15 15:17:00',
63+
'race_grade_number' => 5,
64+
'race_title' => 'にっぽん未来プロジェクト競走in桐生',
65+
'race_subtitle' => '予選',
66+
'race_distance' => 1800,
67+
'boats' => [
68+
[
69+
'racer_boat_number' => 1,
70+
'racer_name' => '松本 浩貴',
71+
'racer_number' => 3860,
72+
'racer_class_number' => 3,
73+
'racer_branch_number' => 11,
74+
'racer_birthplace_number' => 11,
75+
'racer_age' => 52,
76+
'racer_weight' => 52,
77+
'racer_flying_count' => 0,
78+
'racer_late_count' => 0,
79+
'racer_average_start_timing' => 0.19,
80+
'racer_national_top_1_percent' => 4.09,
81+
'racer_national_top_2_percent' => 18.48,
82+
'racer_national_top_3_percent' => 39.13,
83+
'racer_local_top_1_percent' => 4.85,
84+
'racer_local_top_2_percent' => 33.33,
85+
'racer_local_top_3_percent' => 40.74,
86+
'racer_assigned_motor_number' => 24,
87+
'racer_assigned_motor_top_2_percent' => 28,
88+
'racer_assigned_motor_top_3_percent' => 42.67,
89+
'racer_assigned_boat_number' => 23,
90+
'racer_assigned_boat_top_2_percent' => 30.47,
91+
'racer_assigned_boat_top_3_percent' => 51.56,
92+
],
93+
],
94+
],
95+
];
96+
97+
$storage->save($programs, $path);
98+
99+
$this->assertFileExists($path);
100+
101+
$content = json_decode(file_get_contents($path), true);
102+
$this->assertArrayHasKey('programs', $content);
103+
$this->assertSame($programs, $content['programs']);
104+
}
105+
}

0 commit comments

Comments
 (0)