Completed
Push — CI ( ee6bd7...0f01dd )
by Adam
22:32
created

sugar_file_utilsTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 165
rs 10
c 1
b 0
f 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testsugar_mkdir() 0 16 1
A testsugar_fopen() 0 6 1
A testsugar_file_put_contents() 0 9 1
A testsugar_file_put_contents_atomic() 0 8 1
A testsugar_file_get_contents() 0 9 1
A testsugar_touch() 0 19 1
A testsugar_chmod() 0 8 1
A testsugar_chown() 0 12 1
A testsugar_chgrp() 0 11 1
A testget_mode() 0 9 1
A testsugar_is_dir() 0 9 1
A testsugar_is_file() 0 6 1
A testsugar_cached() 0 9 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 3.

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.

Loading history...
2
3
require_once 'include/utils/sugar_file_utils.php';
4
class sugar_file_utilsTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
}