Ejemplo n.º 1
0
 /**
  * @return bool
  */
 public function isSilentMode()
 {
     $root = $this->cfg->queryOne('/cfg:phpdox');
     if (!$root instanceof \DomNode) {
         return false;
     }
     return $root->getAttribute('silent', 'false') === 'true';
 }
Ejemplo n.º 2
0
 private function getCacheDom()
 {
     if ($this->cacheDom === NULL) {
         $this->cacheDom = new fDOMDocument();
         $cacheFile = $this->config->getLogfilePath();
         if (file_exists($cacheFile)) {
             $this->cacheDom->load($cacheFile);
             $sha1 = $this->cacheDom->documentElement->getAttribute('sha1');
             $cwd = getcwd();
             chdir($this->config->getSourceDirectory());
             exec($this->config->getGitBinary() . ' diff --name-only ' . $sha1, $files, $rc);
             foreach ($files as $file) {
                 $fields = array('path' => dirname($file), 'file' => basename($file));
                 $query = $this->cacheDom->prepareQuery('//*[@path = :path and @file = :file]', $fields);
                 $node = $this->cacheDom->queryOne($query);
                 if (!$node) {
                     continue;
                 }
                 $node->parentNode->removeChild($node);
             }
             chdir($cwd);
         } else {
             $this->cacheDom->loadXML('<?xml version="1.0" ?><gitlog xmlns="' . self::GITNS . '" />');
             $this->cacheDom->documentElement->setAttribute('sha1', $this->commitSha1);
         }
     }
     return $this->cacheDom;
 }
Ejemplo n.º 3
0
 public function import(fDOMDocument $dom) {
     $dom->registerNamespace('phpdox', 'http://xml.phpdox.net/src#');
     $dir = $dom->queryOne('/phpdox:source/phpdox:dir');
     if (!$dir)  {
         return;
     }
     $this->importDirNode($dir, '');
 }
Ejemplo n.º 4
0
 private function getMethod($name)
 {
     $ctx = $this->dom->queryOne(sprintf('phpdox:method[@name="%s"]', $name));
     if (!$ctx) {
         throw new UnitObjectException(sprintf('Method "%s" not found', $name), UnitObjectException::NoSuchMethod);
     }
     return new MethodObject($this, $ctx);
 }
Ejemplo n.º 5
0
 public function getProjectConfig($project)
 {
     $filter = is_int($project) ? $project : "@name = '{$project}'";
     $ctx = $this->cfg->queryOne("//cfg:project[{$filter}]");
     if (!$ctx) {
         throw new ConfigException("Project '{$project}' not found in configuration xml file", ConfigException::ProjectNotFound);
     }
     return new ProjectConfig($this->runResolver($ctx));
 }
Ejemplo n.º 6
0
 private function processFindings(fDOMDocument $dom, \DOMNodeList $findings)
 {
     foreach ($findings as $finding) {
         /** @var fDOMElement $finding */
         $line = $finding->getAttribute('line');
         $ref = $dom->queryOne(sprintf('//phpdox:*/*[@line = %d or (@start <= %d and @end >= %d)]', $line, $line, $line));
         if (!$ref) {
             // One src file may contain multiple classes/traits/interfaces, so the
             // finding might not apply to the current object since findings are based on filenames
             // but we have individual objects - so we just ignore the finding for this context
             continue;
         }
         $this->processFinding($dom, $ref, $finding);
     }
 }
Ejemplo n.º 7
0
 private function processViolations(fDOMDocument $dom, \DOMNodeList $violations)
 {
     foreach ($violations as $violation) {
         /** @var fDOMElement $violation */
         $line = $violation->getAttribute('beginline');
         $ref = $dom->queryOne(sprintf('//phpdox:*/*[@line = %d or (@start <= %d and @end >= %d)]', $line, $line, $line));
         if (!$ref) {
             // One src file may contain multiple classes/traits/interfaces, so the
             // finding might not apply to the current object since violations are based on filenames
             // but we have individual objects - so we just ignore the finding for this context
             continue;
         }
         $enrichment = $this->getEnrichtmentContainer($ref, 'pmd');
         $enrichViolation = $dom->createElementNS(self::XMLNS, 'violation');
         $enrichment->appendChild($enrichViolation);
         $enrichViolation->setAttribute('message', trim($violation->nodeValue));
         foreach ($violation->attributes as $attr) {
             $enrichViolation->setAttributeNode($dom->importNode($attr, true));
         }
     }
 }
