]>
Commit | Line | Data |
---|---|---|
b2306b0c A |
1 | <?php |
2 | ||
a0c4dbd9 | 3 | namespace Shaarli; |
f3d2f257 | 4 | |
a0c4dbd9 | 5 | use Exception; |
b2306b0c A |
6 | |
7 | /** | |
8 | * Class FileUtilsTest | |
9 | * | |
10 | * Test file utility class. | |
11 | */ | |
a0c4dbd9 | 12 | class FileUtilsTest extends \PHPUnit\Framework\TestCase |
b2306b0c A |
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 | public function tearDown() | |
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 | * | |
f3d2f257 | 53 | * @expectedException Shaarli\Exceptions\IOException |
b2306b0c A |
54 | * @expectedExceptionMessage Error accessing "sandbox/flat.db" |
55 | */ | |
56 | public function testWriteWithoutPermission() | |
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 | * | |
f3d2f257 | 66 | * @expectedException Shaarli\Exceptions\IOException |
b2306b0c A |
67 | * @expectedExceptionMessage Error accessing "nopefolder" |
68 | */ | |
69 | public function testWriteFolderDoesNotExist() | |
70 | { | |
71 | FileUtils::writeFlatDB('nopefolder/file', null); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Folder non writable: raise an exception. | |
76 | * | |
f3d2f257 | 77 | * @expectedException Shaarli\Exceptions\IOException |
b2306b0c A |
78 | * @expectedExceptionMessage Error accessing "sandbox" |
79 | */ | |
80 | public function testWriteFolderPermission() | |
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 | } |