Skip to content

Commit 5942f4f

Browse files
committed
test: add comprehensive tests for InitDirectoryStructure
- Add InitDirectoryStructureTests: test create method with various path types - Test basic structure creation (migrations, models, repositories, services, seeds) - Test absolute and relative paths - Test nested paths and existing directories - Test .gitkeep file creation - Test Windows paths and trailing slashes - Test empty and null paths handling Coverage improvements: - InitDirectoryStructure: 92.68% lines (was 80.49%)
1 parent cee3074 commit 5942f4f

1 file changed

Lines changed: 244 additions & 0 deletions

File tree

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace tommyknocker\pdodb\tests\shared;
6+
7+
use tommyknocker\pdodb\cli\InitDirectoryStructure;
8+
9+
/**
10+
* Tests for InitDirectoryStructure class.
11+
*/
12+
final class InitDirectoryStructureTests extends BaseSharedTestCase
13+
{
14+
private string $tempDir;
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->tempDir = sys_get_temp_dir() . '/pdodb_test_' . uniqid();
20+
mkdir($this->tempDir, 0755, true);
21+
chdir($this->tempDir);
22+
}
23+
24+
protected function tearDown(): void
25+
{
26+
parent::tearDown();
27+
if (is_dir($this->tempDir)) {
28+
$this->removeDirectory($this->tempDir);
29+
}
30+
chdir(sys_get_temp_dir());
31+
}
32+
33+
private function removeDirectory(string $dir): void
34+
{
35+
if (!is_dir($dir)) {
36+
return;
37+
}
38+
$files = array_diff(scandir($dir), ['.', '..']);
39+
foreach ($files as $file) {
40+
$path = $dir . '/' . $file;
41+
if (is_dir($path)) {
42+
$this->removeDirectory($path);
43+
} else {
44+
unlink($path);
45+
}
46+
}
47+
rmdir($dir);
48+
}
49+
50+
public function testCreateEmptyStructure(): void
51+
{
52+
ob_start();
53+
InitDirectoryStructure::create([]);
54+
$output = ob_get_clean();
55+
56+
$this->assertEmpty($output);
57+
}
58+
59+
public function testCreateBasicStructure(): void
60+
{
61+
$structure = [
62+
'migrations' => 'migrations',
63+
'models' => 'models',
64+
];
65+
66+
ob_start();
67+
InitDirectoryStructure::create($structure);
68+
$output = ob_get_clean();
69+
70+
$this->assertTrue(is_dir($this->tempDir . '/migrations'));
71+
$this->assertTrue(is_dir($this->tempDir . '/models'));
72+
$this->assertStringContainsString('Creating directory structure', $output);
73+
$this->assertStringContainsString('Created directory: migrations', $output);
74+
$this->assertStringContainsString('Created directory: models', $output);
75+
}
76+
77+
public function testCreateFullStructure(): void
78+
{
79+
$structure = [
80+
'migrations' => 'migrations',
81+
'models' => 'models',
82+
'repositories' => 'repositories',
83+
'services' => 'services',
84+
'seeds' => 'seeds',
85+
];
86+
87+
ob_start();
88+
InitDirectoryStructure::create($structure);
89+
$output = ob_get_clean();
90+
91+
$this->assertTrue(is_dir($this->tempDir . '/migrations'));
92+
$this->assertTrue(is_dir($this->tempDir . '/models'));
93+
$this->assertTrue(is_dir($this->tempDir . '/repositories'));
94+
$this->assertTrue(is_dir($this->tempDir . '/services'));
95+
$this->assertTrue(is_dir($this->tempDir . '/seeds'));
96+
}
97+
98+
public function testCreateWithAbsolutePath(): void
99+
{
100+
$absolutePath = $this->tempDir . '/absolute_test';
101+
$structure = [
102+
'migrations' => $absolutePath,
103+
];
104+
105+
ob_start();
106+
InitDirectoryStructure::create($structure);
107+
$output = ob_get_clean();
108+
109+
$this->assertTrue(is_dir($absolutePath));
110+
}
111+
112+
public function testCreateWithRelativePath(): void
113+
{
114+
$structure = [
115+
'migrations' => './relative_migrations',
116+
];
117+
118+
ob_start();
119+
InitDirectoryStructure::create($structure);
120+
$output = ob_get_clean();
121+
122+
$this->assertTrue(is_dir($this->tempDir . '/relative_migrations'));
123+
}
124+
125+
public function testCreateWithNestedPath(): void
126+
{
127+
$structure = [
128+
'migrations' => 'app/database/migrations',
129+
];
130+
131+
ob_start();
132+
InitDirectoryStructure::create($structure);
133+
$output = ob_get_clean();
134+
135+
$this->assertTrue(is_dir($this->tempDir . '/app/database/migrations'));
136+
}
137+
138+
public function testCreateWithExistingDirectory(): void
139+
{
140+
mkdir($this->tempDir . '/existing', 0755, true);
141+
142+
$structure = [
143+
'migrations' => 'existing',
144+
];
145+
146+
ob_start();
147+
InitDirectoryStructure::create($structure);
148+
$output = ob_get_clean();
149+
150+
$this->assertTrue(is_dir($this->tempDir . '/existing'));
151+
$this->assertStringContainsString('Directory already exists', $output);
152+
}
153+
154+
public function testCreateWithEmptyPath(): void
155+
{
156+
$structure = [
157+
'migrations' => '',
158+
'models' => 'models',
159+
];
160+
161+
ob_start();
162+
InitDirectoryStructure::create($structure);
163+
$output = ob_get_clean();
164+
165+
$this->assertFalse(is_dir($this->tempDir . '/migrations'));
166+
$this->assertTrue(is_dir($this->tempDir . '/models'));
167+
}
168+
169+
public function testCreateWithNullPath(): void
170+
{
171+
$structure = [
172+
'migrations' => null,
173+
'models' => 'models',
174+
];
175+
176+
ob_start();
177+
InitDirectoryStructure::create($structure);
178+
$output = ob_get_clean();
179+
180+
$this->assertFalse(is_dir($this->tempDir . '/migrations'));
181+
$this->assertTrue(is_dir($this->tempDir . '/models'));
182+
}
183+
184+
public function testCreateWithGitkeepFiles(): void
185+
{
186+
$structure = [
187+
'migrations' => 'migrations',
188+
'models' => 'models',
189+
];
190+
191+
ob_start();
192+
InitDirectoryStructure::create($structure);
193+
ob_end_clean();
194+
195+
$this->assertTrue(file_exists($this->tempDir . '/migrations/.gitkeep'));
196+
$this->assertTrue(file_exists($this->tempDir . '/models/.gitkeep'));
197+
}
198+
199+
public function testCreateWithWindowsPath(): void
200+
{
201+
$structure = [
202+
'migrations' => 'C:\\test\\migrations',
203+
];
204+
205+
ob_start();
206+
InitDirectoryStructure::create($structure);
207+
$output = ob_get_clean();
208+
209+
// On non-Windows systems, this will be treated as relative path
210+
// On Windows, it would create absolute path
211+
if (PHP_OS_FAMILY === 'Windows') {
212+
$this->assertTrue(is_dir('C:\\test\\migrations'));
213+
} else {
214+
// On Unix, it treats as relative path
215+
$this->assertTrue(is_dir($this->tempDir . '/C:\\test\\migrations') || is_dir('C:\\test\\migrations'));
216+
}
217+
}
218+
219+
public function testCreateWithTrailingSlash(): void
220+
{
221+
$structure = [
222+
'migrations' => 'migrations/',
223+
];
224+
225+
ob_start();
226+
InitDirectoryStructure::create($structure);
227+
ob_end_clean();
228+
229+
$this->assertTrue(is_dir($this->tempDir . '/migrations'));
230+
}
231+
232+
public function testCreateWithBackslash(): void
233+
{
234+
$structure = [
235+
'migrations' => 'migrations\\',
236+
];
237+
238+
ob_start();
239+
InitDirectoryStructure::create($structure);
240+
ob_end_clean();
241+
242+
$this->assertTrue(is_dir($this->tempDir . '/migrations'));
243+
}
244+
}

0 commit comments

Comments
 (0)