Ejemplo n.º 8
0
 private function processFindings(fDOMDocument $dom, \DOMNodeList $findings)
 {
     foreach ($findings as $finding) {
         /** @var fDOMElement $finding */
         $line = $finding->getAttribute('line');
         $ref = $dom->queryOne(sprintf('//phpdox:*/*[@line = %d or (@start <= %d and @end >= %d)]', $line, $line, $line));
         if (!$ref) {
             // One src file may contain multiple classes/traits/interfaces, so the
             // finding might not apply to the current object since findings are based on filenames
             // but we have individual objects - so we just ignore the finding for this context
             continue;
         }
         $enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle');
         $enrichFinding = $dom->createElementNS(self::XMLNS, $finding->getAttribute('severity', 'error'));
         $enrichment->appendChild($enrichFinding);
         foreach ($finding->attributes as $attr) {
             if ($attr->localName == 'severity') {
                 continue;
             }
             $enrichFinding->setAttributeNode($dom->importNode($attr, true));
         }
     }
 }
Ejemplo n.º 9
0
 /**
  * @return string
  */
 public function getInterfaceClassName()
 {
     return $this->dom->queryOne('configuration/interface')->getAttribute('name');
 }
Ejemplo n.º 10
0
 /**
  * @param fDOMDocument $dom
  *
  * @return fDOMDocument
  *
  * @throws EnricherException
  * @throws PHPUnitEnricherException
  */
 private function loadCoverageInformation(fDOMDocument $dom)
 {
     $fileNode = $dom->queryOne('//phpdox:file');
     if (!$fileNode) {
         throw new PHPUnitEnricherException('No file header in event dom');
     }
     $fileInfo = new FileInfo($fileNode->getAttribute('path'));
     $srcDir = $this->getSourceDirectory();
     $paths = explode('/', (string) $fileInfo->getRelative($srcDir));
     $file = $fileNode->getAttribute('file');
     $paths = array_slice($paths, 1);
     $query = sprintf('//pu:project/pu:directory[@name = "%s"]', $srcDir->getRealPath());
     foreach ($paths as $path) {
         $query .= sprintf('/pu:directory[@name = "%s"]', $path);
     }
     $query .= sprintf('/pu:file[@name = "%s"]', $file);
     $phpunitFileNode = $this->index->queryOne($query);
     if (!$phpunitFileNode) {
         throw new PHPUnitEnricherException('No coverage information for file');
     }
     $refDom = $this->loadXML($phpunitFileNode->getAttribute('href'));
     return $refDom;
 }
Ejemplo n.º 11
0
 /**
  * @return bool
  */
 public function isSilentMode()
 {
     $root = $this->cfg->queryOne('/cfg:phpdox');
     return $root->getAttribute('silent', 'false') === 'true';
 }
Ejemplo n.º 12
0
 /**
  * @param string[] $units
  * @param string[] $visibilities
  * @return fDOMDocument
  */
 private function createPhpDoxConfig(array $units, array $visibilities)
 {
     $dom = new fDOMDocument();
     $dom->loadXML($this->factory->getConfigSkeleton()->renderStripped());
     $dom->registerNamespace('config', ConfigLoader::XMLNS);
     // set up silent
     $silent = $this->output->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL ? 'true' : 'false';
     $dom->queryOne('//config:phpdox')->setAttribute('silent', $silent);
     // set up directories
     $projectNode = $dom->queryOne('//config:project');
     $projectNode->setAttribute('source', $this->sourceDirectory);
     $projectNode->setAttribute('workdir', $this->buildDirectory);
     // inject flag to avoid unecessary processing according to the visibility filter
     if (1 === count($visibilities) && in_array('public', $visibilities)) {
         $collectorNode = $dom->queryOne('//config:collector');
         $projectNode->setAttribute('publiconly', 'true');
     }
     // remove current masks
     $query = "//config:collector/*[name()='include' or name()='exclude']";
     foreach ($dom->query($query) as $mask) {
         $mask->parentNode->removeChild($mask);
     }
     // append files to be parsed
     $collector = $dom->queryOne('//config:collector');
     foreach ($units as $unitFile) {
         $include = $dom->createElement('include');
         $include->setAttribute('mask', $unitFile);
         $collector->appendChild($include);
     }
     return $dom;
 }
