]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/CreateCacheDirectoryTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / utils / CreateCacheDirectoryTest.php
1 <?php
2 require_once 'include/utils/file_utils.php';
3
4 class CreateCacheDirectoryTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     private $_original_cwd = '';
7     
8     public function setUp() 
9     {
10         $this->_original_cwd = getcwd();
11         chdir(dirname(__FILE__));
12         $this->_removeCacheDirectory('./cache');
13     }
14
15     public function tearDown() 
16     {
17         $this->_removeCacheDirectory('./cache');
18         chdir($this->_original_cwd);
19     }
20
21     private function _removeCacheDirectory($dir)
22     {
23         $dir_handle = @opendir($dir);
24         if ($dir_handle === false) {
25             return;
26         }
27         while (($filename = readdir($dir_handle)) !== false) {
28             if ($filename == '.' || $filename == '..') {
29                 continue;
30             }
31             if (is_dir("{$dir}/{$filename}")) {
32                 $this->_removecacheDirectory("{$dir}/{$filename}");
33             } else {
34                 unlink("{$dir}/{$filename}");
35             }
36         }
37         closedir($dir_handle);
38         rmdir("{$dir}");
39     }
40
41     public function testCreatesCacheDirectoryIfDoesnotExist()
42     {
43         $this->assertFalse(file_exists('./cache'), 'check that the cache directory does not exist');
44         create_cache_directory('foobar');
45         $this->assertTrue(file_exists('./cache'), 'creates a cache directory');
46     }
47
48     public function testCreatesDirectoryInCacheDirectoryProvidedItIsGivenAFile()
49     {
50         $this->assertFalse(file_exists('./cache/foobar-test'));
51         create_cache_directory('foobar-test/cache-file.php');
52         $this->assertTrue(file_exists('./cache/foobar-test'));
53     }
54
55     public function testReturnsDirectoryCreated()
56     {
57         $created = create_cache_directory('foobar/cache-file.php');
58         $this->assertEquals(
59             'cache/foobar/cache-file.php',
60             $created
61         );
62     }
63 }
64