Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Jul 20, 2013
1 parent ba63d23 commit 5729f97
Show file tree
Hide file tree
Showing 63 changed files with 539 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
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
190 changes: 190 additions & 0 deletions Tests/ScriptHandlerTest.php
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;
}
}
1 change: 1 addition & 0 deletions Tests/fixtures/invalid/invalid_existing_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an array
2 changes: 2 additions & 0 deletions Tests/fixtures/invalid/invalid_existing_values.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
foo: bar
1 change: 1 addition & 0 deletions Tests/fixtures/invalid/missing_top_level.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
another: The parameters key is missing
3 changes: 3 additions & 0 deletions Tests/fixtures/testcases/custom_dist_file/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
foo: bar
boolean: false
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/custom_dist_file/existing.yml
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
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/custom_dist_file/expected.yml
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
6 changes: 6 additions & 0 deletions Tests/fixtures/testcases/custom_dist_file/setup.yml
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
7 changes: 7 additions & 0 deletions Tests/fixtures/testcases/custom_key/dist.yml
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
8 changes: 8 additions & 0 deletions Tests/fixtures/testcases/custom_key/existing.yml
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
8 changes: 8 additions & 0 deletions Tests/fixtures/testcases/custom_key/expected.yml
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
5 changes: 5 additions & 0 deletions Tests/fixtures/testcases/custom_key/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
title: Using a custom parameter key

config:
parameter-key: config

7 changes: 7 additions & 0 deletions Tests/fixtures/testcases/existent/dist.yml
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
8 changes: 8 additions & 0 deletions Tests/fixtures/testcases/existent/existing.yml
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
8 changes: 8 additions & 0 deletions Tests/fixtures/testcases/existent/expected.yml
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
1 change: 1 addition & 0 deletions Tests/fixtures/testcases/existent/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Existing values are kept
2 changes: 2 additions & 0 deletions Tests/fixtures/testcases/existent_without_key/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
foo: bar
2 changes: 2 additions & 0 deletions Tests/fixtures/testcases/existent_without_key/existing.yml
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
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/existent_without_key/expected.yml
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
1 change: 1 addition & 0 deletions Tests/fixtures/testcases/existent_without_key/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Existing files without the parameters key are valid
5 changes: 5 additions & 0 deletions Tests/fixtures/testcases/extra_keys/dist.yml
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
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/extra_keys/existing.yml
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
6 changes: 6 additions & 0 deletions Tests/fixtures/testcases/extra_keys/expected.yml
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'
1 change: 1 addition & 0 deletions Tests/fixtures/testcases/extra_keys/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Extra top level keys are preserved
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/interaction_existent/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
foo: bar
boolean: false
another: ~
3 changes: 3 additions & 0 deletions Tests/fixtures/testcases/interaction_existent/existing.yml
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
5 changes: 5 additions & 0 deletions Tests/fixtures/testcases/interaction_existent/expected.yml
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'
11 changes: 11 additions & 0 deletions Tests/fixtures/testcases/interaction_existent/setup.yml
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"'
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/interaction_non_existent/dist.yml
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 Tests/fixtures/testcases/interaction_non_existent/expected.yml
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 Tests/fixtures/testcases/interaction_non_existent/setup.yml
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'
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 Tests/fixtures/testcases/interaction_with_environment/expected.yml
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
Loading

0 comments on commit 5729f97

Please sign in to comment.