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