php - PHPUnit autoloading classes -
question in short
how can make autoloader find classes required run php tests?
question in detail
i want autoload classes using in phpunit in eclipse. directory structure follows.
project (called yii-app) protected dira classa.php dirb classb.php yii-1.1.14.f0fee9 yii.php tests classatest.php classbtest.php bootstrap.php autoloader.php i use bootstrap.php , autoloader.php found here, see below details. class classa not make use of yii framework, , tests in classatest run smoothly. class classb make use of yii framework. 1 of first lines is:
yii::import('application.<path directory>.*') when try run tests in classbtest.php, following error.
fatal error: class 'yii' not found in /users/physicalattraction/git/yii-app/protected/dirb/classb.php on line 3 even if register entire project directory (including subdirectories), class yii not found, while right there. should change make these tests run well?
note
i have same problem if try run tests directly terminal, not eclipse related.
$ ./composer/vendor/bin/phpunit --bootstrap=tests/bootstrap.php tests phpunit 4.5.1 sebastian bergmann , contributors. fatal error: class 'yii' not found in /users/physicalattraction/git/yii-app/protected/dirb/classb.php on line 3 details
phpunit settings in eclipse

bootstrap.php
<?php include_once('autoloader.php'); // register directory include files toolbox\testing\autoloader::registerdirectory(__dir__.'/../yii-1.1.14.f0fee9'); toolbox\testing\autoloader::registerdirectory(__dir__.'/../protected'); ?> autoloader.php
<?php namespace toolbox\testing; /** * class auto loader use vanilla php projects' testing environment. use in * bootstrap register classes without having use framework (which can, , should if * it's better solution you) , without having use includes everywhere. * * assumes file path in relation namespace follows psr-0 standard. * * important note: when registering directories, class has no ability discern * conflicting class names in different namespaces, means classes same name * override each other! use registernamespace()-method if possible! * * inspired jess telford's autoloader (http://jes.st/). * * @see http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/ * @see http://petermoulding.com/php/psr * @see http://www.php-fig.org/psr/psr-0/ * * @codecoverageignore * * @category toolbox * @package testing * * @author helge söderström <helge.soderstrom@schibsted.se> */ class autoloader { /** * array keeping class names key , path value classes registered * autoloader::registernamespace(). * * @var array */ protected static $namespaceclassnames = array(); /** * array keeping class names key , path value classes registered * autoloader::registerdirectory(). * * @var array */ protected static $directoryclassnames = array(); /** * store filename (sans extension) & full path ".php" files found namespace. * parameter should contain root namespace key , directory value. * * @param string $namespace * @param string $dirname * @return void */ public static function registernamespace($namespace, $dirname) { $directorycontents = new \directoryiterator($dirname); foreach($directorycontents $file) { if ($file->isdir() && !$file->islink() && !$file->isdot()) { $newnamespace = $namespace . "_" . $file->getfilename(); $newdirname = $dirname . "/" . $file->getfilename(); static::registernamespace($newnamespace, $newdirname); } elseif (substr($file->getfilename(), -4) === '.php') { $classname = substr($file->getfilename(), 0, -4); $namespacedclassname = $namespace . "_" . $classname; $filename = realpath($dirname) . "/" . $file->getfilename(); static::$namespaceclassnames[$namespacedclassname] = $filename; } } } /** * store filename (sans extension) & full path of ".php" files found. * * note: method not able differentiate same class names in different * namespaces , therefore overwrite class names if multiple of same name * found. if possible, use registernamespace instead! * * @param string $dirname * @return void */ public static function registerdirectory($dirname) { $directorycontents = new \directoryiterator($dirname); foreach ($directorycontents $file) { if ($file->isdir() && !$file->islink() && !$file->isdot()) { // recurse directories other few special ones. static::registerdirectory($file->getpathname()); } elseif (substr($file->getfilename(), -4) === '.php') { // save class name / path of .php file found. $classname = substr($file->getfilename(), 0, -4); autoloader::registerclass($classname, $file->getpathname()); } } } /** * caches found class class name key , path value use when loading * on fly. class registered class name only, no namespace. * * @param string $classname * @param string $filename * @return void */ public static function registerclass($classname, $filename) { autoloader::$directoryclassnames[$classname] = $filename; } /** * includes found class in runtime environment. strips namespaces. * * @param string $classname * @return void */ public static function loadclass($classname) { // first, see if we've registered entire namespace. $namespacedclassname = str_replace('\\', '_', $classname); if (isset(static::$namespaceclassnames[$namespacedclassname])) { require_once(static::$namespaceclassnames[$namespacedclassname]); return; } // nope. have registered directory? $psrdirectoryseparators = array('\\', '_'); foreach($psrdirectoryseparators $separator) { $separatoroccurrence = strrpos($classname, $separator); if($separatoroccurrence !== false) { $classname = substr($classname, $separatoroccurrence + 1); break; } } if (isset(autoloader::$directoryclassnames[$classname])) { require_once(autoloader::$directoryclassnames[$classname]); } } } // register our autoload class system auto loader. spl_autoload_register(array('toolbox\testing\autoloader', 'loadclass')); ?>
the autoloader doesn't find yii class. did try adding:
toolbox\testing\autoloader::registerdirectory(dir.'/../yii-1.1.14.f0fee9/framework');
to bootstrap.php file. think yii class defined in framework directory.
another thing can try use composer autoloader instead.
p.s practice mirror app directory/file structure in tests directory. in case classatest.php , classbtest.php should separated own directories same way separated in protected directory.
Comments
Post a Comment