|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
require_once 'include/utils/sugar_file_utils.php'; |
|
4
|
|
|
class sugar_file_utilsTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
public function setUp() { |
|
8
|
|
|
$rootFs = org\bovigo\vfs\vfsStream::setup('root'); |
|
9
|
|
|
$rootFs->addChild(org\bovigo\vfs\vfsStream::newDirectory('testDir')); |
|
10
|
|
|
$rootFs->addChild(org\bovigo\vfs\vfsStream::newFile('test.txt')->withContent("Hello world!")); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function testsugar_mkdir() |
|
14
|
|
|
{ |
|
15
|
|
|
//execute the method and test if it returns true and created dir exists |
|
16
|
|
|
|
|
17
|
|
|
$dir = "vfs://root"; |
|
18
|
|
|
|
|
19
|
|
|
//non recursive |
|
20
|
|
|
$result = sugar_mkdir($dir . "/mkdirTest"); |
|
21
|
|
|
$this->assertFileExists($dir . "/mkdirTest"); |
|
22
|
|
|
$this->assertTrue($result); |
|
23
|
|
|
|
|
24
|
|
|
//recursive |
|
25
|
|
|
$result = sugar_mkdir($dir . "/mkdirTest/test",null,true); |
|
26
|
|
|
$this->assertFileExists($dir . "/mkdirTest/test"); |
|
27
|
|
|
$this->assertTrue($result); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testsugar_fopen() |
|
31
|
|
|
{ |
|
32
|
|
|
//execute the method and test if it doesn't returns false |
|
33
|
|
|
$result = sugar_fopen('vfs://root/test.txt', 'r'); |
|
34
|
|
|
$this->assertNotFalse($result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public function testsugar_file_put_contents() |
|
39
|
|
|
{ |
|
40
|
|
|
//execute the method and test if it doesn't returns false and returns the number of bytes written |
|
41
|
|
|
|
|
42
|
|
|
$dir = "vfs://root"; |
|
43
|
|
|
$result = sugar_file_put_contents( $dir . '/testfile.txt', 'some test data'); |
|
44
|
|
|
$this->assertNotFalse($result); |
|
45
|
|
|
$this->assertEquals(14,$result); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function testsugar_file_put_contents_atomic() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->markTestSkipped('Atomic file put cannot be tested with vfsStream'); |
|
53
|
|
|
//execute the method and test if it returns success(true) |
|
54
|
|
|
$dir = "vfs://root"; |
|
55
|
|
|
$result = sugar_file_put_contents_atomic( $dir . '/atomictestfile.txt', 'some test data'); |
|
56
|
|
|
$this->assertTrue($result); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function testsugar_file_get_contents() |
|
61
|
|
|
{ |
|
62
|
|
|
//execute the method and test if it doesn't returns false and returns the expected contents |
|
63
|
|
|
$dir = "vfs://root"; |
|
64
|
|
|
$result = file_get_contents( $dir . '/test.txt'); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertNotFalse($result); |
|
67
|
|
|
$this->assertEquals('Hello world!',$result); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
public function testsugar_touch() |
|
72
|
|
|
{ |
|
73
|
|
|
//execute the method and test if it returns success(true) |
|
74
|
|
|
|
|
75
|
|
|
$dir = "vfs://root"; |
|
76
|
|
|
$test_dt = time() - 3600 ; |
|
77
|
|
|
$expected = date("m d Y H:i:s", time() - 3600 ); |
|
78
|
|
|
|
|
79
|
|
|
//test wihout modified date param |
|
80
|
|
|
$result = sugar_touch( $dir . '/testfiletouch.txt'); |
|
81
|
|
|
$this->assertTrue($result); |
|
82
|
|
|
|
|
83
|
|
|
//test wih modified date param |
|
84
|
|
|
$result = sugar_touch( $dir . '/testfiletouch.txt',$test_dt,$test_dt); |
|
85
|
|
|
$file_dt = date ("m d Y H:i:s", filemtime($dir . '/testfiletouch.txt')) ; |
|
86
|
|
|
|
|
87
|
|
|
$this->assertTrue($result); |
|
88
|
|
|
$this->assertSame($file_dt, $expected ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
public function testsugar_chmod() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->markTestSkipped('Permissions cannot be tested with vfsStream'); |
|
95
|
|
|
//execute the method and test if it returns success(true) |
|
96
|
|
|
$dir = "vfs://test"; |
|
97
|
|
|
$result = sugar_chmod($dir . '/test.txt',0777); |
|
98
|
|
|
$this->assertTrue($result); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
public function testsugar_chown() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->markTestSkipped('Permissions cannot be tested with vfsStream'); |
|
105
|
|
|
//execute the method and test if it returns success(true) |
|
106
|
|
|
$dir = "vfs://test"; |
|
107
|
|
|
$result = sugar_chown($dir . '/test.txt'); |
|
108
|
|
|
$this->assertFalse($result); |
|
109
|
|
|
|
|
110
|
|
|
$result = sugar_chown($dir . '/test.txt',org\bovigo\vfs\vfsStream::getCurrentUser()); |
|
111
|
|
|
$this->assertTrue($result); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
public function testsugar_chgrp() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->markTestSkipped('Permissions cannot be tested with vfsStream'); |
|
119
|
|
|
//execute the method and test if it returns success(true) |
|
120
|
|
|
$dir = "vfs://test"; |
|
121
|
|
|
$result = sugar_chgrp($dir . '/test.txt'); |
|
122
|
|
|
$this->assertFalse($result); |
|
123
|
|
|
|
|
124
|
|
|
$result = sugar_chgrp($dir . '/test.txt',org\bovigo\vfs\vfsStream::getCurrentGroup()); |
|
125
|
|
|
$this->assertFalse($result); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
public function testget_mode() |
|
130
|
|
|
{ |
|
131
|
|
|
//test with all mods defined in config |
|
132
|
|
|
$this->assertSame(1528,get_mode()); |
|
133
|
|
|
$this->assertSame(1528,get_mode('dir_mode', 10)); |
|
134
|
|
|
$this->assertSame(493,get_mode('file_mode', 10)); |
|
135
|
|
|
$this->assertSame('',get_mode('user', 10)); |
|
136
|
|
|
$this->assertSame('',get_mode('group', 10)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function testsugar_is_dir() |
|
140
|
|
|
{ |
|
141
|
|
|
$dir = "vfs://root"; |
|
142
|
|
|
|
|
143
|
|
|
$this->assertFalse(sugar_is_dir('')); //invalid dir |
|
144
|
|
|
$this->assertFalse(sugar_is_dir($dir."/foo")); //invalid dir |
|
145
|
|
|
$this->assertTrue(sugar_is_dir($dir."/testDir")); //valid dir |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testsugar_is_file() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->assertFalse(sugar_is_file('')); //invalid file |
|
152
|
|
|
$this->assertFalse(sugar_is_file('vfs://config')); //invalid file |
|
153
|
|
|
$this->assertTrue(is_file('vfs://root/test.txt')); //valid file |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
public function testsugar_cached() |
|
158
|
|
|
{ |
|
159
|
|
|
$cache_dir = rtrim($GLOBALS['sugar_config']['cache_dir'], '/\\'); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertSame($cache_dir . '/', sugar_cached('')); //invalid file |
|
162
|
|
|
$this->assertSame($cache_dir . '/config', sugar_cached('config')); //valid file |
|
163
|
|
|
$this->assertSame($cache_dir . '/modules' , sugar_cached('modules')); //valid file |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.