aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/FileUtilsTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/FileUtilsTest.php')
-rw-r--r--tests/FileUtilsTest.php88
1 files changed, 87 insertions, 1 deletions
diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php
index 9163bdf1..3384504a 100644
--- a/tests/FileUtilsTest.php
+++ b/tests/FileUtilsTest.php
@@ -3,25 +3,48 @@
3namespace Shaarli; 3namespace Shaarli;
4 4
5use Exception; 5use Exception;
6use Shaarli\Exceptions\IOException;
6 7
7/** 8/**
8 * Class FileUtilsTest 9 * Class FileUtilsTest
9 * 10 *
10 * Test file utility class. 11 * Test file utility class.
11 */ 12 */
12class FileUtilsTest extends \Shaarli\TestCase 13class FileUtilsTest extends TestCase
13{ 14{
14 /** 15 /**
15 * @var string Test file path. 16 * @var string Test file path.
16 */ 17 */
17 protected static $file = 'sandbox/flat.db'; 18 protected static $file = 'sandbox/flat.db';
18 19
20 protected function setUp(): void
21 {
22 @mkdir('sandbox');
23 mkdir('sandbox/folder2');
24 touch('sandbox/file1');
25 touch('sandbox/file2');
26 mkdir('sandbox/folder1');
27 touch('sandbox/folder1/file1');
28 touch('sandbox/folder1/file2');
29 mkdir('sandbox/folder3');
30 mkdir('/tmp/shaarli-to-delete');
31 }
32
19 /** 33 /**
20 * Delete test file after every test. 34 * Delete test file after every test.
21 */ 35 */
22 protected function tearDown(): void 36 protected function tearDown(): void
23 { 37 {
24 @unlink(self::$file); 38 @unlink(self::$file);
39
40 @unlink('sandbox/folder1/file1');
41 @unlink('sandbox/folder1/file2');
42 @rmdir('sandbox/folder1');
43 @unlink('sandbox/file1');
44 @unlink('sandbox/file2');
45 @rmdir('sandbox/folder2');
46 @rmdir('sandbox/folder3');
47 @rmdir('/tmp/shaarli-to-delete');
25 } 48 }
26 49
27 /** 50 /**
@@ -107,4 +130,67 @@ class FileUtilsTest extends \Shaarli\TestCase
107 $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); 130 $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
108 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); 131 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
109 } 132 }
133
134 /**
135 * Test clearFolder with self delete and excluded files
136 */
137 public function testClearFolderSelfDeleteWithExclusion(): void
138 {
139 FileUtils::clearFolder('sandbox', true, ['file2']);
140
141 static::assertFileExists('sandbox/folder1/file2');
142 static::assertFileExists('sandbox/folder1');
143 static::assertFileExists('sandbox/file2');
144 static::assertFileExists('sandbox');
145
146 static::assertFileNotExists('sandbox/folder1/file1');
147 static::assertFileNotExists('sandbox/file1');
148 static::assertFileNotExists('sandbox/folder3');
149 }
150
151 /**
152 * Test clearFolder with self delete and excluded files
153 */
154 public function testClearFolderSelfDeleteWithoutExclusion(): void
155 {
156 FileUtils::clearFolder('sandbox', true);
157
158 static::assertFileNotExists('sandbox');
159 }
160
161 /**
162 * Test clearFolder with self delete and excluded files
163 */
164 public function testClearFolderNoSelfDeleteWithoutExclusion(): void
165 {
166 FileUtils::clearFolder('sandbox', false);
167
168 static::assertFileExists('sandbox');
169
170 // 2 because '.' and '..'
171 static::assertCount(2, new \DirectoryIterator('sandbox'));
172 }
173
174 /**
175 * Test clearFolder on a file instead of a folder
176 */
177 public function testClearFolderOnANonDirectory(): void
178 {
179 $this->expectException(IOException::class);
180 $this->expectExceptionMessage('Provided path is not a directory.');
181
182 FileUtils::clearFolder('sandbox/file1', false);
183 }
184
185 /**
186 * Test clearFolder on a file instead of a folder
187 */
188 public function testClearFolderOutsideOfShaarliDirectory(): void
189 {
190 $this->expectException(IOException::class);
191 $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.');
192
193
194 FileUtils::clearFolder('/tmp/shaarli-to-delete', true);
195 }
110} 196}