<?php

# $HeadURL: https://svn.sigpipe.cz/r/trunk/testilence/tests/tempdir.php $
# $Id: tempdir.php 282 2006-12-16 19:18:28Z roman $


class tencetest_UniqueFilenameGeneratorExhaustionTest extends Tence_TestCase # {{{
implements Tence_util_StringGenerator
{
    private 
$filename$prefix$path;
    function 
generateString($len# {{{
    
{
        return 
$this->filename;
    } 
# }}}
    
function setUp() # {{{
    
{
        
$this->prefix '/tmp';
        
$this->filename 'fixedName';
        
$this->path sprintf('%s/%s'$this->prefix$this->filename);
        
touch($this->path);
    } 
# }}}
    
function tearDown() # {{{
    
{
        
unlink($this->path);
    } 
# }}}
    
function test_generateFilenameThrowsOnExhaustion() # {{{
    
{
        
$this->willThrow('Tence_Exception');
        new 
Tence_util_TempDir($this->prefix, new Tence_util_UniqueFilenameGenerator(1$this));
    } 
# }}}
# }}}

class tencetest_TempDirFreshTest extends Tence_TestCase # {{{
{
    protected 
$td$path;
    function 
setUp()
    {
        
$this->td $this->createDir();
        
$this->path $this->td->path();
    }
    protected function 
createDir() # {{{
    
{
        return new 
Tence_util_TempDir('/tmp');
    } 
# }}}
    
function test_constructorCreatesDirectory() # {{{
    
{
        return 
$this->assertTrue(file_exists($this->path) && is_dir($this->path));
    } 
# }}}
    
function test_constructorUsesDirnameArgument() # {{{
    
{
        
$child = new Tence_util_TempDir($this->path);
        return 
$this->assertEquals(
            
sprintf('%s/'$this->path),
            
substr($child->path(), 0strlen($this->path))
        );
    } 
# }}}
    
function tearDown() # {{{
    
{
        unset(
$this->td);
        
# just in case the test failed, and TempDir's
        # destructor didn't function for any reason
        
@rmdir($this->path);
    } 
# }}}
# }}}
class tencetest_TempDirRemovalTest extends Tence_TestCase # {{{
{
    function 
setUp() # {{{
    
{
        
$this->td = new Tence_util_TempDir('/tmp');
        
$this->dir $this->td->path();
        
$this->file sprintf('%s/file'$this->dir);
    } 
# }}}
    
function test_DestructorRemovesDirectory() # {{{
    
{
        unset(
$this->td);
        return 
$this->assertFalse(file_exists($this->dir));
    } 
# }}}
    
function test_DestructorRemovesDirectoryRecursively() # {{{
    
{
        
touch($this->file);
        unset(
$this->td);
        return 
$this->assertFalse(file_exists($this->dir));
    } 
# }}}
    
function tearDown() # {{{
    
{
        
# just in case TempDir's destructor didn't function for any reason
        # (IOW the test failed)
        
@unlink($this->file);
        @
rmdir($this->path);
    } 
# }}}
# }}}
class tencetest_TempDirDestructorWithoutDir extends Tence_TestCase # {{{
{
    function 
setUp() # {{{
    
{
        
$this->td = new Tence_util_TempDir('/tmp');
        
rmdir($this->td->path());
    } 
# }}}
    
function test_DestructorMustNotThrow() # {{{
    
{
        unset(
$this->td);
        return 
$this->assertTrue(true);
    } 
# }}}
# }}}
class tencetest_util_rm_rThrowsOnPermissionProblemsTest extends Tence_TestCase # {{{
{
    function 
setUp() # {{{
    
{
        
$this->td $this->mkdtemp('/tmp');
        
$this->child sprintf('%s/child'$this->td->path());
    } 
# }}}
    
private function barAccess() # {{{
    
{
        
$p escapeshellarg($this->td->path());
        
exec(
            
sprintf('chmod 0755 %1$s && sudo chown nobody %1$s'$p),
            
$out$rv
        
);
        if (
$rv) {
            throw new 
Exception(implode("\n"$out));
        }
    } 
# }}}
    
function test_unlinkFailure() # {{{
    
{
        
touch($this->child);
        
$this->barAccess($this->td->path());
        
$this->willThrow('Tence_util_unlinkFailed');
        
Tence_util::rm_r($this->td->path());
    } 
# }}}
    
function test_rmdirFailure() # {{{
    
{
        
mkdir($this->child);
        
$this->barAccess($this->td->path());
        
$this->willThrow('Tence_util_rmdirFailed');
        
Tence_util::rm_r($this->td->path());
    } 
# }}}
# }}}

class tencetest_TempDirDangerousTests extends Tence_TestSuite # {{{
{
    function 
__construct()
    {
        
$this
            
->add(new tencetest_UniqueFilenameGeneratorExhaustionTest)
            ->
add(new tencetest_TempDirFreshTest)
            ->
add(new tencetest_TempDirRemovalTest)
            ->
add(new tencetest_TempDirDestructorWithoutDir)
        ;
    }
# }}}

# vim: et ts=4 sts=4 sw=4 fdm=marker cms=\ #\ %s