Beispiel #1
0
/**
 * Autoloader for Photon.
 */
function photonAutoLoad($class)
{
    $parts = array_filter(explode('\\', $class));
    if (1 < count($parts)) {
        // We have a namespace.
        $class_name = array_pop($parts);
        $file = implode(DIRECTORY_SEPARATOR, $parts) . '.php';
    } else {
        $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
    }
    // As we load only once to have everything is the process, the
    // require_once instead of require penalty is low. But the
    // require_once will prevent double loading a file and will result
    // in non confusing error messages.
    // printf("Class: %s, file: %s\n", $class, $file);
    $includePath = \photon\path\Dir::getIncludePath();
    foreach ($includePath as $path) {
        $fullpath = $path . DIRECTORY_SEPARATOR . $file;
        if (is_readable($fullpath)) {
            require_once $fullpath;
            break;
        }
    }
}
Beispiel #2
0
    /** Different bootstrap file to use */
    public function run()
    {
        $this->verbose('Run the project tests...');
        $config_path = $this->loadConfig('config.test.php');
        $apps = Conf::f('tested_components', array());
        // Now, we have a collection of apps, but each app is not
        // necessarily in the 'apps' subfolder of the project, some
        // can be available on the include_path. So, we try to find
        // for each app, the corresponding tests folder.
        $test_dirs = array();
        $test_files = array();
        $inc_dirs = Dir::getIncludePath();
        foreach ($apps as $app) {
            foreach ($inc_dirs as $dir) {
                if (file_exists($dir . '/' . $app . '/tests')) {
                    $test_dirs[] = realpath($dir . '/' . $app . '/tests');
                }
                if (file_exists($dir . '/' . $app . '/tests.php')) {
                    $test_files[] = realpath($dir . '/' . $app . '/tests.php');
                }
            }
        }
        if (empty($test_dirs) && empty($test_files)) {
            $this->info('Nothing to test.');
            return 2;
        }
        // Now we generate the XML config file for PHPUnit
        $tmpl = '<phpunit><testsuites><testsuite name="Photon Tests">' . "\n%s\n%s\n" . '</testsuite></testsuites>
<filter><blacklist><directory suffix=".php">%s</directory></blacklist></filter>
<php>
  <env name="photon.config" value="%s"/>
</php>
</phpunit>';
        $test_files = array_map(function ($file) {
            return '<file>' . $file . '</file>';
        }, $test_files);
        $test_dirs = array_map(function ($dir) {
            return '<directory>' . $dir . '</directory>';
        }, $test_dirs);
        $xml = sprintf($tmpl, implode("\n", $test_files), implode("\n", $test_dirs), $this->photon_path, $config_path);
        $tmpfname = tempnam(Conf::f('tmp_folder', sys_get_temp_dir()), 'phpunit');
        file_put_contents($tmpfname, $xml, LOCK_EX);
        $this->verbose('PHPUnit configuration file:');
        $this->verbose($xml);
        if (isset($this->params['directory'])) {
            if (!file_exists($this->params['directory'])) {
                mkdir($this->params['directory']);
            }
            passthru('phpunit --bootstrap ' . realpath(__DIR__) . '/autoload.php --coverage-html ' . realpath($this->params['directory']) . ' --configuration ' . $tmpfname, $rvar);
            unlink($tmpfname);
            $this->info(sprintf('Code coverage report: %s/index.html.', realpath($this->params['directory'])));
        } else {
            $xmlout = tempnam(Conf::f('tmp_folder', sys_get_temp_dir()), 'phpunit') . '.xml';
            $cmd = 'phpunit --verbose --bootstrap ' . realpath(__DIR__) . '/testbootstrap.php --coverage-clover ' . $xmlout . ' --configuration ' . $tmpfname;
            $this->verbose($cmd);
            passthru($cmd, $rvar);
            unlink($tmpfname);
            if (!file_exists($xmlout)) {
                return $rvar;
            }
            $xml = simplexml_load_string(file_get_contents($xmlout));
            unlink($xmlout);
            if (!isset($xml->project->metrics['coveredstatements'])) {
                return $rvar;
            }
            $perc = 0 == $xml->project->metrics['statements'] ? 1.0 : $xml->project->metrics['coveredstatements'] / (double) $xml->project->metrics['statements'];
            $this->info(sprintf('Code coverage %s/%s (%s%%)', $xml->project->metrics['coveredstatements'], $xml->project->metrics['statements'], round($perc * 100.0, 2)));
        }
        return $rvar;
    }
Beispiel #3
0
 /**
  * Load the locales of a lang.
  *
  * It does not activate the locale.
  *
  * @param $lang Language to load
  * @param $photon Load the Photon translations (true)
  * @return array Path to loaded file
  */
 public static function loadLocale($lang, $photon = true)
 {
     $locale_folders = Conf::f('locale_folders', array());
     $path_folders = Dir::getIncludePath();
     $loaded = array();
     self::$loaded[$lang] = array();
     if ($photon) {
         $pofile = sprintf('%s/locale/%s/photon.po', __DIR__, $lang);
         if (file_exists($pofile)) {
             self::$loaded[$lang] += self::readPoFile($pofile);
             $loaded[] = $pofile;
         }
     }
     foreach ($locale_folders as $locale_folder) {
         foreach ($path_folders as $path_folder) {
             $pofile = sprintf('%s/%s/%s.po', $path_folder, $locale_folder, $lang);
             if (file_exists($pofile)) {
                 self::$loaded[$lang] += self::readPoFile($pofile);
                 $loaded[] = $pofile;
                 break;
             }
         }
     }
     if (count($loaded)) {
         self::$plural_forms[$lang] = plural_to_php(file_get_contents($loaded[0]));
     }
     return $loaded;
 }