Skip to content

RC11 improve composer handler #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 29 additions & 69 deletions src/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class ScriptHandler
const OPTION_KEY_FORMATTER = 'format';
const DEFAULT_OPTION_FORMATTER = 'compact';
protected static $mandatoryOptions = [
self::OPTION_KEY_INPUT,
self::OPTION_KEY_OUTPUT
self::OPTION_KEY_INPUT => 'array',
self::OPTION_KEY_OUTPUT => 'string'
];

/**
* @api
*
* @param Event $event
*
* @throws \InvalidArgumentException
Expand All @@ -34,20 +36,26 @@ public static function generateCSS(Event $event)

$processor = new Processor($event->getIO());

foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) {
foreach ($extra[static::CONFIG_MAIN_KEY] as $options) {
foreach ($options[static::OPTION_KEY_INPUT] as $inputSource) {
$processor->attachFiles(
static::resolvePath($inputSource, getcwd()),
static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd())
static::resolvePath($options[static::OPTION_KEY_OUTPUT], getcwd())
);
}

$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
$formatter = array_key_exists(static::OPTION_KEY_FORMATTER, $options) ? $options[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
$processor->processFiles($formatter);
}
$processor->saveOutput();
}

/**
* @param string $path
* @param string $prefix
*
* @return string
*/
protected static function resolvePath($path, $prefix)
{
return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}";
Expand All @@ -56,92 +64,44 @@ protected static function resolvePath($path, $prefix)
/**
* @param array $config
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateConfiguration(array $config)
{
if (empty($config[static::CONFIG_MAIN_KEY])) {
if (!array_key_exists(static::CONFIG_MAIN_KEY, $config)) {
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
}

if (!is_array($config[static::CONFIG_MAIN_KEY])) {
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
throw new \InvalidArgumentException('the extra.' . static::CONFIG_MAIN_KEY . ' setting must be an array of objects');
}

return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
}

/**
* @param array $config
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateOptions(array $config)
{
foreach ($config as $option) {
if (!is_array($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $options) {
if (!is_array($options)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index] should be an array");
}

static::validateMandatoryOptions($option);
static::validateMandatoryOptions($options, $index);
}

return true;
}

/**
* @param array $config
* @param array $options
* @param int $index
*
* @return bool
* @throws \InvalidArgumentException
*/
protected static function validateMandatoryOptions(array $config)
protected static function validateMandatoryOptions(array $options, $index)
{
foreach (static::$mandatoryOptions as $option) {
if (empty($config[$option])) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
foreach (static::$mandatoryOptions as $optionIndex => $type) {
if (!array_key_exists($optionIndex, $options)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} is required!");
}

switch ($option) {
case static::OPTION_KEY_INPUT:
static::validateIsArray($config[$option]);
break;
case static::OPTION_KEY_OUTPUT:
static::validateIsString($config[$option]);
break;
$callable = "is_{$type}";
if (!$callable($options[$optionIndex])) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} should be {$type}!");
}
}

return true;
}

/**
* @param array $option
*
* @return bool
*/
protected static function validateIsArray($option)
{
if (!is_array($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
}

return true;
}

/**
* @param string $option
*
* @return bool
*/
protected static function validateIsString($option)
{
if (!is_string($option)) {
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
}

return true;
}
}
49 changes: 31 additions & 18 deletions tests/phpunit/ScriptHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,88 +72,101 @@ public function validateConfigurationOnValid()
]
];

$this->assertTrue($this->validateConfiguration($args));
$this->assertNull($this->validateConfiguration($args));
}

/**
* @see ScriptHandler::validateConfiguration
*
* @param $args
*
* @return bool
*/
private function validateConfiguration($args)
{
return $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [$args]);
}
/*** *************************** OPTIONS VALIDATION *************************** ***/
/**
* @see ScriptHandler::validateOptions
* @see ScriptHandler::validateMandatoryOptions
* @test
*
* @expectedException \InvalidArgumentException
*/
public function validateOptionsExpectedExceptionOnMissingInput()
{
$this->validateOptions([[ScriptHandler::OPTION_KEY_OUTPUT => 'output']]);
$this->validateMandatoryOptions([[ScriptHandler::OPTION_KEY_OUTPUT => 'output']]);
}

/**
* @see ScriptHandler::validateOptions
* @see ScriptHandler::validateMandatoryOptions
* @test
*
* @expectedException \InvalidArgumentException
*/
public function validateOptionsExpectedExceptionOnMissingOutput()
{
$this->validateOptions([ScriptHandler::OPTION_KEY_INPUT => 'input']);
$this->validateMandatoryOptions([ScriptHandler::OPTION_KEY_INPUT => 'input']);
}

/**
* @see ScriptHandler::validateOptions
* @see ScriptHandler::validateMandatoryOptions
* @test
*
* @expectedException \InvalidArgumentException
*/
public function validateOptionsExpectedExceptionOnInputNotArray()
{
$this->validateOptions([
$this->validateMandatoryOptions([
ScriptHandler::OPTION_KEY_INPUT => 'string',
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
]);
}

/**
* @see ScriptHandler::validateOptions
* @see ScriptHandler::validateMandatoryOptions
* @test
*
* @expectedException \InvalidArgumentException
*/
public function validateOptionsExpectedExceptionOnOutputNotString()
{
$this->validateOptions([
$this->validateMandatoryOptions([
ScriptHandler::OPTION_KEY_INPUT => ['string'],
ScriptHandler::OPTION_KEY_OUTPUT => ['string']
]);
}

/**
* @see ScriptHandler::validateOptions
* @see ScriptHandler::validateMandatoryOptions
* @test
*
* @group tester
*/
public function validateOptionsOnValid()
{
$this->assertTrue(
$this->validateOptions([
ScriptHandler::OPTION_KEY_INPUT => ['string'],
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
])
$this->assertNull(
$this->validateMandatoryOptions(
[
ScriptHandler::OPTION_KEY_INPUT => ['string'],
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
]
)
);
}

/**
* @see ScriptHandler::validateMandatoryOptions
*
* @param array $config
*
* @return bool
*/
private function validateOptions($config)
private function validateMandatoryOptions($config)
{
return $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[$config]]);
return $this->invokeMethod(new ScriptHandler(), 'validateMandatoryOptions', [$config, 1]);
}

/*** *************************** INTEGRATION *************************** ***/
/**
* @see ScriptHandler::generateCSS
Expand Down