init without trunk

This commit is contained in:
Kevin Adametz 2020-07-09 12:49:32 +02:00
parent ed24ac4994
commit bb809e7233
14652 changed files with 177862 additions and 94817 deletions

View file

@ -0,0 +1,179 @@
<?php
namespace Incenteev\ParameterHandler\Tests;
use Incenteev\ParameterHandler\Processor;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
class ProcessorTest extends TestCase
{
private $io;
private $environmentBackup = array();
/**
* @var Processor
*/
private $processor;
protected function setUp()
{
parent::setUp();
$this->io = $this->prophesize('Composer\IO\IOInterface');
$this->processor = new Processor($this->io->reveal());
}
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 $config, $exceptionMessage)
{
chdir(__DIR__);
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $exceptionMessage);
}
$this->processor->processFile($config);
}
public function provideInvalidConfiguration()
{
return array(
'no file' => array(
array(),
'The extra.incenteev-parameters.file setting is required to use this script handler.',
),
'missing default dist file' => array(
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(
'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(
'file' => 'fixtures/invalid/missing_top_level.yml',
),
'The top-level key parameters is missing.',
),
'invalid values in the existing file' => array(
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(file_get_contents($dataDir.'/setup.yml'))
);
$workingDir = sys_get_temp_dir() . '/incenteev_parameter_handler';
$exists = $this->initializeTestCase($testCase, $dataDir, $workingDir);
$message = sprintf('<info>%s the "%s" file</info>', $exists ? 'Updating' : 'Creating', $testCase['config']['file']);
$this->io->write($message)->shouldBeCalled();
$this->setInteractionExpectations($testCase);
$this->processor->processFile($testCase['config']);
$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;
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Incenteev\ParameterHandler\Tests;
use Incenteev\ParameterHandler\ScriptHandler;
use PHPUnit\Framework\TestCase;
class ScriptHandlerTest extends TestCase
{
private $event;
private $io;
private $package;
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);
}
/**
* @dataProvider provideInvalidConfiguration
*/
public function testInvalidConfiguration(array $extras, $exceptionMessage)
{
$this->package->getExtra()->willReturn($extras);
chdir(__DIR__);
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage($exceptionMessage);
} else {
$this->setExpectedException('InvalidArgumentException', $exceptionMessage);
}
ScriptHandler::buildParameters($this->event->reveal());
}
public function provideInvalidConfiguration()
{
return array(
'no extra' => array(
array(),
'The parameter handler needs to be configured through the extra.incenteev-parameters setting.',
),
'invalid type' => array(
array('incenteev-parameters' => 'not an array'),
'The extra.incenteev-parameters setting must be an array or a configuration object.',
),
'invalid type for multiple file' => array(
array('incenteev-parameters' => array('not an array')),
'The extra.incenteev-parameters setting must be an array of configuration objects.',
),
'no file' => array(
array('incenteev-parameters' => array()),
'The extra.incenteev-parameters.file setting is required to use this script handler.',
),
);
}
}

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1 @@
another: The parameters key is missing

View file

@ -0,0 +1,3 @@
parameters:
foo: bar
boolean: false

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false

View file

@ -0,0 +1,6 @@
title: Existing values are kept
config:
dist-file: parameters.dist.yml
dist-file: parameters.dist.yml

View file

@ -0,0 +1,7 @@
config:
foo: bar
boolean: false
another: ~
nested:
foo: bar
bar: baz

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
title: Using a custom parameter key
config:
parameter-key: config

View file

@ -0,0 +1,7 @@
parameters:
foo: bar
boolean: false
another: ~
nested:
foo: bar
bar: baz

View file

@ -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

View file

@ -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

View file

@ -0,0 +1 @@
title: Existing values are kept

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
parameters:
foo: bar

View file

@ -0,0 +1 @@
title: Existing empty files are valid (Capifony compatibility)

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1,2 @@
# This file is auto-generated during the composer install
foobar: baz

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: bar
foobar: baz

View file

@ -0,0 +1 @@
title: Existing files without the parameters key are valid

View file

@ -0,0 +1,5 @@
parameters:
foo: bar
boolean: false
extra_key: a new extra key

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
another_key: foo

View file

