-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from MontealegreLuis/master
Fix validation of 'git' and 'files' options
- Loading branch information
Showing
6 changed files
with
251 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="vendor/autoload.php"> | ||
|
||
<testsuites> | ||
<testsuite name="Unit tests"> | ||
<directory>tests/unit</directory> | ||
</testsuite> | ||
<testsuite name="Integration tests"> | ||
<directory>tests/integration</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace JMOlivas\Phpqa\Input; | ||
|
||
class FilesOption | ||
{ | ||
/** @var array */ | ||
private $files; | ||
|
||
/** | ||
* @param array $files | ||
*/ | ||
public function __construct(array $files) | ||
{ | ||
$this->files = $files; | ||
} | ||
|
||
/** | ||
* Returns true if this option is provided but has no values | ||
* | ||
* @return bool | ||
*/ | ||
public function isEmpty() | ||
{ | ||
return count($this->files) === 1 && $this->files[0] === null; | ||
} | ||
|
||
/** | ||
* Returns true if this option is not provided | ||
* | ||
* @return bool | ||
*/ | ||
public function isAbsent() | ||
{ | ||
return empty($this->files); | ||
} | ||
|
||
/** | ||
* Normalize the provided values as an array | ||
* | ||
* - If it's either empty or absent, it returns an empty array | ||
* - If it's a single value separated by commas, it converts it to array | ||
* - Otherwise returns the value as is. | ||
* | ||
* @return array | ||
*/ | ||
public function normalize() | ||
{ | ||
if ($this->isAbsent() || $this->isEmpty()) { | ||
return []; | ||
} | ||
if (count($this->files) === 1 && strpos($this->files[0], ',') !== false) { | ||
return explode(',', $this->files[0]); | ||
} | ||
|
||
return $this->files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace JMOlivas\Phpqa\Command; | ||
|
||
use JMOlivas\Phpqa\Console\Application; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class AnalyzeCommandTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage You must set `files` or `git` options. | ||
*/ | ||
function it_should_throw_exception_if_neither_files_nor_git_options_are_provided() | ||
{ | ||
$application = new Application(); | ||
$command = new AnalyzeCommand(); | ||
$command->setApplication($application); | ||
|
||
$tester = new CommandTester($command); | ||
|
||
$tester->execute([]); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Options `files` and `git` cannot be used in combination. | ||
*/ | ||
function it_should_throw_exception_if_both_files_and_git_options_are_provided() | ||
{ | ||
$application = new Application(); | ||
$command = new AnalyzeCommand(); | ||
$command->setApplication($application); | ||
|
||
$tester = new CommandTester($command); | ||
|
||
$tester->execute([ | ||
'--files' => [null], | ||
'--git' => true | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Options `files` needs at least one file. | ||
*/ | ||
function it_should_throw_exception_if_files_is_provided_but_it_is_empty() | ||
{ | ||
$application = new Application(); | ||
$command = new AnalyzeCommand(); | ||
$command->setApplication($application); | ||
|
||
$tester = new CommandTester($command); | ||
|
||
$tester->execute([ | ||
'--files' => [null], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace JMOlivas\Phpqa\Input; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
class FilesOptionTest extends TestCase | ||
{ | ||
/** @test */ | ||
function it_should_recognize_if_option_is_absent() | ||
{ | ||
$absentInput = []; | ||
$files = new FilesOption($absentInput); | ||
|
||
$this->assertTrue($files->isAbsent()); | ||
} | ||
|
||
/** @test */ | ||
function it_should_recognize_if_option_is_provided_but_is_empty() | ||
{ | ||
$emptyInput = [null]; | ||
$files = new FilesOption($emptyInput); | ||
|
||
$this->assertTrue($files->isEmpty()); | ||
} | ||
|
||
/** @test */ | ||
function it_should_recognize_if_option_is_provided_correctly() | ||
{ | ||
$validInput = ['src/']; | ||
$files = new FilesOption($validInput); | ||
|
||
$this->assertFalse($files->isAbsent()); | ||
$this->assertFalse($files->isEmpty()); | ||
} | ||
|
||
/** @test */ | ||
function it_should_normalize_input_separated_by_commas() | ||
{ | ||
// bin/phpqa analyze --files=src/,test/ | ||
$singleInputWithMultipleValues = ['src/,test/']; | ||
$files = new FilesOption($singleInputWithMultipleValues); | ||
|
||
$values = $files->normalize(); | ||
|
||
$this->assertCount(2, $values); | ||
$this->assertEquals('src/', $values[0]); | ||
$this->assertEquals('test/', $values[1]); | ||
} | ||
|
||
/** @test */ | ||
function it_should_return_multiple_files_input_as_is() | ||
{ | ||
// bin/phpqa analyze --files=src/ --files=test/ | ||
$singleInputWithMultipleValues = ['src/','test/']; | ||
$files = new FilesOption($singleInputWithMultipleValues); | ||
|
||
$values = $files->normalize(); | ||
|
||
$this->assertCount(2, $values); | ||
$this->assertEquals('src/', $values[0]); | ||
$this->assertEquals('test/', $values[1]); | ||
} | ||
|
||
/** @test */ | ||
function it_should_return_empty_array_if_input_is_absent() | ||
{ | ||
$absentInput = []; | ||
$files = new FilesOption($absentInput); | ||
|
||
$this->assertCount(0, $files->normalize()); | ||
} | ||
|
||
/** @test */ | ||
function it_should_return_empty_array_if_input_is_empty() | ||
{ | ||
$emptyInput = [null]; | ||
$files = new FilesOption($emptyInput); | ||
|
||
$this->assertCount(0, $files->normalize()); | ||
} | ||
} |