3 defined('AK_TEST_DATABASE_ON') ?
null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__
).'/../../../fixtures/config/config.php');
6 require_once(AK_LIB_DIR
.DS
.'Ak.php');
8 class test_of_Ak_file_functions
extends UnitTestCase
10 function Test_file_put_contents()
12 $file_name = AK_TMP_DIR
.DS
.'test_file_1.txt';
13 $content = 'This is the content of file 1';
14 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
16 $file_name = '/cache'.DS
.'test_file_1.txt';
17 $content = 'This is the NEW content for file 1';
18 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
20 $file_name = AK_TMP_DIR
.DS
.'test_file_2.txt';
21 $content = "\n\rThis is the content of file 2\n";
22 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
24 $file_name = 'cache'.DS
.'test_file_3.txt';
25 $content = "\rThis is the content of file 3\r\n";
26 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
28 $file_name = 'cache/test_file_4.txt';
29 $content = "\rThis is the content of file 4\r\n";
30 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
32 $file_name = 'ak_test_folder/test_file.txt';
33 $content = "\rThis is the content of the test file";
34 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
36 $file_name = 'ak_test_folder/new_folder/test_file.txt';
37 $content = "\rThis is the content of the test file";
38 $this->assertFalse(!Ak
::file_put_contents($file_name, $content));
42 function Test_file_get_contents()
44 $file_name = AK_TMP_DIR
.DS
.'test_file_1.txt';
45 $content = 'This is the NEW content for file 1';
46 $this->assertFalse(!Ak
::file_get_contents($file_name) === $content);
48 $file_name = AK_TMP_DIR
.DS
.'test_file_2.txt';
49 $content = "\n\rThis is the content of file 2\n";
50 $this->assertFalse(!Ak
::file_get_contents($file_name) === $content);
52 $file_name = 'cache'.DS
.'test_file_3.txt';
53 $content = "\rThis is the content of file 3\r\n";
54 $this->assertFalse(!Ak
::file_get_contents($file_name) === $content);
56 $file_name = 'cache/test_file_4.txt';
57 $content = "\rThis is the content of file 4\r\n";
58 $this->assertFalse(!Ak
::file_get_contents($file_name) === $content);
62 function Test_copy_files()
64 $original_path = AK_TMP_DIR
.DS
.'test_file_1.txt';
65 $copy_path = $original_path.'.copy';
66 $this->assertTrue(Ak
::copy($original_path, $copy_path));
67 $this->assertEqual(Ak
::file_get_contents($original_path), Ak
::file_get_contents($copy_path));
68 $this->assertTrue(Ak
::file_delete($copy_path));
71 function Test_copy_directories()
73 $original_path = 'ak_test_folder';
74 $copy_path = $original_path.'_copy';
75 $this->assertTrue(Ak
::copy($original_path,$copy_path));
77 $file_name = $copy_path.'/new_folder/test_file.txt';
78 $content = "\rThis is the content of the test file";
79 $this->assertTrue(Ak
::file_get_contents($file_name) === $content);
82 function Test_file_delete()
84 $this->assertFalse(!Ak
::file_delete(AK_TMP_DIR
.DS
.'test_file_1.txt'));
85 $this->assertFalse(!Ak
::file_delete(AK_TMP_DIR
.DS
.'test_file_2.txt'));
86 $this->assertFalse(!Ak
::file_delete('cache/test_file_3.txt'));
87 $this->assertFalse(!Ak
::file_delete('cache/test_file_4.txt'));
88 $this->assertFalse(!Ak
::file_delete('ak_test_folder/new_folder/test_file.txt'));
92 function Test_directory_delete()
94 $this->assertFalse(!Ak
::directory_delete('ak_test_folder'));
95 $this->assertFalse(!Ak
::directory_delete('ak_test_folder_copy'));
96 $this->assertFalse(Ak
::directory_delete('../../'));
97 $this->assertFalse(Ak
::directory_delete('..\..\\'));
98 $this->assertFalse(Ak
::directory_delete(' '));
99 $this->assertFalse(Ak
::directory_delete('/'));
100 $this->assertFalse(Ak
::directory_delete('./'));
103 $this->assertFalse(is_dir(AK_BASE_DIR
.DS
.'ak_test_folder'), 'Did not remove empty dir ./ak_test_folder');
104 $this->assertFalse(is_dir(AK_BASE_DIR
.DS
.'ak_test_folder_copy'), 'Did not remove nested dir ./ak_test_folder_copy');
107 function test_mime_type_detection()
109 // png is not in any RFC so we might want to check if it has a /x- preffix for non standard values
110 $this->assertTrue(in_array(Ak
::mime_content_type(AK_PUBLIC_DIR
.DS
.'images'.DS
.'akelos_framework_logo.png'),array('image/png','image/x-png')));
111 $this->assertEqual(Ak
::mime_content_type('C:\Folder\image.png'),'image/png');
114 function test_should_read_files_using_scoped_file_get_contents_function()
116 $this->assertEqual(Ak
::file_get_contents(AK_LIB_DIR
.DS
.'AkActiveRecord.php'), file_get_contents(AK_LIB_DIR
.DS
.'AkActiveRecord.php'));
119 function test_dir_should_not_recurse_when_set_to_false()
121 $files_and_dirs = Ak
::dir(AK_LIB_DIR
.DS
.'AkActionView', array('dirs' => true, 'recurse' => false));
122 foreach ($files_and_dirs as $k=>$file_or_dir){
123 if(is_array($file_or_dir)){
124 $this->assertEqual(count($files_and_dirs[$k]), 1);
129 function test_should_delete_nested_directories_when_include_hidden_files()
131 $dir_name = AK_TMP_DIR
.Ak
::randomString();
132 Ak
::make_dir($dir_name);
133 Ak
::make_dir($dir_name.DS
.'.hidden');
134 $this->assertTrue(is_dir($dir_name), 'Could not create test directory '.$dir_name);
135 $this->assertTrue(Ak
::directory_delete($dir_name));
137 $this->assertFalse(is_dir($dir_name));
141 ak_test('test_of_Ak_file_functions');