@ -0,0 +1,6 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false
extra_key: 'a new extra key'
another_key: foo

View file

@ -0,0 +1 @@
title: Extra top level keys are preserved

View file

@ -0,0 +1,4 @@
parameters:
foo: bar
boolean: false
another: ~

View file

@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo

View file

@ -0,0 +1,5 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false
another: 'null'

View file

@ -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"'

View file

@ -0,0 +1,4 @@
parameters:
boolean: false
another: test
nested: nested

View file

@ -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

View file

@ -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'

View file

@ -0,0 +1,4 @@
parameters:
boolean: false
another: test
nested: nested

View file

@ -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

View file

@ -0,0 +1,17 @@
title: Values provided by the environment are not asked interactively
config:
env-map:
boolean: IC_TEST_BOOL
nested: IC_TEST_NESTED
environment:
IC_TEST_BOOL: 'true'
IC_TEST_NESTED: '{foo: env_foo, bar: [env, test, null]}'
interactive: true
requested_params:
another:
default: test
input: 'null'

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
outdated: outdated_param

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
outdated: outdated_param

View file

@ -0,0 +1,4 @@
title: Outdated keys can be kept in the parameters file
config:
keep-outdated: true

View file

@ -0,0 +1,7 @@
parameters:
foo: bar
boolean: false
another: ~
nested:
foo: bar
bar: baz

View file

@ -0,0 +1,8 @@
# This file is auto-generated during the composer install
parameters:
foo: bar
boolean: false
another: null
nested:
foo: bar
bar: baz

View file

@ -0,0 +1 @@
title: Non existent files are created

View file

@ -0,0 +1,7 @@
parameters:
foo: bar
boolean: false
another: ~
nested:
foo: bar
bar: baz

View file

@ -0,0 +1,11 @@
# This file is auto-generated during the composer install
parameters:
foo: foobar
boolean: true
another: null
nested:
foo: env_foo
bar:
- env
- test
- null

View file

@ -0,0 +1,13 @@
title: Environment variables are used over dist file defaults
config:
env-map:
boolean: IC_TEST_BOOL
foo: IC_TEST_FOO
nested: IC_TEST_NESTED
another: IC_TEST_NOT_SET
environment:
IC_TEST_BOOL: 'true'
IC_TEST_FOO: 'foobar'
IC_TEST_NESTED: '{foo: env_foo, bar: [env, test, null]}'

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
outdated: outdated_param

View file

@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo

View file

@ -0,0 +1 @@
title: Outdated keys are removed from the parameters file

View file

@ -0,0 +1,6 @@
parameters:
new: bar
new2: new2
new3: test
new4: test4
new5: test5

View file

@ -0,0 +1,6 @@
# This file is auto-generated during the composer install
parameters:
old: old_value
new2: foo
old2: bar
old4: old4

View file

@ -0,0 +1,7 @@
# This file is auto-generated during the composer install
parameters:
new: old_value
new2: foo
new3: test
new4: old4
new5: old4

View file

@ -0,0 +1,9 @@
title: Key can be renamed and the value is reused
config:
rename-map:
new: old
new2: old2
new3: old3
new4: old4
new5: new4 # Cascade renaming

View file

@ -0,0 +1,3 @@
parameters:
new: bar
new2: new2

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
old: old_value
old2: old_value2

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
new: new_env_value
new2: old_value2

View file

@ -0,0 +1,11 @@
title: Environment variables win over renamed keys
config:
rename-map:
new: old
new2: old2
env-map:
new: IC_TEST_NEW
environment:
IC_TEST_NEW: 'new_env_value'

View file

@ -0,0 +1,3 @@
parameters:
foo: bar
boolean: false

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false

View file

@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
boolean: false

View file

@ -0,0 +1,6 @@
title: Files can be located in subfolders
config:
file: 'app/parameters.yml'
dist-file: 'app/parameters.yml.dist'

View file

@ -0,0 +1,2 @@
parameters:
foo: bar

View file

@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
parameters:
foo: bar

View file

@ -0,0 +1,7 @@
title: Files can be located in different folders than the dist and the folder is created
config:
file: 'app/parameters.yml'
dist-file: 'dist/parameters.yml'
dist-file: 'dist/parameters.yml'