<?php

# $HeadURL: https://svn.sigpipe.cz/r/trunk/testilence/tests/runner.php $
# $Id: runner.php 496 2008-06-03 05:37:47Z roman $


require_once dirname(__FILE__) . '/utils/reporter.php';

class 
tencetest_RunnerConfig
implements Tence_RunnerConfig
{
    function 
__construct(
        array 
$targets = array(),
        array 
$include_path = array(),
        
Tence_Reporter $reporter null
    
)
    {
        if (
is_null($reporter)) {
            
$this->reporter = new tencetest_QuietReporter;
        } else {
            
$this->reporter $reporter;
        }
        
$this->targets $targets;
        
$this->include_path $include_path;
    }
    function 
reporter()
    {
        return 
$this->reporter;
    }
    function 
targets()
    {
        return 
$this->targets;
    }
    function 
include_path()
    {
        return 
$this->include_path;
    }
}

abstract class 
tencetest_BasicRunnerTest extends Tence_TestCase # {{{
{
    protected function 
createConfig($fileName$className# {{{
    
{
        return new 
tencetest_RunnerConfig(
            array(new 
Tence_cmdline_util_Target(
                new 
Tence_TestClassFile($fileName),
                array(
$className)
            ))
        );
    } 
# }}}
    
protected function generateClassName() # {{{
    
{
        do {
            
$class sprintf('%s_%x'__CLASS__rand());
        } while (
class_exists($class));
        return 
$class;
    } 
# }}}
    
protected function createRunner()
    {
        return new 
Tence_BasicRunner(new Tence_BasicLoader);
    }
# }}}
class tencetest_BasicRunnerBasicTest extends tencetest_BasicRunnerTest # {{{
{
    private 
$class$td;
    private 
$classBody '<?php
class %s extends Tence_TestCase
{
    static $beenRun = false;
    function test()
    {
        self::$beenRun = true;
    }
}
'
;
    private function 
createTestClassFile() # {{{
    
{
        
$file $this->mkstemp();
        
$class $this->generateClassName();
        
fputs($file->fd(), sprintf($this->classBody$class));
        
fclose($file->fd());
        return array(
$file$class);
    } 
# }}}
    
function setUp() # {{{
    
{
        list(
$file$this->class) = $this->createTestClassFile();
        
$runner $this->createRunner();
        
$runner->run($this->createConfig($file->path(), $this->class));
    } 
# }}}
    
function test_runTestsIncludesFile() # {{{
    
{
        return 
$this->assertTrue(class_exists($this->class));
    } 
# }}}
    
function test_runTestsRunsTests() # {{{
    
{
        
$cvars get_class_vars($this->class);
        return 
$this->assertTrue($cvars['beenRun']);
    } 
# }}}
# }}}
class tencetest_BasicRunnerFailuresTest extends tencetest_BasicRunnerTest # {{{
{
    function 
setUp() # {{{
    
{
        
$this->tf $this->mkstemp();
        
$this->runner $this->createRunner();
    } 
# }}}
    
function testNonexistentFile() # {{{
    
{
        
$file $this->tf->path();
        unset(
$this->tf);
        
$this->willThrow('Tence_LoaderException');
        
$this->runner->run($this->createConfig($file'stdclass'));
    } 
# }}}
    
function testNonexistentClass() # {{{
    
{
        
$this->willThrow('Tence_BadTestClass');
        
$this->runner->run($this->createConfig(
            
$this->tf->path(), $this->generateClassName()
        ));
    } 
# }}}
    
function testNontestClass() # {{{
    
{
        
$this->willThrow('Tence_BadTestClass');
        
$this->runner->run($this->createConfig(
            
$this->tf->path(), 'stdclass'
        
));
    } 
# }}}
# }}}
class tencetest_BasicRunnerTests extends Tence_TestSuite # {{{
{
    function 
__construct()
    {
        
$this
            
->add(new tencetest_BasicRunnerBasicTest)
            ->
add(new tencetest_BasicRunnerFailuresTest)
        ;
    }
# }}}

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