Ejemplo n.º 13
0
 /**
  * @param $fname
  *
  * @return array
  */
 private function findAffectedUnits($fname)
 {
     $affected = array();
     $dom = new fDOMDocument();
     $dom->load($this->xmlDir . '/' . $fname);
     $dom->registerNamespace('phpdox', 'http://xml.phpdox.net/src#');
     $extends = $dom->queryOne('//phpdox:extends');
     if ($extends instanceof fDOMElement) {
         try {
             $affected[$extends->getAttribute('full')] = $this->getUnitByName($extends->getAttribute('full'));
         } catch (ProjectException $e) {
         }
     }
     $implements = $dom->query('//phpdox:implements');
     foreach ($implements as $implement) {
         try {
             $affected[$implement->getAttribute('full')] = $this->getUnitByName($implement->getAttribute('full'));
         } catch (ProjectException $e) {
         }
     }
     return $affected;
 }
Ejemplo n.º 14
0
        private function processUnit(fDOMDocument $unit, fDOMDocument $coverage) {
            $enrichment = $this->getEnrichtmentContainer($unit->documentElement, 'phpunit');

            $className = $unit->documentElement->getAttribute('name');
            $classNamespace = $unit->documentElement->getAttribute('namespace');

            $classNode = $coverage->queryOne(
                sprintf('//pu:class[@name = "%s" and pu:namespace[@name = "%s"]]', $className, $classNamespace)
            );
            if (!$classNode) {
                // This class seems to be newer than the last phpunit run
                return;
            }
            $coverageTarget = $enrichment->appendElementNS(self::XMLNS, 'coverage');
            foreach(array('executable','executed', 'crap') as $attr) {
                $coverageTarget->appendChild(
                    $coverageTarget->ownerDocument->importNode($classNode->getAttributeNode($attr))
                );
            }

            $result = array(
                'PASSED'  => 0,
                'SKIPPED'  => 0,
                'INCOMPLETE'  => 0,
                'FAILURE'  => 0,
                'ERROR'  => 0,
                'RISKY'  => 0
            );

            $methods = $unit->query('/phpdox:*/phpdox:constructor|/phpdox:*/phpdox:destructor|/phpdox:*/phpdox:method');
            $xp = $this->index->getDOMXPath();

            foreach($methods as $method) {
                $start = $method->getAttribute('start');
                $end = $method->getAttribute('end');

                $enrichment = $this->getEnrichtmentContainer($method, 'phpunit');
                $coverageTarget = $enrichment->appendElementNS(self::XMLNS, 'coverage');

                /** @var fDOMElement $coverageMethod */
                $coverageMethod = $coverage->queryOne(
                    sprintf('//pu:method[@start = "%d" and @end = "%d"]', $start, $end)
                );

                if ($coverageMethod != NULL) {
                    foreach(array('executable','executed','coverage', 'crap') as $attr) {
                        $coverageTarget->appendChild(
                            $coverageTarget->ownerDocument->importNode($coverageMethod->getAttributeNode($attr))
                        );
                    }
                }

                $coveredNodes = $coverage->query(
                    sprintf('//pu:coverage/pu:line[@nr >= "%d" and @nr <= "%d"]/pu:covered', $start, $end)
                );

                $seen = array();
                foreach($coveredNodes as $coveredNode) {
                    $by = $coveredNode->getAttribute('by');
                    if (isset($seen[$by])) {
                        continue;
                    }
                    $seen[$by] = true;

                    $name = $xp->prepare(':name', array('name' => $by));
                    $test = $coverageTarget->appendChild(
                        $unit->importNode(
                            $this->index->queryOne(
                                sprintf('//pu:tests/pu:test[@name = %s]', $name)
                            )
                        )
                    );

                    $result[$test->getAttribute('status')]++;

                }

            }

            if (!isset($this->results[$classNamespace])) {
                $this->results[$classNamespace] = array();
                $this->coverage[$classNamespace] = array();
            }
            $this->results[$classNamespace][$className] = $result;
            $this->coverage[$classNamespace][$className] = $coverageTarget->cloneNode(false);
        }
Ejemplo n.º 15
0
 /**
  * @covers \TheSeer\fDOM\fDOMDocument::queryOne
  */
 public function testThatTwoNodesAreIdentifiedAsBeingInTheSameDocument()
 {
     $this->dom->loadXML('<?xml version="1.0" ?><root><node /></root>');
     $node = $this->dom->queryOne('//node');
     $this->assertTrue($this->dom->inSameDocument($node));
 }