Skip to content

Commit

Permalink
Added functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
exussum12 committed Feb 11, 2017
1 parent 46837ea commit 81067df
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 79 deletions.
18 changes: 1 addition & 17 deletions bin/phpcsDiffFilter
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#!/usr/bin/env php
<?php
namespace exussum12\CoverageChecker;

include (__DIR__ . "/../functions.php");

findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($args->getArg(1));
$phpcs = new PhpCsLoader($args->getArg(2));
$coverageCheck = new CoverageCheck($diff, $phpcs, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
require __DIR__ . '/../src/runners/phpcsDiffFilter.php';
23 changes: 1 addition & 22 deletions bin/phpmdDiffFilter
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
#!/usr/bin/env php
<?php
namespace exussum12\CoverageChecker;

include (__DIR__ . "/../functions.php");

findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();

$diff = new DiffFileLoader(adjustForStdIn($args->getArg(1)));
$phpmd = new PhpMdLoader(adjustForStdIn($args->getArg(2)));
if ($args->getArg("strict")) {
$phpmd = new PhpMdLoaderStrict(adjustForStdIn($args->getArg(2)));
}

$coverageCheck = new CoverageCheck($diff, $phpmd, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
require __DIR__ . '/../src/runners/phpmdDiffFilter.php';
18 changes: 1 addition & 17 deletions bin/phpunitDiffFilter
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#!/usr/bin/env php
<?php
namespace exussum12\CoverageChecker;

require_once (__DIR__ . "/../functions.php");

findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($args->getArg(1));
$phpunit = new XMLReport($args->getArg(2));
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
require __DIR__ . '/../src/runners/phpunitDiffFilter.php';
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
Expand Down
5 changes: 2 additions & 3 deletions src/ArgParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getArg($name)

protected function numericArg($position)
{
foreach($this->args as $arg) {
foreach ($this->args as $arg) {
if ($arg{0} != '-' && $position-- == 0) {
return $arg;
}
Expand All @@ -35,8 +35,7 @@ protected function letterArg($name)
$name = strlen($name) == 1 ?
"-" . $name :
"--" . $name;

foreach($this->args as $arg) {
foreach ($this->args as $arg) {
if ($arg{0} == '-' && $name == $arg) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DiffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getChangedLines()
private function getLineHandle($line)
{
foreach ($this->diffLines as $lineType) {
$lineType = $this->getClass($lineType);
$lineType = $this->getClass($lineType);
if ($lineType->isValid($line)) {
return $lineType;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhpMdLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public function getLines()
public function isValidLine($file, $lineNumber)
{
$valid = true;
foreach($this->errorRanges[$file] as $number => $errors) {
if (
foreach ($this->errorRanges[$file] as $number => $errors) {
if ((
$errors['start'] >= $lineNumber &&
$errors['end'] <= $lineNumber
) {
)) {
//unset this error
unset($this->errorRanges[$file][$number]);
$valid = false;
Expand Down
52 changes: 39 additions & 13 deletions functions.php → src/functions.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
namespace exussum12\CoverageChecker;

use Exception;

function findAutoLoader()
{
$locations = [
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/autoload.php'
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../autoload.php'
];

$found = false;
Expand All @@ -17,27 +19,31 @@ function findAutoLoader()
break;
}
}

// @codeCoverageIgnoreStart
if (!$found) {
error_log(
"Can't find the autoload file," .
"please make sure 'composer install' has been run"
);

exit(1);
// @codeCoverageIgnoreEnd
}
}

function checkCallIsCorrect(ArgParser $args)
{
if (false === $args->getArg(1) || false === $args->getArg(2)) {
error_log(
"Missing arguments, please call with diff and check file"
if (!$args->getArg(1) || !$args->getArg(2)) {
throw new Exception(
"Missing arguments, please call with diff and check file",
1
);
exit(1);
}
}

/**
* @codeCoverageIgnore
*/
function adjustForStdIn($argument)
{
if ($argument == "-") {
Expand All @@ -63,14 +69,14 @@ function getMinPercent($percent)

function handleOutput($lines, $minimumPercentCovered)
{
$coveredLines = count($lines['coveredLines'], COUNT_RECURSIVE);
$uncoveredLines = count($lines['uncoveredLines'], COUNT_RECURSIVE);
$coveredLines = calculateLines($lines['coveredLines']);
$uncoveredLines = calculateLines($lines['uncoveredLines']);


if ($coveredLines + $uncoveredLines == 0) {
echo "No lines found!";
exit(0);
return;
}

$percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));

$extra = PHP_EOL;
Expand All @@ -85,8 +91,28 @@ function handleOutput($lines, $minimumPercentCovered)
printf('%.2f%% Covered%s', $percentCovered, $extra);

if ($percentCovered >= $minimumPercentCovered) {
exit(0);
return;
}

exit(2);
throw new Exception(
"Failing due to coverage being lower than threshold",
2
);
}

function calculateLines($lines)
{
return count($lines, COUNT_RECURSIVE) - count($lines);
}

function addExceptionHandler()
{
set_exception_handler(
function (Exception $exception) {
// @codeCoverageIgnoreStart
error_log($exception->getMessage());
exit($exception->getCode());
// @codeCoverageIgnoreEnd
}
);
}
20 changes: 20 additions & 0 deletions src/runners/phpcsDiffFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace exussum12\CoverageChecker;

require_once __DIR__ . "/../functions.php";
global $argv;

addExceptionHandler();
findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($args->getArg(1));
$phpcs = new PhpCsLoader($args->getArg(2));
$coverageCheck = new CoverageCheck($diff, $phpcs, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
25 changes: 25 additions & 0 deletions src/runners/phpmdDiffFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace exussum12\CoverageChecker;

require_once __DIR__ . "/../functions.php";
global $argv;

addExceptionHandler();
findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();

$diff = new DiffFileLoader(adjustForStdIn($args->getArg(1)));
$phpmd = new PhpMdLoader(adjustForStdIn($args->getArg(2)));
if ($args->getArg("strict")) {
$phpmd = new PhpMdLoaderStrict(adjustForStdIn($args->getArg(2)));
}

$coverageCheck = new CoverageCheck($diff, $phpmd, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
20 changes: 20 additions & 0 deletions src/runners/phpunitDiffFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace exussum12\CoverageChecker;

global $argv;

require_once __DIR__ . "/../functions.php";
addExceptionHandler();
findAutoLoader();
$args = new ArgParser($argv);
checkCallIsCorrect($args);
$minimumPercentCovered = getMinPercent($args->getArg(3));

$matcher = new FileMatchers\EndsWith();
$diff = new DiffFileLoader($args->getArg(1));
$phpunit = new XMLReport($args->getArg(2));
$coverageCheck = new CoverageCheck($diff, $phpunit, $matcher);

$lines = $coverageCheck->getCoveredLines();

handleOutput($lines, $minimumPercentCovered);
2 changes: 1 addition & 1 deletion tests/DiffFileLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testDiffResultsMatch($file, $expected)
*/
public function testNonExistantFile()
{
$changed = $this->getChangedLines('ufhbubfusdf');
$this->getChangedLines('ufhbubfusdf');
}

public function getResults()
Expand Down
2 changes: 1 addition & 1 deletion tests/LoadPhpcsReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testCanMakeClass()
*/
public function testRejectsInvalidData()
{
$phpcs = new PhpCsLoader(__DIR__ . '/fixtures/change.txt');
new PhpCsLoader(__DIR__ . '/fixtures/change.txt');
}

public function testCorrectMissingFile()
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpcsDiffFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace exussum12\CoverageChecker\tests;

use PHPUnit\Framework\TestCase;

class PhpcsDiffFilterTest extends TestCase
{

public function testValid()
{
$GLOBALS['argv'] = [
'phpunitDiffFilter',
__DIR__ . '/fixtures/change.txt',
__DIR__ . '/fixtures/phpcs.json'
];
ob_start();
require(__DIR__ . "/../src/runners/phpcsDiffFilter.php");
$output = ob_get_clean();
$this->assertContains('100.00%', $output);
}
}
63 changes: 63 additions & 0 deletions tests/PhpmdDiffFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace exussum12\CoverageChecker\tests;

use PHPUnit\Framework\TestCase;
use Exception;

class PhpmdDiffFilterTest extends TestCase
{

public function testValid()
{
$GLOBALS['argv'] = [
'phpunitDiffFilter',
__DIR__ . '/fixtures/change.txt',
__DIR__ . '/fixtures/phpmd.xml'
];
ob_start();
require(__DIR__ . "/../src/runners/phpmdDiffFilter.php");
$output = ob_get_clean();
$this->assertContains('100.00%', $output);
}

public function testNoValidLines()
{
$GLOBALS['argv'] = [
'phpunitDiffFilter',
__DIR__ . '/fixtures/change.txt',
__DIR__ . '/fixtures/phpmd-change.xml',
];
try {
ob_start();
require(__DIR__ . "/../src/runners/phpmdDiffFilter.php");
} catch (Exception $e) {
$output = ob_get_clean();
$this->assertEquals(2, $e->getCode());
$this->assertContains('0.00%', $output);
return;
}
$this->fail("no exception thrown");

}

public function testNoValidLinesStrict()
{
$GLOBALS['argv'] = [
'phpunitDiffFilter',
__DIR__ . '/fixtures/change.txt',
__DIR__ . '/fixtures/phpmd-change.xml',
'--strict',
];
try {
ob_start();
require(__DIR__ . "/../src/runners/phpmdDiffFilter.php");
} catch (Exception $e) {
$output = ob_get_clean();
$this->assertEquals(2, $e->getCode());
$this->assertContains('0%', $output);
return;
}

$this->fail("no exception thrown");
}
}
Loading

0 comments on commit 81067df

Please sign in to comment.