Пример #1
0
 protected function checkProject($files, $expected_defects)
 {
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider = new XRef_FileProvider_InMemory($files);
     $report = $lint_engine->getReport($file_provider);
     // 1. check that there were no fatal (parse) errors
     // if any file can't be parsed, the rest of the report is invalid
     foreach ($report as $file_name => $list) {
         foreach ($list as $e) {
             if ($e->severity == XRef::FATAL) {
                 throw new XRef_DummyException("Can't parse file {$file_name}: {$e->message}");
             }
         }
     }
     // 2. count errors
     $count_found = 0;
     foreach ($report as $file_name => $list) {
         $count_found += count($list);
     }
     $count_expected = count($expected_defects);
     if ($count_found != $count_expected) {
         print_r($report);
         $this->fail("Wrong number of errors: found={$count_found}, expected={$count_expected}");
     } else {
         $this->assertTrue($count_found == $count_expected, "Expected number of defects");
     }
     // 3. check every entry individually
     $i = 0;
     foreach ($report as $file_name => $list) {
         foreach ($list as $e) {
             list($expected_file_name, $token_text, $line_number, $severity) = $expected_defects[$i];
             $this->checkFoundProjectDefect($e, $file_name, $expected_file_name, $token_text, $severity, $line_number);
             ++$i;
         }
     }
 }
Пример #2
0
 public function testProjectCheckEngine()
 {
     // same tests as for Simple engine - they should report the same errors
     // usage with parsed file
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $pf = $this->xref->getParsedFile("fileA.php", '<?php echo $foo;');
     $lint_engine->addParsedFile($pf);
     $report = $lint_engine->collectReport();
     $this->assertTrue(count($report) == 1);
     $this->assertTrue(isset($report["fileA.php"]));
     $this->assertTrue(count($report["fileA.php"]) == 1);
     $this->assertTrue($report["fileA.php"][0]->tokenText == '$foo');
     // usage with file provider
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php echo $bar;'));
     $report = $lint_engine->getReport($file_provider);
     $this->assertTrue(count($report) == 1);
     $this->assertTrue(isset($report["fileA.php"]));
     $this->assertTrue(count($report["fileA.php"]) == 1);
     $this->assertTrue($report["fileA.php"][0]->tokenText == '$bar');
     // only the php files are checked
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php echo $baz;', 'fileB.txt' => 'some text with $foo', 'fileC.txt' => '<?php looks like php'));
     $report = $lint_engine->getReport($file_provider);
     $this->assertTrue(count($report) == 1);
     $this->assertTrue(isset($report["fileA.php"]));
     $this->assertTrue(count($report["fileA.php"]) == 1);
     $this->assertTrue($report["fileA.php"][0]->tokenText == '$baz');
     // but project integrity is checked now!
     // no project integrity is checked
     $lint_engine = new XRef_LintEngine_ProjectCheck($this->xref, false);
     $file_provider = new XRef_FileProvider_InMemory(array('fileA.php' => '<?php class A { const FOO = 1; }', 'fileB.php' => '<?php echo A::BAR; '));
     $report = $lint_engine->getReport($file_provider);
     $this->assertTrue(count($report) == 1);
     $this->assertTrue(isset($report["fileB.php"]));
     $this->assertTrue(count($report["fileB.php"]) == 1);
     $this->assertTrue($report["fileB.php"][0]->tokenText == 'BAR');
 }
Пример #3
0
 public function init()
 {
     $cwd = getcwd();
     // create data dir
     if (!file_exists(".xref")) {
         echo "Creating dir .xref\n";
         if (!mkdir(".xref")) {
             throw new Exception("Can't create dir .xref");
         }
     }
     $config_filename = ".xref/xref.ini";
     if (file_exists($config_filename)) {
         echo "Config file {$config_filename} exists; won't overwrite\n";
     } else {
         echo "Creating config file {$config_filename}\n";
         // create config file
         $fh = fopen($config_filename, "w");
         if (!$fh) {
             throw new Exception("Can't create file {$config_filename}");
         }
         $config = array();
         $config[] = "[project]";
         $config[] = array("name", basename($cwd));
         $config[] = array("source-code-dir[]", realpath($cwd));
         $config[] = array(";source-url", 'https://github.com/<author>/<project>/blob/{%revision}/{%fileName}#L{%lineNumber}');
         $config[] = null;
         $config[] = "[xref]";
         $config[] = array("data-dir", realpath("{$cwd}/.xref"));
         $config[] = array("project-check", "true");
         $config[] = array(";smarty-class", "/path/to/Smarty.class.php");
         $config[] = array(";script-url", "http://xref.your.domain.com/bin");
         $config[] = null;
         $config[] = "[lint]";
         $config[] = array("ignore-missing-class[]", 'PHPUnit_Framework_TestCase');
         $config[] = array(";ignore-error[]", 'xr052');
         $config[] = null;
         $config[] = "[doc]";
         $config[] = array(";output-dir", "/path/for/generated/doc");
         $config[] = null;
         if (file_exists(".git")) {
             // TODO: need a better way to check that this is a git project
             // http://stackoverflow.com/questions/957928
             // git rev-parse --show-toplevel
             $config[] = "[git]";
             $config[] = array("repository-dir", realpath($cwd));
             $config[] = null;
             $config[] = "[ci]";
             $config[] = array("incremental", "true");
             $config[] = array("update-repository", "true");
             $config[] = null;
             $config[] = "[mail]";
             $config[] = array("from", "XRef Continuous Integration");
             $config[] = array(";reply-to", "*****@*****.**");
             $config[] = array(";to[]", "{%ae}");
             $config[] = array(";to[]", "*****@*****.**");
             $config[] = null;
         }
         foreach ($config as $line) {
             $l = null;
             if (!$line) {
                 $l = "\n";
             } elseif (!is_array($line)) {
                 $l = "{$line}\n";
             } else {
                 list($k, $v) = $line;
                 if (preg_match('#^\\w+$#', $v)) {
                     $l = sprintf("%-24s= %s\n", $k, $v);
                 } else {
                     $v = preg_replace('#\\\\#', '\\\\', $v);
                     $l = sprintf("%-24s= \"%s\"\n", $k, $v);
                 }
             }
             fwrite($fh, $l);
         }
         fclose($fh);
         // re-read config values from the file
         self::getConfig(true);
     }
     // index files for the first time
     $this->loadPluginGroup('lint');
     $file_provider = new XRef_FileProvider_FileSystem($cwd);
     $lint_engine = new XRef_LintEngine_ProjectCheck($this);
     $lint_engine->setShowProgressBar(true);
     $lint_engine->setRewriteCache(true);
     // re-index files and refill cache
     echo "Indexing files\n";
     $lint_engine->getReport($file_provider);
     echo "\nDone.\n";
     // done
 }