Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1<?php
2
3namespace SocialDept\AtpSchema\Tests\Unit\Generator;
4
5use Orchestra\Testbench\TestCase;
6use SocialDept\AtpSchema\Exceptions\GenerationException;
7use SocialDept\AtpSchema\Generator\FileWriter;
8
9class FileWriterTest extends TestCase
10{
11 protected string $tempDir;
12
13 protected FileWriter $writer;
14
15 protected function setUp(): void
16 {
17 parent::setUp();
18
19 $this->tempDir = sys_get_temp_dir().'/schema-test-'.uniqid();
20 mkdir($this->tempDir, 0755, true);
21
22 $this->writer = new FileWriter(overwrite: true, createDirectories: true);
23 }
24
25 protected function tearDown(): void
26 {
27 if (is_dir($this->tempDir)) {
28 $this->deleteDirectory($this->tempDir);
29 }
30
31 parent::tearDown();
32 }
33
34 protected function deleteDirectory(string $dir): void
35 {
36 if (! is_dir($dir)) {
37 return;
38 }
39
40 $items = scandir($dir);
41
42 foreach ($items as $item) {
43 if ($item === '.' || $item === '..') {
44 continue;
45 }
46
47 $path = $dir.'/'.$item;
48
49 if (is_dir($path)) {
50 $this->deleteDirectory($path);
51 } else {
52 unlink($path);
53 }
54 }
55
56 rmdir($dir);
57 }
58
59 public function test_it_writes_file(): void
60 {
61 $path = $this->tempDir.'/test.txt';
62 $content = 'Hello, World!';
63
64 $this->writer->write($path, $content);
65
66 $this->assertFileExists($path);
67 $this->assertSame($content, file_get_contents($path));
68 }
69
70 public function test_it_creates_directories(): void
71 {
72 $path = $this->tempDir.'/nested/directory/test.txt';
73 $content = 'Test content';
74
75 $this->writer->write($path, $content);
76
77 $this->assertFileExists($path);
78 $this->assertDirectoryExists(dirname($path));
79 }
80
81 public function test_it_overwrites_existing_file_when_enabled(): void
82 {
83 $path = $this->tempDir.'/test.txt';
84
85 $this->writer->write($path, 'Original');
86 $this->writer->write($path, 'Updated');
87
88 $this->assertSame('Updated', file_get_contents($path));
89 }
90
91 public function test_it_throws_when_file_exists_and_overwrite_disabled(): void
92 {
93 $writer = new FileWriter(overwrite: false);
94 $path = $this->tempDir.'/test.txt';
95
96 file_put_contents($path, 'Existing content');
97
98 $this->expectException(GenerationException::class);
99 $this->expectExceptionMessage('File already exists');
100
101 $writer->write($path, 'New content');
102 }
103
104 public function test_it_checks_if_file_exists(): void
105 {
106 $path = $this->tempDir.'/test.txt';
107
108 $this->assertFalse($this->writer->exists($path));
109
110 file_put_contents($path, 'Content');
111
112 $this->assertTrue($this->writer->exists($path));
113 }
114
115 public function test_it_deletes_file(): void
116 {
117 $path = $this->tempDir.'/test.txt';
118
119 file_put_contents($path, 'Content');
120 $this->assertFileExists($path);
121
122 $this->writer->delete($path);
123
124 $this->assertFileDoesNotExist($path);
125 }
126
127 public function test_it_reads_file(): void
128 {
129 $path = $this->tempDir.'/test.txt';
130 $content = 'Test content';
131
132 file_put_contents($path, $content);
133
134 $this->assertSame($content, $this->writer->read($path));
135 }
136
137 public function test_it_throws_when_reading_nonexistent_file(): void
138 {
139 $path = $this->tempDir.'/nonexistent.txt';
140
141 $this->expectException(GenerationException::class);
142 $this->expectExceptionMessage('File not found');
143
144 $this->writer->read($path);
145 }
146
147 public function test_it_sets_overwrite_option(): void
148 {
149 $writer = new FileWriter(overwrite: false);
150 $path = $this->tempDir.'/test.txt';
151
152 file_put_contents($path, 'Existing');
153
154 $writer->setOverwrite(true);
155 $writer->write($path, 'Updated');
156
157 $this->assertSame('Updated', file_get_contents($path));
158 }
159}