[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/fs/tests/cases/ -> lmbFsTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/fs/src/lmbFs.class.php');
  10  
  11  class SpecialDirWalker
  12  {
  13    var $walked = array();
  14    var $counter = 0;
  15  
  16    function walk($dir, $file, $path, $params, &$return_params)
  17    {
  18      $this->walked[] = lmbFs :: normalizePath($path);
  19      $return_params[] = $this->counter++;
  20    }
  21  }
  22  
  23  class lmbFsTest extends UnitTestCase
  24  {
  25    function _createFileSystem()
  26    {
  27      lmbFs :: mkdir(LIMB_VAR_DIR . '/tmp/wow/hey/');
  28  
  29      touch(LIMB_VAR_DIR . '/tmp/test1_1');
  30      touch(LIMB_VAR_DIR . '/tmp/test1_2');
  31      touch(LIMB_VAR_DIR . '/tmp/test1_3');
  32  
  33      touch(LIMB_VAR_DIR . '/tmp/wow/test2_1');
  34      touch(LIMB_VAR_DIR . '/tmp/wow/test2_2');
  35      touch(LIMB_VAR_DIR . '/tmp/wow/test2_3');
  36  
  37      touch(LIMB_VAR_DIR . '/tmp/wow/hey/test3_1');
  38      touch(LIMB_VAR_DIR . '/tmp/wow/hey/test3_2');
  39      touch(LIMB_VAR_DIR . '/tmp/wow/hey/test3_3');
  40    }
  41  
  42    function _removeFileSystem()
  43    {
  44      $this->_rmdir(LIMB_VAR_DIR . '/tmp/');
  45    }
  46  
  47    function _rmdir($path)
  48    {
  49      if(!is_dir($path))
  50        return;
  51  
  52      $dir = opendir($path);
  53      while($entry = readdir($dir))
  54      {
  55        if(is_file("$path/$entry"))
  56          unlink("$path/$entry");
  57        elseif(is_dir("$path/$entry") && $entry != '.' && $entry != '..')
  58          $this->_rmdir("$path/$entry");
  59      }
  60      closedir($dir);
  61      $res = rmdir($path);
  62      clearstatcache();
  63      return $res;
  64    }
  65  
  66    //make multiprocess test someday

  67    function testSafeWrite()
  68    {
  69      lmbFs :: safeWrite(LIMB_VAR_DIR . '/test', 'test');
  70      $this->assertEqual('test',
  71                         file_get_contents(LIMB_VAR_DIR . '/test'));
  72    }
  73  
  74    function testJoinPath()
  75    {
  76      $path = lmbFs :: joinPath(array('wow', 'hey', 'yo'), lmbFs :: UNIX);
  77      $this->assertEqual($path, 'wow/hey/yo');
  78    }
  79  
  80    function testRemoveNoSuchFile()
  81    {
  82      $this->assertFalse(lmbFs :: rm('blaaaaaaaaaaaaaaaah'));
  83    }
  84  
  85    function testRemoveFile()
  86    {
  87      lmbFs :: safeWrite($file = LIMB_VAR_DIR . '/test', 'test');
  88      $this->assertTrue(lmbFs :: rm($file));
  89      $this->assertFalse(file_exists($file));
  90    }
  91  
  92    function testRemoveDirectory()
  93    {
  94      $this->_createFileSystem();
  95  
  96      $this->assertTrue(lmbFs :: rm(LIMB_VAR_DIR . '/tmp/'));
  97      $this->assertFalse(is_dir(LIMB_VAR_DIR . '/tmp/'));
  98    }
  99  
 100    function testIsPathAbsolute()
 101    {
 102      $this->assertTrue(lmbFs :: isPathAbsolute('c:/var/wow', lmbFs :: DOS));
 103      $this->assertTrue(lmbFs :: isPathAbsolute('/var/wow', lmbFs :: UNIX));
 104      $this->assertTrue(lmbFs :: isPathAbsolute('/var/wow', lmbFs :: DOS));
 105      $this->assertFalse(lmbFs :: isPathAbsolute('c:/var/wow', lmbFs :: UNIX));
 106  
 107      $this->assertFalse(lmbFs :: isPathAbsolute('var/wow'));
 108    }
 109  
 110    function testNormalizeUglyPath()
 111    {
 112      $path = lmbFs :: normalizePath('/tmp\../tmp/wow////hey/');
 113      $this->assertEqual($path, lmbFs :: separator() . 'tmp' . lmbFs :: separator() . 'wow' . lmbFs :: separator() . 'hey');
 114  
 115      $path = lmbFs :: normalizePath('tmp\../tmp/wow////hey/');
 116      $this->assertEqual($path, 'tmp' . lmbFs :: separator() . 'wow' . lmbFs :: separator() . 'hey');
 117    }
 118  
 119    function testNormalizePathForWindows()
 120    {
 121      $path = lmbFs :: normalizePath('c:\\var\\dev\\demo\\design\\templates\\test.html');
 122  
 123      $this->assertEqual($path,
 124        'c:' . lmbFs :: separator() .
 125        'var' . lmbFs :: separator() .
 126        'dev' . lmbFs :: separator() .
 127        'demo' . lmbFs :: separator() .
 128        'design' . lmbFs :: separator() .
 129        'templates' . lmbFs :: separator() .
 130        'test.html');
 131    }
 132  
 133    function testNormalizePathTrimTrailingSlashes()
 134    {
 135      $path1 = lmbFs :: normalizePath('/tmp/wow////hey/\\');
 136      $path2 = lmbFs :: normalizePath('/tmp\\wow//../wow/hey');
 137      $this->assertEqual($path1, $path2);
 138      $this->assertEqual($path1, lmbFs :: separator() . 'tmp' . lmbFs :: separator() . 'wow' . lmbFs :: separator() . 'hey');
 139    }
 140  
 141    function testExplodeAbsolutePath()
 142    {
 143      $path = lmbFs :: explodePath('/tmp\../tmp/wow////hey/');
 144  
 145      $this->assertEqual(sizeof($path), 4);
 146  
 147      $this->assertEqual($path[0], '');
 148      $this->assertEqual($path[1], 'tmp');
 149      $this->assertEqual($path[2], 'wow');
 150      $this->assertEqual($path[3], 'hey');
 151  
 152      $path = lmbFs :: explodePath('/tmp\../tmp/wow////hey'); // no trailing slash

 153  
 154      $this->assertEqual(sizeof($path), 4);
 155  
 156      $this->assertEqual($path[0], '');
 157      $this->assertEqual($path[1], 'tmp');
 158      $this->assertEqual($path[2], 'wow');
 159      $this->assertEqual($path[3], 'hey');
 160    }
 161  
 162    function testExplodeRelativePath()
 163    {
 164      $path = lmbFs :: explodePath('tmp\../tmp/wow////hey/');
 165  
 166      $this->assertEqual(sizeof($path), 3);
 167  
 168      $this->assertEqual($path[0], 'tmp');
 169      $this->assertEqual($path[1], 'wow');
 170      $this->assertEqual($path[2], 'hey');
 171  
 172      $path = lmbFs :: explodePath('tmp\../tmp/wow////hey'); // no trailing slash

 173  
 174      $this->assertEqual(sizeof($path), 3);
 175  
 176      $this->assertEqual($path[0], 'tmp');
 177      $this->assertEqual($path[1], 'wow');
 178      $this->assertEqual($path[2], 'hey');
 179    }
 180  
 181    function testMkdirAbsolutePath()
 182    {
 183      lmbFs :: rm(LIMB_VAR_DIR . '/tmp/');
 184  
 185      $this->assertFalse(is_dir(LIMB_VAR_DIR . '/tmp/wow/hey/'));
 186  
 187      lmbFs :: mkdir(LIMB_VAR_DIR . '/./tmp\../tmp/wow////hey/');
 188  
 189      $this->assertTrue(is_dir(LIMB_VAR_DIR . '/tmp/wow/hey/'));
 190    }
 191  
 192    function testDirpath()
 193    {
 194      $this->assertEqual(lmbFs :: dirpath('/wow/test.txt'), lmbFs :: normalizePath('/wow'));
 195      $this->assertEqual(lmbFs :: dirpath('wow/hey/test.txt'), lmbFs :: normalizePath('wow/hey'));
 196      $this->assertEqual(lmbFs :: dirpath('test.txt'), 'test.txt');
 197      $this->assertEqual(lmbFs :: dirpath('/'), '');
 198    }
 199  
 200    function testLs()
 201    {
 202      $this->_createFileSystem();
 203  
 204      $a1 = array('test1_1', 'test1_2', 'test1_3', 'wow');
 205      sort($a1);
 206      $a2 =  lmbFs :: ls(LIMB_VAR_DIR . '/tmp/');
 207      sort($a2);
 208  
 209      $this->assertEqual($a1, $a2);
 210      $this->assertEqual(array('hey', 'test2_1', 'test2_2', 'test2_3'), lmbFs :: ls(LIMB_VAR_DIR . '/tmp/wow'));
 211  
 212      $this->_removeFileSystem();
 213    }
 214  
 215    function testPath()
 216    {
 217      $this->assertEqual(lmbFs :: path(array('test')), 'test');
 218      $this->assertEqual(lmbFs :: path(array('test', 'wow')), 'test' . lmbFs :: separator() . 'wow');
 219      $this->assertEqual(lmbFs :: path(array('test', 'wow/')), 'test' . lmbFs :: separator() . 'wow');
 220  
 221      $this->assertEqual(lmbFs :: path(array('test'), true), 'test' . lmbFs :: separator());
 222      $this->assertEqual(lmbFs :: path(array('test', 'wow'), true), 'test' . lmbFs :: separator() . 'wow' . lmbFs :: separator());
 223    }
 224  
 225    function testChop()
 226    {
 227      $this->assertEqual(lmbFs :: chop('test'), 'test');
 228      $this->assertEqual(lmbFs :: chop('test/'), 'test');
 229      $this->assertEqual(lmbFs :: chop('test\\'), 'test');
 230    }
 231  
 232    function testWalkDir()
 233    {
 234      $this->_createFileSystem();
 235  
 236      $mock = new SpecialDirWalker();
 237  
 238      $this->assertEqual(
 239        lmbFs :: walkDir(LIMB_VAR_DIR . '/tmp/',
 240                      array(&$mock, 'walk'),
 241                      array('test')),
 242        array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 243      );
 244  
 245      sort($mock->walked);
 246  
 247      $this->assertEqual(sizeof($mock->walked), 11);
 248  
 249      $this->assertEqual($mock->walked[0], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_1'));
 250      $this->assertEqual($mock->walked[1], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_2'));
 251      $this->assertEqual($mock->walked[2], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_3'));
 252      $this->assertEqual($mock->walked[3], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow'));
 253      $this->assertEqual($mock->walked[4], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey'));
 254      $this->assertEqual($mock->walked[5], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_1'));
 255      $this->assertEqual($mock->walked[6], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_2'));
 256      $this->assertEqual($mock->walked[7], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_3'));
 257      $this->assertEqual($mock->walked[8], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_1'));
 258      $this->assertEqual($mock->walked[9], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_2'));
 259      $this->assertEqual($mock->walked[10], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_3'));
 260  
 261      $this->_removeFileSystem();
 262    }
 263  
 264    function testWalkDirIncludeFirst()
 265    {
 266      $this->_createFileSystem();
 267  
 268      $mock = new SpecialDirWalker();
 269  
 270      $this->assertEqual(
 271        $res = lmbFs :: walkDir(LIMB_VAR_DIR . '/tmp/',
 272                       array(&$mock, 'walk'),
 273                       array('test'),
 274                       true),
 275        array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
 276      );
 277  
 278      sort($mock->walked);
 279  
 280      $this->assertEqual(sizeof($mock->walked), 12);
 281  
 282      $this->assertEqual($mock->walked[0], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp'));
 283      $this->assertEqual($mock->walked[1], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_1'));
 284      $this->assertEqual($mock->walked[2], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_2'));
 285      $this->assertEqual($mock->walked[3], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/test1_3'));
 286      $this->assertEqual($mock->walked[4], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow'));
 287      $this->assertEqual($mock->walked[5], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey'));
 288      $this->assertEqual($mock->walked[6], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_1'));
 289      $this->assertEqual($mock->walked[7], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_2'));
 290      $this->assertEqual($mock->walked[8], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/hey/test3_3'));
 291      $this->assertEqual($mock->walked[9], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_1'));
 292      $this->assertEqual($mock->walked[10], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_2'));
 293      $this->assertEqual($mock->walked[11], lmbFs :: normalizePath(LIMB_VAR_DIR . '/tmp/wow/test2_3'));
 294  
 295      $this->_removeFileSystem();
 296    }
 297  
 298    function testMv()
 299    {
 300      $this->_createFileSystem();
 301  
 302      lmbFs :: mv(LIMB_VAR_DIR . '/tmp/wow',
 303                  LIMB_VAR_DIR . '/tmp/whatever');
 304  
 305      $this->assertFalse(is_dir(LIMB_VAR_DIR . '/tmp/wow'));
 306      $this->assertTrue(is_dir(LIMB_VAR_DIR . '/tmp/whatever'));
 307    }
 308  
 309    function testMoveOntoItselfDoesNothing()
 310    {
 311      $this->_createFileSystem();
 312  
 313      lmbFs :: mv(LIMB_VAR_DIR . '/tmp/wow',
 314                  LIMB_VAR_DIR . '/tmp/wow');
 315  
 316      $this->assertTrue(is_dir(LIMB_VAR_DIR . '/tmp/wow'));
 317    }
 318  
 319    function testMoveNonExistingFails()
 320    {
 321      $this->_createFileSystem();
 322  
 323      try
 324      {
 325        lmbFs :: mv(LIMB_VAR_DIR . '/tmp/blaaah',
 326                    LIMB_VAR_DIR . '/tmp/cp');
 327        $this->assertFalse(true);
 328      }
 329      catch(lmbFsException $e){}
 330    }
 331  
 332    function testCpDirs()
 333    {
 334      $this->_createFileSystem();
 335  
 336      $res = lmbFs :: cp(LIMB_VAR_DIR . '/tmp/wow',
 337                         LIMB_VAR_DIR . '/tmp/cp');
 338      sort($res);
 339  
 340      $this->assertEqual(
 341        $res,
 342        array(
 343        'hey',
 344        lmbFs :: normalizePath('hey/test3_1'),
 345        lmbFs :: normalizePath('hey/test3_2'),
 346        lmbFs :: normalizePath('hey/test3_3'),
 347        'test2_1',
 348        'test2_2',
 349        'test2_3',
 350        )
 351      );
 352  
 353      $this->assertEqual(
 354        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/cp'),
 355        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/wow'));
 356  
 357      $this->assertEqual(
 358        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/cp/hey'),
 359        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/wow/hey'));
 360  
 361      $this->_removeFileSystem();
 362    }
 363  
 364    function testCpAsChild()
 365    {
 366      $this->_createFileSystem();
 367  
 368      lmbFs :: cp(LIMB_VAR_DIR . '/tmp/wow',
 369                  LIMB_VAR_DIR . '/tmp/cp', null, null, true);
 370  
 371      $this->assertEqual(
 372        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/cp/wow/'),
 373        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/wow'));
 374  
 375      $this->assertEqual(
 376        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/cp/wow/hey'),
 377        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/wow/hey'));
 378  
 379      $this->_removeFileSystem();
 380    }
 381  
 382    function testCpDirsWithExclude()
 383    {
 384      $this->_createFileSystem();
 385  
 386      $res = lmbFs :: cp(LIMB_VAR_DIR . '/tmp/wow',
 387                         LIMB_VAR_DIR . '/tmp/cp', '/hey/');
 388      sort($res);
 389  
 390      $this->assertEqual(
 391        $res,
 392        array('test2_1', 'test2_2', 'test2_3')
 393      );
 394  
 395      $this->assertEqual(
 396        $res,
 397        lmbFs :: ls(LIMB_VAR_DIR . '/tmp/cp/')
 398      );
 399  
 400      $this->assertFalse(is_dir(LIMB_VAR_DIR . '/tmp/cp/hey'));
 401  
 402      $this->_removeFileSystem();
 403    }
 404  
 405    function testCpDirsWithInclude()
 406    {
 407      $this->_createFileSystem();
 408  
 409      $res = lmbFs :: cp(LIMB_VAR_DIR . '/tmp/wow',
 410                         LIMB_VAR_DIR . '/tmp/cp', null, '/test2/');
 411  
 412      $this->assertEqual(
 413        $res,
 414        array('test2_1', 'test2_2', 'test2_3')
 415      );
 416  
 417      $this->assertEqual(
 418        $res,
 419        lmbFs ::