Skip to content

Commit

Permalink
Merge pull request #2 from Sweetchuck/more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
Sweetchuck authored Jan 9, 2018
2 parents a2c0e1f + 86a9083 commit 837f910
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 23 deletions.
2 changes: 1 addition & 1 deletion RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected function getTaskPhpmdLint()
{
return $this
->taskPhpmdLintFiles()
->addPathsFromFile('./rulesets/custom.include-pattern.txt')
->setInputFile('./rulesets/custom.include-pattern.txt')
->addExcludePathsFromFile('./rulesets/custom.exclude-pattern.txt')
->setRuleSetFileNames(['custom'])
->setOutput($this->output());
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"require": {
"php": ">=7.1",
"consolidation/robo": "^1.0",
"phpmd/phpmd": "^2.6"
"phpmd/phpmd": "^2.6",
"symfony/process": "^3.2"
},
"require-dev": {
"codeception/codeception": "^2.2",
"danielstjules/stringy": "^3.0",
"mikey179/vfsStream": "^1.6",
"sweetchuck/codeception-module-robo-task-runner": "^0.0",
"sweetchuck/git-hooks": "^0.0",
"sweetchuck/robo-git": "^0.0",
Expand Down
62 changes: 54 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions src/Task/PhpmdLintTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,6 @@ public function setPaths(array $value)

return $this;
}

/**
* @return $this
*/
public function addPathsFromFile(string $fileName)
{
$lines = array_map('trim', file($fileName));
$this->paths = array_fill_keys(array_filter($lines), true) + $this->paths;

return $this;
}
// endregion

// region reportFormat
Expand Down Expand Up @@ -576,7 +565,7 @@ public function setOptions(array $option)
$this->setReportFile($value);
break;

case 'reportFileHTML':
case 'reportFileHtml':
$this->setReportFileHtml($value);
break;

Expand Down Expand Up @@ -837,7 +826,7 @@ protected function getCommandOptions(): array
'value' => $this->getInputFile(),
],
'coverage' => [
'type' => 'option:value',
'type' => 'option:flag',
'value' => $this->getCoverage(),
],
'reportFile' => [
Expand All @@ -846,14 +835,17 @@ protected function getCommandOptions(): array
],
'reportFileHtml' => [
'type' => 'option:value',
'name' => 'reportfile-html',
'value' => $this->getReportFileHtml(),
],
'reportFileText' => [
'type' => 'option:value',
'name' => 'reportfile-text',
'value' => $this->getReportFileText(),
],
'reportFileXml' => [
'type' => 'option:value',
'name' => 'reportfile-xml',
'value' => $this->getReportFileXml(),
],
'suffixes' => [
Expand Down
145 changes: 145 additions & 0 deletions tests/unit/Task/PhpmdTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Sweetchuck\Robo\PhpMessDetector\Tests\Unit\Task;

use Codeception\Test\Unit;
use Codeception\Util\Stub;
use org\bovigo\vfs\vfsStream;
use Robo\Robo;
use Sweetchuck\Codeception\Module\RoboTaskRunner\DummyProcess;
use Sweetchuck\Robo\PhpMessDetector\Task\PhpmdLintFilesTask;
use Webmozart\PathUtil\Path;

class PhpmdTaskTest extends Unit
{
Expand All @@ -24,7 +29,12 @@ public function casesGetCommand(): array
"'html'",
"'a.xml,c.xml'",
"--minimumpriority '4'",
"--inputfile 'if.php'",
"--coverage",
"--reportfile 'd.html'",
"--reportfile-html 'rf.html'",
"--reportfile-text 'rf.txt'",
"--reportfile-xml 'rf.xml'",
"--suffixes 'php,inc'",
"--exclude 'd.php,f.php'",
'--strict',
Expand All @@ -46,7 +56,12 @@ public function casesGetCommand(): array
'c.xml' => true,
],
'minimumPriority' => 4,
'inputFile' => 'if.php',
'coverage' => true,
'reportFile' => 'd.html',
'reportFileHtml' => 'rf.html',
'reportFileText' => 'rf.txt',
'reportFileXml' => 'rf.xml',
'suffixes' => [
'php' => true,
'phtml' => false,
Expand All @@ -73,4 +88,134 @@ public function testGetCommand(string $expected, array $options)
$task->setOptions($options);
$this->tester->assertEquals($expected, $task->getCommand());
}

public function testSuffixAddRemove()
{
$task = new PhpmdLintFilesTask();
$task
->setSuffixes(['a', 'b', 'c'])
->removeSuffix('b')
->addSuffix('d');
$expected = [
'a' => true,
'c' => true,
'd' => true,
];
$this->tester->assertEquals($expected, $task->getSuffixes());
}

public function testExcludePaths()
{
$task = new PhpmdLintFilesTask();
$task
->setPhpmdExecutable('phpmd')
->setExcludePaths(['a', 'b', 'c'])
->removeExcludePath('b')
->addExcludePath('d');

$this->tester->assertEquals(
['a' => true, 'c' => true, 'd' => true],
$task->getExcludePaths()
);

$vfs = vfsStream::setup(
'root',
0777,
[
__FUNCTION__ => [
'exclude-pattern.txt' => implode("\n", [
'src/',
'a',
'b',
'',
])
]
]
);

$fileName = $vfs->url() . '/' . __FUNCTION__ . '/exclude-pattern.txt';
$task->addExcludePathsFromFile($fileName);

$this->tester->assertEquals(
"phpmd 'text' --exclude 'src/,a,b,c,d'",
$task->getCommand()
);
}

public function casesRunSuccess(): array
{
$vfs = vfsStream::setup(
'root',
0777,
[
__FUNCTION__ => [],
]
);

return [
'basic' => [
[
'exitCode' => 0,
],
[
'workingDirectory' => $vfs->url(),
'reportFile' => __FUNCTION__ . '/basic/foo/phpmd.txt',
],
],
];
}

/**
* @dataProvider casesRunSuccess
*/
public function testRunSuccess(array $expected, array $options)
{
$container = Robo::createDefaultContainer();
Robo::setContainer($container);

/** @var \Sweetchuck\Robo\PhpMessDetector\Task\PhpmdLintFilesTask $task */
$task = Stub::construct(
PhpmdLintFilesTask::class,
[],
[
'processClass' => DummyProcess::class,
]
);
$task->setOptions($options);

$processIndex = count(DummyProcess::$instances);
DummyProcess::$prophecy[$processIndex] = [
'exitCode' => 0,
'stdOutput' => '',
'stdError' => '',
];

$result = $task->run();

$this->tester->assertEquals(
$expected['exitCode'],
$result->getExitCode(),
'Result "exitCode"'
);

$workingDirectory = $options['workingDirectory'] ?? '.';
$reportFileOptions = [
'reportFile',
'reportFileHtml',
'reportFileText',
'reportFileXml',
];
foreach ($reportFileOptions as $reportFileOption) {
if (empty($options[$reportFileOption])) {
continue;
}

$fileName = Path::join($workingDirectory, $options[$reportFileOption]);
$dirName = Path::getDirectory($fileName);
$this->tester->assertFileExists(
$dirName,
"Directory is prepared for file; '$reportFileOption' = '$fileName'"
);
}
}
}

0 comments on commit 837f910

Please sign in to comment.