aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/FileUtilsTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-11-12 13:11:07 +0100
committerArthurHoaro <arthur@hoa.ro>2020-11-12 13:11:07 +0100
commitaf50eba28a7bd286de4c8c9ee6dc5216b915d149 (patch)
treeffa30a9358e82d27be75d8fc5e57f3c8820dc6d3 /tests/FileUtilsTest.php
parentb6f678a5a1d15acf284ebcec16c905e976671ce1 (diff)
parent1409f1c89a7ca01456ae2dcd6357d296e2b99f5a (diff)
downloadShaarli-latest.tar.gz
Shaarli-latest.tar.zst
Shaarli-latest.zip
Merge tag 'v0.12.1' into latestlatest
v0.12.1
Diffstat (limited to 'tests/FileUtilsTest.php')
-rw-r--r--tests/FileUtilsTest.php110
1 files changed, 0 insertions, 110 deletions
diff --git a/tests/FileUtilsTest.php b/tests/FileUtilsTest.php
deleted file mode 100644
index 9163bdf1..00000000
--- a/tests/FileUtilsTest.php
+++ /dev/null
@@ -1,110 +0,0 @@
1<?php
2
3namespace Shaarli;
4
5use Exception;
6
7/**
8 * Class FileUtilsTest
9 *
10 * Test file utility class.
11 */
12class FileUtilsTest extends \Shaarli\TestCase
13{
14 /**
15 * @var string Test file path.
16 */
17 protected static $file = 'sandbox/flat.db';
18
19 /**
20 * Delete test file after every test.
21 */
22 protected function tearDown(): void
23 {
24 @unlink(self::$file);
25 }
26
27 /**
28 * Test writeDB, then readDB with different data.
29 */
30 public function testSimpleWriteRead()
31 {
32 $data = ['blue', 'red'];
33 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
34 $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
35 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
36
37 $data = 0;
38 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
39 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
40
41 $data = null;
42 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
43 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
44
45 $data = false;
46 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
47 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
48 }
49
50 /**
51 * File not writable: raise an exception.
52 */
53 public function testWriteWithoutPermission()
54 {
55 $this->expectException(\Shaarli\Exceptions\IOException::class);
56 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
57
58 touch(self::$file);
59 chmod(self::$file, 0440);
60 FileUtils::writeFlatDB(self::$file, null);
61 }
62
63 /**
64 * Folder non existent: raise an exception.
65 */
66 public function testWriteFolderDoesNotExist()
67 {
68 $this->expectException(\Shaarli\Exceptions\IOException::class);
69 $this->expectExceptionMessage('Error accessing "nopefolder"');
70
71 FileUtils::writeFlatDB('nopefolder/file', null);
72 }
73
74 /**
75 * Folder non writable: raise an exception.
76 */
77 public function testWriteFolderPermission()
78 {
79 $this->expectException(\Shaarli\Exceptions\IOException::class);
80 $this->expectExceptionMessage('Error accessing "sandbox"');
81
82 chmod(dirname(self::$file), 0555);
83 try {
84 FileUtils::writeFlatDB(self::$file, null);
85 } catch (Exception $e) {
86 chmod(dirname(self::$file), 0755);
87 throw $e;
88 }
89 }
90
91 /**
92 * Read non existent file, use default parameter.
93 */
94 public function testReadNotExistentFile()
95 {
96 $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
97 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
98 }
99
100 /**
101 * Read non readable file, use default parameter.
102 */
103 public function testReadNotReadable()
104 {
105 touch(self::$file);
106 chmod(self::$file, 0220);
107 $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
108 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
109 }
110}