note 88385 modified in class.directoryiterator by danbrown

From: Date: Wed, 21 Jan 2009 21:08:57 +0000
Subject: note 88385 modified in class.directoryiterator by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149619@lists.php.net to get a copy of this message
DirectoryIterator::getBasename() has been also been available since 5.2.2, according to the changelog (not documented yet). It takes a parameter $suffix, and is useful if, for instance, you use a naming convention for your files (e.g. ClassName.php). The following code uses this to add recursively All*Tests.php in any subdirectory off of tests/, basically, suites of suites. <?php // PHPUnit boilerplate code goes here class AllTests { public static function main() { $parameters = array('verbose' => true); PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('AllMyTests'); // this must be something different than the class name, per PHPUnit $it = new AllTestsFilterIterator( new RecursiveIteratorIterator( new RecursiveDirectoryIterator(dirname(__FILE__) . '/tests'))); for ($it->rewind(); $it->valid(); $it->next()) { require_once($it->current()); $className = $it->current()->getBasename('.php'); $suite->addTest($className::suite()); } return $suite; } } ?> Also, the AllTestsFilterIterator above extends FilterIterator, and contains one method, accept(): <?php class AllTestsFilterIterator extends FilterIterator { public function accept() { if (preg_match('/All.*Tests\.php/', $this->current())) { return true; } else { return false; } } } ?> --was-- DirectoryIterator::getBasename() has been also been available since 5.2.2, according to the changelog (not documented yet). It takes a parameter $suffix, and is useful if, for instance, you use a naming convention for your files (e.g. ClassName.php). The following code uses this to add recursively All*Tests.php in any subdirectory off of tests/, basically, suites of suites. // PHPUnit boilerplate code goes here class AllTests { public static function main() { $parameters = array('verbose' => true); PHPUnit_TextUI_TestRunner::run(self::suite(), $parameters); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('AllMyTests'); // this must be something different than the class name, per PHPUnit $it = new AllTestsFilterIterator( new RecursiveIteratorIterator( new RecursiveDirectoryIterator(dirname(__FILE__) . '/tests'))); for ($it->rewind(); $it->valid(); $it->next()) { require_once($it->current()); $className = $it->current()->getBasename('.php'); $suite->addTest($className::suite()); } return $suite; } } Also, the AllTestsFilterIterator above extends FilterIterator, and contains one method, accept(): class AllTestsFilterIterator extends FilterIterator { public function accept() { if (preg_match('/All.*Tests\.php/', $this->current())) { return true; } else { return false; } } } http://php.net/manual/en/class.directoryiterator.php

« previous php.notes (#149619) next »