diff --git a/bin/phpcsDiffFilter b/bin/phpcsDiffFilter
index 8f00341..8bef469 100755
--- a/bin/phpcsDiffFilter
+++ b/bin/phpcsDiffFilter
@@ -1,19 +1,3 @@
#!/usr/bin/env php
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';
diff --git a/bin/phpmdDiffFilter b/bin/phpmdDiffFilter
index 146753e..4b061fe 100755
--- a/bin/phpmdDiffFilter
+++ b/bin/phpmdDiffFilter
@@ -1,24 +1,3 @@
#!/usr/bin/env php
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';
diff --git a/bin/phpunitDiffFilter b/bin/phpunitDiffFilter
index 3dd94e0..89752cd 100755
--- a/bin/phpunitDiffFilter
+++ b/bin/phpunitDiffFilter
@@ -1,19 +1,3 @@
#!/usr/bin/env php
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';
diff --git a/phpunit.xml b/phpunit.xml
index 60e4c49..9efa5b8 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -13,7 +13,7 @@
-
+
src
diff --git a/src/ArgParser.php b/src/ArgParser.php
index 1ee8696..47afb45 100644
--- a/src/ArgParser.php
+++ b/src/ArgParser.php
@@ -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;
}
@@ -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;
}
diff --git a/src/DiffFileLoader.php b/src/DiffFileLoader.php
index 7bf49a8..f66c07d 100644
--- a/src/DiffFileLoader.php
+++ b/src/DiffFileLoader.php
@@ -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;
}
diff --git a/src/PhpMdLoader.php b/src/PhpMdLoader.php
index ee901fb..4a0262e 100644
--- a/src/PhpMdLoader.php
+++ b/src/PhpMdLoader.php
@@ -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;
diff --git a/functions.php b/src/functions.php
similarity index 62%
rename from functions.php
rename to src/functions.php
index 2cf7676..c1a613e 100644
--- a/functions.php
+++ b/src/functions.php
@@ -1,11 +1,13 @@
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 == "-") {
@@ -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;
@@ -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
+ }
+ );
}
diff --git a/src/runners/phpcsDiffFilter.php b/src/runners/phpcsDiffFilter.php
new file mode 100644
index 0000000..e74c66f
--- /dev/null
+++ b/src/runners/phpcsDiffFilter.php
@@ -0,0 +1,20 @@
+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);
diff --git a/src/runners/phpmdDiffFilter.php b/src/runners/phpmdDiffFilter.php
new file mode 100644
index 0000000..8fe9e62
--- /dev/null
+++ b/src/runners/phpmdDiffFilter.php
@@ -0,0 +1,25 @@
+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);
diff --git a/src/runners/phpunitDiffFilter.php b/src/runners/phpunitDiffFilter.php
new file mode 100644
index 0000000..1ed0012
--- /dev/null
+++ b/src/runners/phpunitDiffFilter.php
@@ -0,0 +1,20 @@
+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);
diff --git a/tests/DiffFileLoadTest.php b/tests/DiffFileLoadTest.php
index 8ceabad..971b8e4 100644
--- a/tests/DiffFileLoadTest.php
+++ b/tests/DiffFileLoadTest.php
@@ -22,7 +22,7 @@ public function testDiffResultsMatch($file, $expected)
*/
public function testNonExistantFile()
{
- $changed = $this->getChangedLines('ufhbubfusdf');
+ $this->getChangedLines('ufhbubfusdf');
}
public function getResults()
diff --git a/tests/LoadPhpcsReportTest.php b/tests/LoadPhpcsReportTest.php
index 3a51760..62c9b23 100644
--- a/tests/LoadPhpcsReportTest.php
+++ b/tests/LoadPhpcsReportTest.php
@@ -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()
diff --git a/tests/PhpcsDiffFilterTest.php b/tests/PhpcsDiffFilterTest.php
new file mode 100644
index 0000000..2415511
--- /dev/null
+++ b/tests/PhpcsDiffFilterTest.php
@@ -0,0 +1,21 @@
+assertContains('100.00%', $output);
+ }
+}
diff --git a/tests/PhpmdDiffFilterTest.php b/tests/PhpmdDiffFilterTest.php
new file mode 100644
index 0000000..3013141
--- /dev/null
+++ b/tests/PhpmdDiffFilterTest.php
@@ -0,0 +1,63 @@
+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");
+ }
+}
diff --git a/tests/PhpunitDiffFilterTest.php b/tests/PhpunitDiffFilterTest.php
new file mode 100644
index 0000000..d584301
--- /dev/null
+++ b/tests/PhpunitDiffFilterTest.php
@@ -0,0 +1,81 @@
+assertContains('100.00%', $output);
+ }
+
+ public function testFailingBuild()
+ {
+ $GLOBALS['argv'] = [
+ 'phpunitDiffFilter',
+ __DIR__ . '/fixtures/newFile.txt',
+ __DIR__ . '/fixtures/coverage-change.xml',
+ 70
+ ];
+ try {
+ ob_start();
+ require(__DIR__ . "/../src/runners/phpunitDiffFilter.php");
+ } catch (Exception $e) {
+ $output = ob_get_clean();
+ $this->assertEquals(2, $e->getCode());
+ $this->assertContains('66.67%', $output);
+ return;
+ }
+
+ $this->fail("no exception thrown");
+ }
+
+ public function testPassingLowPercentage()
+ {
+ $GLOBALS['argv'] = [
+ 'phpunitDiffFilter',
+ __DIR__ . '/fixtures/newFile.txt',
+ __DIR__ . '/fixtures/coverage-change.xml',
+ 60
+ ];
+
+ ob_start();
+ require(__DIR__ . "/../src/runners/phpunitDiffFilter.php");
+ $output = ob_get_clean();
+ $this->assertContains('66.67%', $output);
+ }
+
+ public function testNoCoveredLines()
+ {
+ $GLOBALS['argv'] = [
+ 'phpunitDiffFilter',
+ __DIR__ . '/fixtures/removeFile.txt',
+ __DIR__ . '/fixtures/coverage-change.xml',
+ ];
+
+ ob_start();
+ require(__DIR__ . "/../src/runners/phpunitDiffFilter.php");
+ $output = ob_get_clean();
+ $this->assertContains('No lines found', $output);
+ }
+}
diff --git a/tests/fixtures/coverage-change.xml b/tests/fixtures/coverage-change.xml
new file mode 100644
index 0000000..f5ee4ed
--- /dev/null
+++ b/tests/fixtures/coverage-change.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/fixtures/phpmd-change.xml b/tests/fixtures/phpmd-change.xml
new file mode 100644
index 0000000..1810c31
--- /dev/null
+++ b/tests/fixtures/phpmd-change.xml
@@ -0,0 +1,8 @@
+
+
+
+
+ The method addUnCoveredLine has a boolean flag argument $message, which is a certain sign of a Single Responsibility Principle violation.
+
+
+