Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
new commands and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 20, 2016
1 parent 1ad7043 commit fdde5af
Show file tree
Hide file tree
Showing 12 changed files with 1,468 additions and 33 deletions.
17 changes: 14 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"type" : "library",
"description" : "Tools for JetBrains TeamCity CI",
"license" : "MIT",
"keywords" : ["TeamCity", "CI", "Tools", "Reports"],
"keywords" : [
"TeamCity", "CI", "Tools", "Reports", "Clover", "pdepend", "PHP Depend", "phploc", "phpmetrics"
],
"minimum-stability" : "stable",
"authors" : [
{
Expand All @@ -14,10 +16,12 @@
],
"require" : {
"php" : ">=5.4",
"jbzoo/console" : "^1.1"
"jbzoo/console" : "^1.1",
"jbzoo/utils" : "^1.6.1"
},
"require-dev" : {
"jbzoo/phpunit" : "^1.5.1"
"jbzoo/phpunit" : "^1.5.1",
"symfony/process" : "^2.8|^3.0"
},
"autoload" : {
"psr-4" : {
Expand All @@ -38,11 +42,18 @@
"@manifest",
"@autoload",
"@phpunit",
"@test-bin",
"@phpmd",
"@phpcs",
"@phpcpd",
"@phploc"
],
"test-bin" : [
"php ./vendor/jbzoo/console/bin/jbzoo teamcity:clover ./tests/fixtures/clover.xml",
"php ./vendor/jbzoo/console/bin/jbzoo teamcity:pdepend ./tests/fixtures/pdepend.xml",
"php ./vendor/jbzoo/console/bin/jbzoo teamcity:phploc ./tests/fixtures/phploc.xml",
"php ./vendor/jbzoo/console/bin/jbzoo teamcity:phpmetrics ./tests/fixtures/phpmetrics.xml"
],
"update-all" : [
"composer self-update --no-interaction",
"composer update --no-interaction --optimize-autoloader"
Expand Down
12 changes: 12 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
JBZoo TeamCity
This file is part of the JBZoo CCK package.
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
@package TeamCity
@license MIT
@copyright Copyright (C) JBZoo.com, All rights reserved.
@link https://github.com/JBZoo/TeamCity
-->
<phpunit bootstrap="tests/autoload.php"
colors="true"
convertErrorsToExceptions="true"
Expand Down
72 changes: 72 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* JBZoo TeamCity
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamCity
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamCity".
*/

namespace JBZoo\TeamCity;

use JBZoo\Console\Command as ConsoleCommand;
use JBZoo\Utils\Cli;

/**
* Class Clover
* @package JBZoo\TeamCity
*/
abstract class Command extends ConsoleCommand
{
protected $_prefix = '';
protected $_exclude = [];
protected $_colmap = [];

/**
* @param array $fields
* @param bool $translate
* @throws Exception
*/
protected function _printFields(array $fields, $translate = true)
{
foreach ($fields as $key => $value) {

if (in_array($key, $this->_exclude, true)) {
continue;
}

if (!$translate) {
$this->_tcEcho($key, $value);

} elseif (isset($this->_colmap[$key]) && $this->_colmap[$key]) {
$this->_tcEcho($this->_colmap[$key], $value);

} else {
throw new Exception("{$key}=>{$value} not found in column map");
}
}
}

/**
* @param $key
* @param $value
*/
protected function _tcEcho($key, $value)
{
$key = str_replace("'", "\\'", $key);
$key = $this->_prefix ? $this->_prefix . ': ' . $key : $key;

if (strpos($value, '.') || is_float($value)) {
$value = round($value, 6);
} else {
$value = str_replace("'", "\\'", $value);
}

Cli::out("##teamcity[buildStatisticValue key='{$key}' value='{$value}']");
}
}
111 changes: 111 additions & 0 deletions src/Command/Clover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* JBZoo TeamCity
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamCity
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamCity".
*/

namespace JBZoo\Console\Command;

use JBZoo\TeamCity\Command as TeamCityCommand;
use JBZoo\Console\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class Clover
* @package JBZoo\Console
*/
class Clover extends TeamCityCommand
{
/**
* {@inheritdoc}
*/
protected function configure() // @codingStandardsIgnoreLine
{
$this
->setName('teamcity:clover')
->setDescription('Parse phpunit clover.xml file and send it to TeamCity report!')
->addArgument('xml', InputOption::VALUE_REQUIRED, 'Path to phploc xml file')
->addOption('crap-threshold', null, InputOption::VALUE_OPTIONAL, 'Path to phploc xml file', 30);
}


/**
* {@inheritdoc}
* @throws Exception
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function execute(InputInterface $input, OutputInterface $output) // @codingStandardsIgnoreLine
{
$this->_executePrepare($input, $output);

$crapThreshold = $this->_getOpt('crap-threshold');
$xmlPath = $input->getArgument('xml');
if (!file_exists($xmlPath)) {
throw new Exception('XML file not found: ' . $xmlPath);
}

$cloverXml = new \SimpleXMLElement($xmlPath, null, true);
$info = $cloverXml->project->metrics;

$coveredClasses = 0;
foreach ($cloverXml->xpath('//class') as $class) {
if ((int)$class->metrics['coveredmethods'] === (int)$class->metrics['methods']) {
$coveredClasses++;
}
}

$data = array(
'CodeCoverageAbsLTotal' => (int)$info['elements'],
'CodeCoverageAbsLCovered' => (int)$info['coveredelements'],
'CodeCoverageAbsBTotal' => (int)$info['statements'],
'CodeCoverageAbsBCovered' => (int)$info['coveredstatements'],
'CodeCoverageAbsMTotal' => (int)$info['methods'],
'CodeCoverageAbsMCovered' => (int)$info['coveredmethods'],
'CodeCoverageAbsCTotal' => (int)$info['classes'],
'CodeCoverageAbsCCovered' => $coveredClasses,
'CodeCoverageL' => $info['elements'] ? $info['coveredelements'] / $info['elements'] * 100 : 0,
'CodeCoverageM' => $info['methods'] ? $info['coveredmethods'] / $info['methods'] * 100 : 0,
'CodeCoverageC' => $info['classes'] ? $coveredClasses / $info['classes'] * 100 : 0,
'Files' => (int)$info['files'],
'LinesOfCode' => (int)$info['loc'],
'NonCommentLinesOfCode' => (int)$info['ncloc'],
);

$data['CodeCoverageB'] = $info['statements'] ? $info['coveredstatements'] / $info['statements'] * 100 : 0;

if ($crapThreshold) {
$crapValues = array();
$crapAmount = 0;

foreach ($cloverXml->xpath('//@crap') as $crap) {
$crap = (float)$crap;
$crapValues[] = $crap;
if ($crap >= $crapThreshold) {
$crapAmount++;
}
}

$crapValuesCount = count($crapValues);
$crapTotal = array_sum($crapValues);

$data['CRAPAmount'] = $crapAmount;
$data['CRAPPercent'] = $crapValuesCount ? $crapAmount / $crapValuesCount * 100 : 0;
$data['CRAPTotal'] = $crapTotal;
$data['CRAPAverage'] = $crapValuesCount ? $crapTotal / $crapValuesCount : 0;
$data['CRAPMaximum'] = max($crapValues);
}

$this->_printFields($data, false);
}
}
109 changes: 109 additions & 0 deletions src/Command/Pdepend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* JBZoo TeamCity
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamCity
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamCity".
*/

namespace JBZoo\Console\Command;

use JBZoo\TeamCity\Command as TeamCityCommand;
use JBZoo\Console\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class Pdepend
* @package JBZoo\Console
*/
class Pdepend extends TeamCityCommand
{
protected $_prefix = 'PHP Depend';

protected $_exclude = [
'generated',
'pdepend',
];

protected $_colmap = [
'ahh' => 'Average Hierarchy Height',
'andc' => 'Average Number of Derived Classes',
'ca' => 'Afferent Coupling',
'calls' => 'Number of Method or Function Calls',
'cbo' => 'Coupling Between Objects',
'ccn' => 'Cyclomatic Complexity Number',
'ccn2' => 'Extended Cyclomatic Complexity Number',
'ce' => 'Efferent Coupling',
'cis' => 'Class Interface Size',
'cloc' => 'Comment Lines fo Code',
'clsa' => 'Number of Abstract Classes',
'clsc' => 'Number of Concrete Classes',
'cr' => 'Code Rank',
'csz' => 'Class Size',
'dit' => 'Depth of Inheritance Tree',
'eloc' => 'Executable Lines of Code',
'fanout' => 'Number of Fanouts',
'leafs' => 'Number of Leaf Classes',
'lloc' => 'Logical Lines Of Code',
'loc' => 'Lines Of Code',
'maxDIT' => 'Max Depth of Inheritance Tree',
'noam' => 'Number Of Added Methods',
'nocc' => 'Number Of Child Classes',
'noom' => 'Number Of Overwritten Methods',
'ncloc' => 'Non Comment Lines Of Code',
'noc' => 'Number Of Classes',
'nof' => 'Number Of Functions',
'noi' => 'Number Of Interfaces',
'nom' => 'Number Of Methods',
'npm' => 'Number of Public Methods',
'npath' => 'NPath Complexity',
'nop' => 'Number of Packages',
'rcr' => 'Reverse Code Rank',
'roots' => 'Number of Root Classes',
'vars' => 'Properties',
'varsi' => 'Inherited Properties',
'varsnp' => 'Non Private Properties',
'wmc' => 'Weighted Method Count',
'wmci' => 'Inherited Weighted Method Count',
'wmcnp' => 'Non Private Weighted Method Count',
];

/**
* {@inheritdoc}
*/
protected function configure() // @codingStandardsIgnoreLine
{
$this
->setName('teamcity:pdepend')
->setDescription('Parse pdepend summary.xml and send it to TeamCity report!')
->addArgument('xml', InputOption::VALUE_REQUIRED, 'Path to phploc xml file');
}

/**
* {@inheritdoc}
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) // @codingStandardsIgnoreLine
{
$this->_executePrepare($input, $output);

$xmlPath = $input->getArgument('xml');

if (!file_exists($xmlPath)) {
throw new Exception('XML file not found: ' . $xmlPath);
}

$cloverXml = new \SimpleXMLElement($xmlPath, null, true);
$fields = current($cloverXml->attributes());

$this->_printFields($fields);
}
}
Loading

0 comments on commit fdde5af

Please sign in to comment.