-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CFileHelperTest.php
124 lines (107 loc) · 4.32 KB
/
CFileHelperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class CFileHelperTest extends CTestCase
{
private $testDir;
private $testMode = 0770;
private $rootDir1 = "test1";
private $rootDir2 = "test2";
private $subDir = 'sub';
private $file = 'testfile';
protected function setUp()
{
$this->testDir = Yii::getPathOfAlias('application.runtime.CFileHelper');
if(!is_dir($this->testDir) &&
!(@mkdir($this->testDir)))
$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
// create temporary testing data files
$filesData=array(
'mimeTypes1.php'=>"<?php return array('txa'=>'application/json','txb'=>'another/mime');",
'mimeTypes2.php'=>"<?php return array('txt'=>'text/plain','txb'=>'another/mime2');",
);
foreach($filesData as $fileName=>$fileData)
if(!(@file_put_contents($this->testDir.$fileName,$fileData)))
$this->markTestIncomplete('Unit tests runtime directory should have writable permissions!');
}
protected function tearDown()
{
if (is_dir($this->testDir)) $this->rrmdir($this->testDir);
}
public function testGetMimeTypeByExtension()
{
// run everything ten times in one test action to be sure that caching inside
// CFileHelper::getMimeTypeByExtension() is working the right way
for($i=0; $i<10; $i++)
{
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txa'));
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txb'));
$this->assertEquals('text/plain',CFileHelper::getMimeTypeByExtension('test.txt'));
$this->assertEquals('application/json',CFileHelper::getMimeTypeByExtension('test.txa',$this->testDir.'mimeTypes1.php'));
$this->assertEquals('another/mime',CFileHelper::getMimeTypeByExtension('test.txb',$this->testDir.'mimeTypes1.php'));
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txt',$this->testDir.'mimeTypes1.php'));
$this->assertNull(CFileHelper::getMimeTypeByExtension('test.txa',$this->testDir.'mimeTypes2.php'));
$this->assertEquals('another/mime2',CFileHelper::getMimeTypeByExtension('test.txb',$this->testDir.'mimeTypes2.php'));
$this->assertEquals('text/plain',CFileHelper::getMimeTypeByExtension('test.txt',$this->testDir.'mimeTypes2.php'));
}
}
public function testCopyDirectory_subDir_modeShoudBe0775()
{
if (substr(PHP_OS, 0, 3) == 'WIN') {
$this->markTestSkipped("Can't reliably test it on Windows because fileperms() always return 0777.");
}
$this->createTestStruct($this->testDir);
$src = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
$dst = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir2;
CFileHelper::copyDirectory($src, $dst, array('newDirMode' => $this->testMode));
$subDir2Mode = $this->getMode($dst . DIRECTORY_SEPARATOR . $this->subDir );
$expectedMode = sprintf('%o', $this->testMode);
$this->assertEquals($expectedMode, $subDir2Mode, "Subdir mode is not {$expectedMode}");
}
public function testCopyDirectory_subDir_modeShoudBe0777()
{
if (substr(PHP_OS, 0, 3) == 'WIN') {
$this->markTestSkipped("Can't reliably test it on Windows because fileperms() always return 0777.");
}
$this->createTestStruct($this->testDir);
$src = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
$dst = $this->testDir . DIRECTORY_SEPARATOR . $this->rootDir2;
CFileHelper::copyDirectory($src, $dst);
$subDir2Mode = $this->getMode($dst . DIRECTORY_SEPARATOR . $this->subDir );
$expectedMode = sprintf('%o', 0777);
$this->assertEquals($expectedMode, $subDir2Mode, "Subdir mode is not {$expectedMode}");
}
private function createTestStruct($testDir)
{
$rootDir = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1;
mkdir($rootDir);
$subDir = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1 . DIRECTORY_SEPARATOR . $this->subDir;
mkdir($subDir);
$file = $testDir . DIRECTORY_SEPARATOR . $this->rootDir1 . DIRECTORY_SEPARATOR . $this->subDir . DIRECTORY_SEPARATOR . $this->file;
file_put_contents($file, '12321312');
}
private function getMode($file)
{
return substr(sprintf('%o', fileperms($file)), -4);
}
private function rrmdir($dir)
{
if ($handle = opendir($dir))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
if (is_dir($dir . "/" . $entry) === true)
{
$this->rrmdir($dir . "/" . $entry);
}
else
{
unlink($dir . "/" . $entry);
}
}
}
closedir($handle);
rmdir($dir);
}
}
}