Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit b2c4383472c7f19da29241ad16ebf5baefd75a50 @agallou committed Apr 3, 2010
Showing with 349 additions and 0 deletions.
  1. +7 −0 LICENSE
  2. +39 −0 README
  3. +5 −0 config/agEmmaCoverageReportPlugin.php
  4. +235 −0 lib/task/ecrCoverageTask.class.php
  5. +63 −0 package.xml
@@ -0,0 +1,7 @@
+Copyright (c) Adrien Gallou
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 README
@@ -0,0 +1,39 @@
+agEmmaCoverageReport plugin
+==============
+
+The `agEmmaCoverageReportPlugin` generate unit test coverage reports in emma xml format.
+
+
+Prerequisite
+------------
+ * xDebug module installed
+ * XHProf PHP extension installed : [very good XHProf presentation and installation guide](http://techportal.ibuildings.com/2009/12/01/profiling-with-xhprof/)
+
+Installation
+------------
+
+ * Install the plugin
+
+ $ symfony plugin:install elXHProfPlugin
+
+ * Deploy web dir by copying elXHProfPlugin/web content into sf_web_dir/elXHProfPlugin or executing the following command
+
+ $ symfony plugin:publish-assets
+
+How to profile
+--------------
+ $run = new XHProfRun('namespace');
+ // Code to profile
+ $run->end()
+
+The namespace allows you to group multiple runs.
+
+How to view/compare the results
+--------------
+The links to the runs results are available in the web debut toolbar. They are grouped by namespace and a distinguished by whether the runs have been done in the current PHP process or in previous processes.
+
+You can compare 2 runs by checking them and clicking on the `Compare` button.
+
+TODO
+----
+ *
@@ -0,0 +1,5 @@
+<?php
+
+class agEmmaCoverageReportPluginConfiguration extends sfPluginConfiguration
+{
+}
@@ -0,0 +1,235 @@
+<?php
+class srCoverageReportTask extends sfBaseTask
+{
+ /**
+ * (non-PHPdoc)
+ * @see lib/vendor/symfony/task/sfTask#configure()
+ */
+ protected function configure()
+ {
+ $this->namespace = 'emma-coverage';
+ $this->name = 'report';
+ $this->briefDescription = 'Crée un rapport du coverage des tests unitaires pour emma';
+ $this->detailedDescription = 'Crée un rapport du coverage des tests unitaires pour emma';
+
+ $this->addOption('xml', null, sfCommandOption::PARAMETER_REQUIRED, 'Chemin ou sauver le fichier xml');
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see lib/vendor/symfony/task/sfTask#execute()
+ */
+ protected function execute($arguments = array(), $options = array())
+ {
+ $coverage = $this->getCoveragePercentByFile();
+ file_put_contents($options['xml'], $this->toXmlEmma($coverage));
+ }
+
+ protected function toXmlEmma(array $coverageByFile)
+ {
+
+ $dom = new DOMDocument('1.0', 'UTF-8');
+ $dom->formatOutput = true;
+ $dom->appendChild($dom->createComment(' EMMA report, generated ' . date('r') . ' '));
+ $dom->appendChild($report = $dom->createElement('report'));
+
+ $stats = $dom->createElement('stats');
+
+ $packages = $dom->createElement('packages');
+ $classes = $dom->createElement('classes');
+ $methods = $dom->createElement('methods');
+ $srclines = $dom->createElement('srclines');
+ $srcfiles = $dom->createElement('srcfiles');
+
+ $packages->setAttribute('value', 2);
+ $classes->setAttribute('value', 2);
+ $methods->setAttribute('value', 2);
+
+ $stats->appendChild($packages);
+ $stats->appendChild($classes);
+ $stats->appendChild($methods);
+ $stats->appendChild($srcfiles);
+ $stats->appendChild($srclines);
+
+ $report->appendChild($stats);
+
+ $data = $dom->createElement('data');
+ $all = $dom->createElement('all');
+ $all->setAttribute('name', 'all classes');
+ $data->appendChild($all);
+ $report->appendChild($data);
+
+ $coverageClass = $dom->createElement('coverage');
+ $coverageClass->setAttribute('type', 'class, %');
+ $coverageClass->setAttribute('value', '0% (0/0)');
+ $all->appendChild($coverageClass);
+
+ $coverageMethod = $dom->createElement('coverage');
+ $coverageMethod->setAttribute('type', 'method, %');
+ $coverageMethod->setAttribute('value', '0% (0/0)');
+ $all->appendChild($coverageMethod);
+
+ $coverageBlock = $dom->createElement('coverage');
+ $coverageBlock->setAttribute('type', 'block, %');
+ $coverageBlock->setAttribute('value', '0% (0/0)');
+ $all->appendChild($coverageBlock);
+
+ $elementTotalLines = $dom->createElement('coverage');
+ $elementTotalLines->setAttribute('type', 'line, %');
+ $all->appendChild($elementTotalLines);
+
+ $dirs = array();
+ foreach (array_keys($coverageByFile) as $file)
+ {
+ $dirs[] = str_replace('/', '_', pathinfo($file, PATHINFO_DIRNAME));
+ }
+ $dirs = array_unique($dirs);
+
+ $packagesTab = array();
+ $totalLinesPackageElements = array();
+ foreach ($dirs as $dir)
+ {
+ $coverageClass = $dom->createElement('coverage');
+ $coverageClass->setAttribute('type', 'class, %');
+ $coverageClass->setAttribute('value', '0% (0/0)');
+
+ $coverageMethod = $dom->createElement('coverage');
+ $coverageMethod->setAttribute('type', 'method, %');
+ $coverageMethod->setAttribute('value', '0% (0/0)');
+
+ $coverageBlock = $dom->createElement('coverage');
+ $coverageBlock->setAttribute('type', 'block, %');
+ $coverageBlock->setAttribute('value', '0% (0/0)');
+
+ $totalLinesPackageElements[$dir] = $dom->createElement('coverage');
+ $totalLinesPackageElements[$dir]->setAttribute('type', 'line, %');
+
+ $packagesTab[$dir] = $dom->createElement('package');
+ $packagesTab[$dir]->setAttribute('name', $dir);
+ $packagesTab[$dir]->appendChild($coverageClass);
+ $packagesTab[$dir]->appendChild($coverageMethod);
+ $packagesTab[$dir]->appendChild($coverageBlock);
+ $packagesTab[$dir]->appendChild($totalLinesPackageElements[$dir]);
+ $all->appendChild($packagesTab[$dir]);
+ }
+
+ $totalLines = 0;
+ $totalTestedLines = 0;
+ $testedLinesPackage = array();
+ $totalLinesPackage = array();
+ foreach ($coverageByFile as $file => $coveragePercent)
+ {
+ $key = str_replace('/', '_', pathinfo($file, PATHINFO_DIRNAME));
+ $srcfile = $dom->createElement('srcfile');
+ $srcfile->setAttribute('name', $key);
+ $numberOfLines = $this->getNumberOfLinesFile(sfConfig::get('sf_root_dir') . $file);
+ $testedLines = round(substr($coveragePercent, 0, -1) / 100 * $numberOfLines);
+ $totalLines += $numberOfLines;
+ $totalTestedLines += $testedLines;
+ $testedLinesPackage[$key] += $testedLines;
+ $totalLinesPackage[$key] += $numberOfLines;
+ $coverageString = sprintf('%s (%s/%s)', $coveragePercent, $testedLines, $numberOfLines);
+
+ $coverageClass = $dom->createElement('coverage');
+ $coverageClass->setAttribute('type', 'class, %');
+ $coverageClass->setAttribute('value', '0% (0/0)');
+ $srcfile->appendChild($coverageClass);
+
+ $coverageMethod = $dom->createElement('coverage');
+ $coverageMethod->setAttribute('type', 'method, %');
+ $coverageMethod->setAttribute('value', '0% (0/0)');
+ $srcfile->appendChild($coverageMethod);
+
+ $coverageBlock = $dom->createElement('coverage');
+ $coverageBlock->setAttribute('type', 'block, %');
+ $coverageBlock->setAttribute('value', '0% (0/0)');
+ $srcfile->appendChild($coverageBlock);
+
+ $coverageLine = $dom->createElement('coverage');
+ $coverageLine->setAttribute('type', 'line, %');
+ $coverageLine->setAttribute('value', $coverageString);
+ $srcfile->appendChild($coverageLine);
+
+ $class = $dom->createElement('class');
+ $class->setAttribute('name', $key);
+ $coverageClass2 = clone $coverageClass;
+ $coverageMethod2 = clone $coverageMethod;
+ $coverageBlock2 = clone $coverageBlock;
+ $coverageLine2 = clone $coverageLine;
+ $class->appendChild($coverageClass2);
+ $class->appendChild($coverageMethod2);
+ $class->appendChild($coverageBlock2);
+ $class->appendChild($coverageLine2);
+
+ $method = $dom->createElement('method');
+ $method->setAttribute('name', $key);
+ $coverageMethod3 = clone $coverageMethod;
+ $coverageBlock3 = clone $coverageBlock;
+ $coverageLine3 = clone $coverageLine;
+ $method->appendChild($coverageMethod3);
+ $method->appendChild($coverageBlock3);
+ $method->appendChild($coverageLine3);
+
+ $class->appendChild($method);
+
+ $srcfile->appendChild($class);
+
+ $packagesTab[$key]->appendChild($srcfile);
+ }
+
+ foreach($dirs as $dir)
+ {
+ $totalLinesString = sprintf('%s%% (%s/%s)', round($testedLinesPackage[$dir]/$totalLinesPackage[$dir]*100), $testedLinesPackage[$dir], $totalLinesPackage[$dir]);
+ $totalLinesPackageElements[$dir]->setAttribute('value', $totalLinesString);
+ }
+
+ $stringTotalLines = sprintf('%s%% (%s/%s)', round($totalTestedLines/$totalLines*100), $totalTestedLines, $totalLines);
+ $elementTotalLines->setAttribute('value', $stringTotalLines);
+
+ $srcfiles->setAttribute('value', count($coverageByFile));
+ $srclines->setAttribute('value', $totalLines);
+
+ return $dom->saveXml();
+ }
+
+ protected function getNumberOfLinesFile($file)
+ {
+ return count(file($file));
+ }
+
+ protected function getCoveragePercentByFile()
+ {
+ $coverage = array();
+ $files = sfFinder::type('file')->name('*.php')->in($this->getUnitTestsDir());
+ foreach ($files as $file)
+ {
+ $testFile = substr($file, strlen(sfConfig::get('sf_test_dir')) + 1);
+ $testedFile = $this->getTestedFileFromTestFile($file);
+ $cmd = sprintf('%s symfony test:coverage test/%s %s', sfToolkit::getPhpCli(), $testFile, $testedFile);
+ $output = '';
+ exec($cmd, $output);
+ $percent = substr($output[1], -3);
+ $coverage[$testedFile] = $percent;
+ }
+ return $coverage;
+ }
+
+ protected function getRelativePathFromSfRootDir($path)
+ {
+ return substr($path, strlen($this->getUnitTestsDir()));
+ }
+
+ protected function getUnitTestsDir()
+ {
+ return sfConfig::get('sf_test_dir') . DIRECTORY_SEPARATOR . 'unit';
+ }
+
+ protected function getTestedFileFromTestFile($testFile)
+ {
+ $basename = pathinfo($testFile, PATHINFO_BASENAME);
+ $testedFile = str_replace(array('Test', '.php'), array('', '.class.php'), $basename);
+ $testedPath = str_replace($basename, $testedFile, $this->getRelativePathFromSfRootDir($testFile));
+ return $testedPath;
+ }
+
+}
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.9.0" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+ <name>agEmmaCoverageReportPlugin</name>
+ <channel>plugins.symfony-project.org</channel>
+ <summary>Generate coverage report in emma xml format</summary>
+ <description>Generate coverage report in emma xml format</description>
+ <lead>
+ <name>agallou</name>
+ <user>agallou</user>
+ <email>ecrire@adrien-gallou.fr</email>
+ <active>yes</active>
+ </lead>
+ <date>2010-04-02</date>
+ <time>13:08:50</time>
+ <version>
+ <release>1.0.0</release>
+ <api>1.0.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <license uri="http://www.symfony-project.org/license">MIT license</license>
+ <notes>
+-
+ </notes>
+ <contents>
+ <dir name="/">
+ <file role="data" name="README" />
+ <file role="data" name="LICENSE" />
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>1.4.1</min>
+ </pearinstaller>
+
+ </required>
+ </dependencies>
+ <phprelease/>
+ <changelog>
+ <release>
+ <version>
+ <release>1.0.0</release>
+ <api>1.0.0</api>
+ </version>
+ <stability>
+ <release>stable</release>
+ <api>stable</api>
+ </stability>
+ <license uri="http://www.symfony-project.org/license">MIT license</license>
+ <license>MIT</license>
+ <date>2010-04-02</date>
+ <notes>
+* initial release
+ </notes>
+ </release>
+ </changelog>
+</package>

0 comments on commit b2c4383

Please sign in to comment.