]>
Commit | Line | Data |
---|---|---|
b2306b0c A |
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 | } |