]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/FileUtilsTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / FileUtilsTest.php
1 <?php
2
3 namespace Shaarli;
4
5 use Exception;
6
7 /**
8 * Class FileUtilsTest
9 *
10 * Test file utility class.
11 */
12 class 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 }