Skip to content

Extend xml coverage #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/Report/Xml/BuildInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/*
* This file is part of the php-code-coverage package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SebastianBergmann\CodeCoverage\Report\Xml;

use SebastianBergmann\Environment\Runtime;

class BuildInformation
{
/**
* @var \DOMElement
*/
private $contextNode;

/**
* @param \DOMElement $contextNode
*/
public function __construct(\DOMElement $contextNode)
{
$this->contextNode = $contextNode;
}

/**
* @param Runtime $runtime
*/
public function setRuntimeInformation(Runtime $runtime)
{
$runtimeNode = $this->getNodeByName('runtime');

$runtimeNode->setAttribute('name', $runtime->getName());
$runtimeNode->setAttribute('version', $runtime->getVersion());
$runtimeNode->setAttribute('url', $runtime->getVendorUrl());

$driverNode = $this->getNodeByName('driver');
if ($runtime->isHHVM()) {
$driverNode->setAttribute('name', 'hhvm');
$driverNode->setAttribute('version', constant('HHVM_VERSION'));
return;
}

if ($runtime->hasPHPDBGCodeCoverage()) {
$driverNode->setAttribute('name', 'phpdbg');
$driverNode->setAttribute('version', constant('PHPDBG_VERSION'));
}

if ($runtime->hasXdebug()) {
$driverNode->setAttribute('name', 'xdebug');
$driverNode->setAttribute('version', phpversion('xdebug'));
}
}

/**
* @param $name
*
* @return \DOMElement
*/
private function getNodeByName($name)
{
$node = $this->contextNode->getElementsByTagNameNS(
'http://schema.phpunit.de/coverage/1.0',
$name
)->item(0);

if (!$node) {
$node = $this->contextNode->appendChild(
$this->contextNode->ownerDocument->createElementNS(
'http://schema.phpunit.de/coverage/1.0',
$name
)
);
}

return $node;
}

/**
* @param \DateTime $date
*/
public function setBuildTime(\DateTime $date)
{
$this->contextNode->setAttribute('time', $date->format('D M j G:i:s T Y'));
}

/**
* @param string $phpUnitVersion
* @param string $coverageVersion
*/
public function setGeneratorVersions($phpUnitVersion, $coverageVersion)
{
$this->contextNode->setAttribute('phpunit', $phpUnitVersion);
$this->contextNode->setAttribute('coverage', $coverageVersion);
}
}
2 changes: 1 addition & 1 deletion src/Report/Xml/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(\DOMElement $context, $line)
{
$this->contextNode = $context;

$this->writer = new \XMLWriter;
$this->writer = new \XMLWriter();
$this->writer->openMemory();
$this->writer->startElementNs(null, $context->nodeName, 'http://schema.phpunit.de/coverage/1.0');
$this->writer->writeAttribute('nr', $line);
Expand Down
61 changes: 47 additions & 14 deletions src/Report/Xml/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
use SebastianBergmann\CodeCoverage\Node\File as FileNode;
use SebastianBergmann\CodeCoverage\RuntimeException;
use SebastianBergmann\CodeCoverage\Version;
use SebastianBergmann\Environment\Runtime;

class Facade
{
Expand All @@ -28,6 +30,19 @@ class Facade
*/
private $project;

/**
* @var string
*/
private $phpUnitVersion;

/**
* @param string $version
*/
public function __construct($version)
{
$this->phpUnitVersion = $version;
}

/**
* @param CodeCoverage $coverage
* @param string $target
Expand All @@ -49,19 +64,25 @@ public function process(CodeCoverage $coverage, $target)
$coverage->getReport()->getName()
);

$this->setBuildInformation();
$this->processTests($coverage->getTests());
$this->processDirectory($report, $this->project);

$index = $this->project->asDom();
$index->formatOutput = true;
$index->preserveWhiteSpace = false;
$index->save($target . '/index.xml');
$this->saveDocument($this->project->asDom(), 'index');
}

private function setBuildInformation()
{
$buildNode = $this->project->getBuildInformation();
$buildNode->setRuntimeInformation(new Runtime());
$buildNode->setBuildTime(\DateTime::createFromFormat('U', $_SERVER['REQUEST_TIME']));
$buildNode->setGeneratorVersions($this->phpUnitVersion, Version::id());
}

/**
* @param string $directory
*/
private function initTargetDirectory($directory)
protected function initTargetDirectory($directory)
{
if (file_exists($directory)) {
if (!is_dir($directory)) {
Expand Down Expand Up @@ -101,7 +122,7 @@ private function processFile(FileNode $file, Directory $context)
{
$fileObject = $context->addFile(
$file->getName(),
$file->getId() . '.xml'
$file->getId().'.xml'
);

$this->setTotals($file, $fileObject->getTotals());
Expand Down Expand Up @@ -132,14 +153,7 @@ private function processFile(FileNode $file, Directory $context)
$coverage->finalize();
}

$this->initTargetDirectory(
$this->target . dirname($file->getId()) . '/'
);

$fileDom = $fileReport->asDom();
$fileDom->formatOutput = true;
$fileDom->preserveWhiteSpace = false;
$fileDom->save($this->target . $file->getId() . '.xml');
$this->saveDocument($fileReport->asDom(), $file->getId());
}

private function processUnit($unit, Report $report)
Expand Down Expand Up @@ -235,4 +249,23 @@ private function setTotals(AbstractNode $node, Totals $totals)
$node->getNumTestedFunctions()
);
}

/**
* @return string
*/
protected function getTargetDirectory()
{
return $this->target;
}

protected function saveDocument(\DOMDocument $document, $name)
{
$filename = sprintf('%s/%s.xml', $this->getTargetDirectory(), $name);

$document->formatOutput = true;
$document->preserveWhiteSpace = false;
$this->initTargetDirectory(dirname($filename));

$document->save($filename);
}
}
22 changes: 19 additions & 3 deletions src/Report/Xml/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,35 @@ class File
/**
* @var \DOMDocument
*/
protected $dom;
private $dom;

/**
* @var \DOMElement
*/
protected $contextNode;
private $contextNode;

public function __construct(\DOMElement $context)
{
$this->dom = $context->ownerDocument;
$this->dom = $context->ownerDocument;
$this->contextNode = $context;
}

/**
* @return \DOMElement
*/
protected function getContextNode()
{
return $this->contextNode;
}

/**
* @return \DOMDocument
*/
protected function getDomDocument()
{
return $this->dom;
}

public function getTotals()
{
$totalsContainer = $this->contextNode->firstChild;
Expand Down
2 changes: 1 addition & 1 deletion src/Report/Xml/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(\DOMElement $context)

protected function setContextNode(\DOMElement $context)
{
$this->dom = $context->ownerDocument;
$this->dom = $context->ownerDocument;
$this->contextNode = $context;
}

Expand Down
29 changes: 27 additions & 2 deletions src/Report/Xml/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class Project extends Node
{
/**
* @param string $name
*/
public function __construct($name)
{
$this->init();
Expand All @@ -20,8 +23,8 @@ public function __construct($name)

private function init()
{
$dom = new \DOMDocument;
$dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="http://schema.phpunit.de/coverage/1.0"><project/></phpunit>');
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="http://schema.phpunit.de/coverage/1.0"><build/><project/></phpunit>');

$this->setContextNode(
$dom->getElementsByTagNameNS(
Expand All @@ -36,6 +39,28 @@ private function setProjectName($name)
$this->getContextNode()->setAttribute('name', $name);
}

/**
* @return BuildInformation
*/
public function getBuildInformation()
{
$buildNode = $this->getDom()->getElementsByTagNameNS(
'http://schema.phpunit.de/coverage/1.0',
'build'
)->item(0);

if (!$buildNode) {
$buildNode = $this->getDom()->documentElement->appendChild(
$this->getDom()->createElementNS(
'http://schema.phpunit.de/coverage/1.0',
'build'
)
);
}

return new BuildInformation($buildNode);
}

public function getTests()
{
$testsNode = $this->getContextNode()->getElementsByTagNameNS(
Expand Down
19 changes: 10 additions & 9 deletions src/Report/Xml/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,32 @@ class Report extends File
{
public function __construct($name)
{
$this->dom = new \DOMDocument;
$this->dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="http://schema.phpunit.de/coverage/1.0"><file /></phpunit>');
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><phpunit xmlns="http://schema.phpunit.de/coverage/1.0"><file /></phpunit>');

$this->contextNode = $this->dom->getElementsByTagNameNS(
$contextNode = $dom->getElementsByTagNameNS(
'http://schema.phpunit.de/coverage/1.0',
'file'
)->item(0);

parent::__construct($contextNode);
$this->setName($name);
}

private function setName($name)
{
$this->contextNode->setAttribute('name', $name);
$this->getContextNode()->setAttribute('name', $name);
}

public function asDom()
{
return $this->dom;
return $this->getDomDocument();
}

public function getFunctionObject($name)
{
$node = $this->contextNode->appendChild(
$this->dom->createElementNS(
$node = $this->getContextNode()->appendChild(
$this->getDomDocument()->createElementNS(
'http://schema.phpunit.de/coverage/1.0',
'function'
)
Expand All @@ -59,8 +60,8 @@ public function getTraitObject($name)

private function getUnitObject($tagName, $name)
{
$node = $this->contextNode->appendChild(
$this->dom->createElementNS(
$node = $this->getContextNode()->appendChild(
$this->getDomDocument()->createElementNS(
'http://schema.phpunit.de/coverage/1.0',
$tagName
)
Expand Down
Loading