-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
63 changed files
with
539 additions
and
2 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,9 @@ | ||
language: php | ||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
|
||
before_script: composer install --dev | ||
|
||
script: phpunit --coverage-text |
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,190 @@ | ||
<?php | ||
|
||
namespace Incenteev\ParameterHandler\Tests; | ||
|
||
use Incenteev\ParameterHandler\ScriptHandler; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTestCase; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class ScriptHandlerTest extends ProphecyTestCase | ||
{ | ||
private $event; | ||
private $io; | ||
private $package; | ||
private $environmentBackup = array(); | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->event = $this->prophesize('Composer\Script\Event'); | ||
$this->io = $this->prophesize('Composer\IO\IOInterface'); | ||
$this->package = $this->prophesize('Composer\Package\PackageInterface'); | ||
$composer = $this->prophesize('Composer\Composer'); | ||
|
||
$composer->getPackage()->willReturn($this->package); | ||
$this->event->getComposer()->willReturn($composer); | ||
$this->event->getIO()->willReturn($this->io); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
|
||
foreach ($this->environmentBackup as $var => $value) { | ||
if (false === $value) { | ||
putenv($var); | ||
} else { | ||
putenv($var.'='.$value); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvalidConfiguration | ||
*/ | ||
public function testInvalidConfiguration(array $extras, $exceptionMessage) | ||
{ | ||
$this->package->getExtra()->willReturn($extras); | ||
|
||
chdir(__DIR__); | ||
|
||
$this->setExpectedException('InvalidArgumentException', $exceptionMessage); | ||
|
||
ScriptHandler::buildParameters($this->event->reveal()); | ||
} | ||
|
||
public function provideInvalidConfiguration() | ||
{ | ||
return array( | ||
'no extra' => array( | ||
array(), | ||
'The extra.incenteev-parameters.file setting is required to use this script handler.', | ||
), | ||
'invalid type' => array( | ||
array('incenteev-parameters' => 'not an array'), | ||
'The extra.incenteev-parameters.file setting is required to use this script handler.', | ||
), | ||
'no file' => array( | ||
array('incenteev-parameters' => array()), | ||
'The extra.incenteev-parameters.file setting is required to use this script handler.', | ||
), | ||
'missing default dist file' => array( | ||
array('incenteev-parameters' => array( | ||
'file' => 'fixtures/invalid/missing.yml', | ||
)), | ||
'The dist file "fixtures/invalid/missing.yml.dist" does not exist. Check your dist-file config or create it.', | ||
), | ||
'missing custom dist file' => array( | ||
array('incenteev-parameters' => array( | ||
'file' => 'fixtures/invalid/missing.yml', | ||
'dist-file' => 'fixtures/invalid/non-existent.dist.yml', | ||
)), | ||
'The dist file "fixtures/invalid/non-existent.dist.yml" does not exist. Check your dist-file config or create it.', | ||
), | ||
'missing top level key in dist file' => array( | ||
array('incenteev-parameters' => array( | ||
'file' => 'fixtures/invalid/missing_top_level.yml', | ||
)), | ||
'The dist file seems invalid.', | ||
), | ||
'invalid values in the existing file' => array( | ||
array('incenteev-parameters' => array( | ||
'file' => 'fixtures/invalid/invalid_existing_values.yml', | ||
)), | ||
'The existing "fixtures/invalid/invalid_existing_values.yml" file does not contain an array', | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider provideParameterHandlingTestCases | ||
*/ | ||
public function testParameterHandling($testCaseName) | ||
{ | ||
$dataDir = __DIR__.'/fixtures/testcases/'.$testCaseName; | ||
|
||
$testCase = array_replace_recursive( | ||
array( | ||
'title' => 'unknown test', | ||
'config' => array( | ||
'file' => 'parameters.yml', | ||
), | ||
'dist-file' => 'parameters.yml.dist', | ||
'environment' => array(), | ||
'interactive' => false, | ||
), | ||
(array) Yaml::parse($dataDir.'/setup.yml') | ||
); | ||
|
||
$workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler'; | ||
$exists = $this->initializeTestCase($testCase, $dataDir, $workingDir); | ||
|
||
$this->package->getExtra()->willReturn(array('incenteev-parameters' => $testCase['config'])); | ||
|
||
$message = sprintf('<info>%s the "%s" file.</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']); | ||
$this->io->write($message)->shouldBeCalled(); | ||
|
||
$this->setInteractionExpectations($testCase); | ||
|
||
ScriptHandler::buildParameters($this->event->reveal()); | ||
|
||
$this->assertFileEquals($dataDir.'/expected.yml', $workingDir.'/'.$testCase['config']['file'], $testCase['title']); | ||
} | ||
|
||
private function initializeTestCase(array $testCase, $dataDir, $workingDir) | ||
{ | ||
$fs = new Filesystem(); | ||
|
||
if (is_dir($workingDir)) { | ||
$fs->remove($workingDir); | ||
} | ||
|
||
$fs->copy($dataDir.'/dist.yml', $workingDir.'/'. $testCase['dist-file']); | ||
|
||
if ($exists = file_exists($dataDir.'/existing.yml')) { | ||
$fs->copy($dataDir.'/existing.yml', $workingDir.'/'.$testCase['config']['file']); | ||
} | ||
|
||
foreach ($testCase['environment'] as $var => $value) { | ||
$this->environmentBackup[$var] = getenv($var); | ||
putenv($var.'='.$value); | ||
}; | ||
|
||
chdir($workingDir); | ||
|
||
return $exists; | ||
} | ||
|
||
private function setInteractionExpectations(array $testCase) | ||
{ | ||
$this->io->isInteractive()->willReturn($testCase['interactive']); | ||
|
||
if (!$testCase['interactive']) { | ||
return; | ||
} | ||
|
||
if (!empty($testCase['requested_params'])) { | ||
$this->io->write('<comment>Some parameters are missing. Please provide them.</comment>')->shouldBeCalledTimes(1); | ||
} | ||
|
||
foreach ($testCase['requested_params'] as $param => $settings) { | ||
$this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $param, $settings['default']), $settings['default']) | ||
->willReturn($settings['input']) | ||
->shouldBeCalled(); | ||
} | ||
} | ||
|
||
public function provideParameterHandlingTestCases() | ||
{ | ||
$tests = array(); | ||
|
||
foreach (glob(__DIR__.'/fixtures/testcases/*/') as $folder) { | ||
$tests[] = array(basename($folder)); | ||
} | ||
|
||
return $tests; | ||
} | ||
} |
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 @@ | ||
not an array |
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,2 @@ | ||
parameters: | ||
foo: bar |
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 @@ | ||
another: The parameters key is missing |
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,3 @@ | ||
parameters: | ||
foo: bar | ||
boolean: false |
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,4 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false |
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,4 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false |
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,6 @@ | ||
title: Existing values are kept | ||
|
||
config: | ||
dist-file: parameters.dist.yml | ||
|
||
dist-file: parameters.dist.yml |
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,7 @@ | ||
config: | ||
foo: bar | ||
boolean: false | ||
another: ~ | ||
nested: | ||
foo: bar | ||
bar: baz |
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,8 @@ | ||
# This file is auto-generated during the composer install | ||
config: | ||
foo: existing_foo | ||
boolean: false | ||
another: ~ | ||
nested: | ||
foo: bar | ||
bar: baz |
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,8 @@ | ||
# This file is auto-generated during the composer install | ||
config: | ||
foo: existing_foo | ||
boolean: false | ||
another: null | ||
nested: | ||
foo: bar | ||
bar: baz |
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,5 @@ | ||
title: Using a custom parameter key | ||
|
||
config: | ||
parameter-key: config | ||
|
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,7 @@ | ||
parameters: | ||
foo: bar | ||
boolean: false | ||
another: ~ | ||
nested: | ||
foo: bar | ||
bar: baz |
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,8 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false | ||
another: ~ | ||
nested: | ||
foo: bar | ||
bar: baz |
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,8 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false | ||
another: null | ||
nested: | ||
foo: bar | ||
bar: baz |
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 @@ | ||
title: Existing values are kept |
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,2 @@ | ||
parameters: | ||
foo: bar |
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,2 @@ | ||
# This file is auto-generated during the composer install | ||
foobar: baz |
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,4 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: bar | ||
foobar: baz |
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 @@ | ||
title: Existing files without the parameters key are valid |
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,5 @@ | ||
parameters: | ||
foo: bar | ||
boolean: false | ||
|
||
extra_key: a new extra key |
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,4 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
another_key: foo |
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,6 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false | ||
another_key: foo | ||
extra_key: 'a new extra key' |
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 @@ | ||
title: Extra top level keys are preserved |
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,4 @@ | ||
parameters: | ||
foo: bar | ||
boolean: false | ||
another: ~ |
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,3 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo |
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,5 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
foo: existing_foo | ||
boolean: false | ||
another: '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,11 @@ | ||
title: Existing values are not asked interactively again | ||
|
||
interactive: true | ||
|
||
requested_params: | ||
boolean: | ||
default: 'false' | ||
input: 'false' | ||
another: | ||
default: 'null' | ||
input: '"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,4 @@ | ||
parameters: | ||
boolean: false | ||
another: test | ||
nested: nested |
10 changes: 10 additions & 0 deletions
10
Tests/fixtures/testcases/interaction_non_existent/expected.yml
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,10 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
boolean: true | ||
another: null | ||
nested: | ||
foo: bar | ||
bar: | ||
- foo | ||
- test | ||
- null |
14 changes: 14 additions & 0 deletions
14
Tests/fixtures/testcases/interaction_non_existent/setup.yml
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,14 @@ | ||
title: Missing keys are asked interactively | ||
|
||
interactive: true | ||
|
||
requested_params: | ||
boolean: | ||
default: 'false' | ||
input: 'true' | ||
nested: | ||
default: nested | ||
input: '{foo: bar, bar: [foo, test, null]}' | ||
another: | ||
default: test | ||
input: 'null' |
4 changes: 4 additions & 0 deletions
4
Tests/fixtures/testcases/interaction_with_environment/dist.yml
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,4 @@ | ||
parameters: | ||
boolean: false | ||
another: test | ||
nested: nested |
10 changes: 10 additions & 0 deletions
10
Tests/fixtures/testcases/interaction_with_environment/expected.yml
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,10 @@ | ||
# This file is auto-generated during the composer install | ||
parameters: | ||
boolean: true | ||
nested: | ||
foo: env_foo | ||
bar: | ||
- env | ||
- test | ||
- null | ||
another: null |
Oops, something went wrong.