List Info

Thread: Re: Passing initialization information to tests




Re: Passing initialization information to tests
user name
2007-05-31 10:24:40
I have experienced a similar problem and wrote a solution.

I used a server introspection object as well but I have
removed it
because it's not significant for your needs. You can trust
that these
work on windows and linux because I've unit tested them and
I use them
to run my tests (unless I've just introduced a bug in my
minor
alterations).

Example usage:
<?php
$files = new Ole_Test_Loader();
define('DS', DIRECTORY_SEPARATOR);
$files->setIncludeDirs(array(
   '..' . DS . 'somePath' . DS . 'Foo',
   '..' . DS . 'somePath' . DS . 'Bar',
));
$files->setExcludeDirs(array(
   '..' . DS . 'somePath' . DS . 'Foo' . DS . 'Zim',
));

$unitTests = new Ole_Test_All();
$unitTests->setTestFiles($files->getTestFiles());
$unitTests->run();
?>

This will run files ending in 'Test.php' in those
directories. If you
wish to run all PHP files you can change the SUFFIX constant
in
Ole_Test_Loader to '.php'. Personally I like to put my test
cases the
same folder as the code they are testing. So I'll have a
class called
Foo in file 'Foo.php' and it's test case in 'FooTest.php' in
the same
dir directly beneath it.


<?php
class Ole_Test_All
{
   /**
    * var array
    */
   private $_testFiles;
   /**
    * return array
    */
   public function getTestFiles()
   {
       return $this->_testFiles;
   }
   /**
    * param array $testFiles
    * return $this
    */
   public function setTestFiles($testFiles)
   {
       $this->_testFiles = $testFiles;
       return $this;
   }
   /**
    * var GroupTest
    */
   private $_groupTest;
   /**
    * return GroupTest
    */
   public function getGroupTest()
   {
       if (!$this->_groupTest) {
           $this->_groupTest = new GroupTest(__CLASS__);
       }
       return $this->_groupTest;
   }
   /**
    * No interface for SimpleTest can't type hint this :(
    *
    * param GroupTest $groupTest
    * return $this
    */
   public function setGroupTest($groupTest)
   {
       $this->_groupTest = $groupTest;
       return $this;
   }
   /**
    * Run a SimpleTest test suite using the group test files
added with
    * setTestFiles() and the optional GroupTest object
assigned with
    * setGroupTest().
    *
    * return mixed group test status
    */
   public function run()
   {
       $groupTest = $this->getGroupTest();
       error_reporting(E_ALL & ~E_STRICT); // for
SimpleTest ands its
PHP4 obsession
       array_map(array($groupTest, 'addTestFile'),
$this->getTestFiles());

       if ($this->isCliSapi()) {
           echo
"nn==================================================
==========n";
       }
       return $groupTest->run($this->_getReporter());
   }
   /**
    * Get the correct SimpleTest reporter according to the
current server
    * environment
    *
    * return SimpleReporter
    */
   protected function _getReporter()
   {
       if ($this->isCliSapi()) {
           return new TextReporter();
       }
       return new HtmlReporter();
   }
   private function isCliSapi()
   {
       return PHP_SAPI == 'cli';
   }
}
class Ole_Test_Loader
{
   const SUFFIX = 'Test.php';
   /**
    * var array
    */
   private $_includeDirs = array();
   /**
    * var array
    */
   private $_excludeDirs = array();
   /**
    * var array
    */
   private $_singleFiles = array();
   /**
    * var Ole_Shell_Interface
    */
   private $_shell;
   /**
    * return Ole_Shell_Interface
    */
   public function getShell()
   {
       if (!$this->_shell) {
           $this->_shell = new Ole_Shell();
       }
       return $this->_shell;
   }
   /**
    * param Ole_Shell_Interface $shell
    * return $this
    */
   public function setShell(Ole_Shell_Interface $shell)
   {
       $this->_shell = $shell;
       return $this;
   }
   /**
    * Get the test files within the include directory and
not in the
exclude directories
    * plus any additional single files.
    *
    * return array
    */
   public function getTestFiles()
   {
       $files = array();
       foreach ($this->_includeDirs as $includeDir) {
           foreach ($this->getFilesFromDir($includeDir)
as $file) {
                   $files[$file] = true;
           }
       }
       foreach ($this->_excludeDirs as $excludeDir) {
           foreach ($this->getFilesFromDir($excludeDir)
as $file) {
               unset($files[$file]);
           }
       }
       foreach ($this->_singleFiles as $file) {
               $files[$file] = true;
       }
       return array_keys($files);
   }
   /**
    * Get all the test files from a specific directory
    *
    * param string $dir
    * return array
    */
   public function getFilesFromDir($dir)
   {
       $command = sprintf($this->_getCommand(), $dir); //
tmp var
used for readability
       return array_filter(explode("n",
$this->getShell()->run($command)));
   }
   protected function _getCommand()
   {
       if (preg_match('~^WIN~i', PHP_OS)) {
           return 'dir %s /s /b /a:-d | find "' .
self::SUFFIX . '"';
       } else {
           return 'find %s -name *' . self::SUFFIX . '
-print';
       }
   }
   public function setIncludeDirs(array $dirs)
   {
       $this->_includeDirs = $dirs;
   }
   public function setExcludeDirs(array $dirs)
   {
       $this->_excludeDirs = $dirs;
   }
   public function addSingleFile($value)
   {
       $this->_singleFiles[] = $value;
   }
}
interface Ole_Shell_Interface
{
   function run($command);
}
/**
 * Worlds shortest class!
 * For mocking.
 */
class Ole_Shell implements Ole_Shell_Interface
{
   public function run($command)
   {
       return `$command`;
   }
}
?>

Hope this helps.

------------------------------------------------------------
-------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and
take
control of your XML. No limits. Just data. Click to get it
now.
http://sourcefor
ge.net/powerbar/db2/
_______________________________________________
Simpletest-support mailing list
Simpletest-supportlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/simp
letest-support

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )