diff --git a/ScriptHandler.php b/ScriptHandler.php index f2988ad..519c860 100644 --- a/ScriptHandler.php +++ b/ScriptHandler.php @@ -3,60 +3,15 @@ namespace EM\CssCompiler\Handler; use Composer\Script\Event; -use EM\CssCompiler\Processor\Processor; +use EM\CssCompiler\ScriptHandler as Handler; -class ScriptHandler +/** + * @deprecated + */ +class ScriptHandler extends Handler { - const CONFIG_MAIN_KEY = 'css-compiler'; - const CONFIG_INPUT_KEY = 'input'; - const CONFIG_OUTPUT_KEY = 'output'; - const CONFIG_FORMATTER_KEY = 'format'; - const CONFIG_DEFAULT_FORMATTER = 'compact'; - public static function compileCSS(Event $event) { - $extra = $event->getComposer()->getPackage()->getExtra(); - - static::validateConfiguration($extra); - - $processor = new Processor($event->getIO()); - $currentDirectory = getcwd(); - foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { - foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) { - $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}"); - } - - $formatter = isset($config[static::CONFIG_FORMATTER_KEY]) - ? $config[static::CONFIG_FORMATTER_KEY] - : static::CONFIG_DEFAULT_FORMATTER; - - $processor->processFiles($formatter); - } - - $processor->saveOutput(); - } - - protected static function validateConfiguration(array $config) - { - if (!isset($config[static::CONFIG_MAIN_KEY])) { - throw new \InvalidArgumentException('the parameter handler 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 a configuration objects.'); - } - - foreach ($config[static::CONFIG_MAIN_KEY] as $el) { - if (!is_array($el)) { - throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.'); - } - - if (!isset($el[static::CONFIG_INPUT_KEY])) { - throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!'); - } - if (!isset($el[static::CONFIG_OUTPUT_KEY])) { - throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!'); - } - } + static::generateCSS($event); } } diff --git a/src/Container/File.php b/src/Container/FileContainer.php similarity index 99% rename from src/Container/File.php rename to src/Container/FileContainer.php index a63f9f5..36662a4 100644 --- a/src/Container/File.php +++ b/src/Container/FileContainer.php @@ -7,7 +7,7 @@ /** * @since 0.1 */ -class File +class FileContainer { const TYPE_UNKNOWN = 'unknown'; const TYPE_COMPASS = 'compass'; diff --git a/src/Processor/Processor.php b/src/Processor/Processor.php index d8b7e56..77521f1 100644 --- a/src/Processor/Processor.php +++ b/src/Processor/Processor.php @@ -3,7 +3,7 @@ namespace EM\CssCompiler\Processor; use Composer\IO\IOInterface; -use EM\CssCompiler\Container\File; +use EM\CssCompiler\Container\FileContainer; use EM\CssCompiler\Exception\CompilerException; use Leafo\ScssPhp\Compiler as SASSCompiler; use lessc as LESSCompiler; @@ -31,7 +31,7 @@ class Processor */ private $io; /** - * @var File[] + * @var FileContainer[] */ private $files = []; /** @@ -73,14 +73,14 @@ public function attachFiles($inputPath, $outputPath) $this->attachFiles("$inputPath/$file", $outputPath); } } else if (is_file($inputPath)) { - $this->files[] = new File($inputPath, $outputPath); + $this->files[] = new FileContainer($inputPath, $outputPath); } else { throw new \Exception("file doesn't exists"); } } /** - * @return File[] + * @return FileContainer[] */ public function getFiles() { @@ -149,14 +149,14 @@ public function processFiles($formatter) * @return File * @throws CompilerException */ - public function processFile(File $file) + public function processFile(FileContainer $file) { switch ($file->getType()) { - case File::TYPE_COMPASS: - case File::TYPE_SCSS: - case File::TYPE_SASS: + case FileContainer::TYPE_COMPASS: + case FileContainer::TYPE_SCSS: + case FileContainer::TYPE_SASS: return $file->setParsedContent($this->sass->compile($file->getSourceContent())); - case File::TYPE_LESS: + case FileContainer::TYPE_LESS: return $file->setParsedContent($this->less->compile($file->getSourceContent())); } diff --git a/src/ScriptHandler.php b/src/ScriptHandler.php new file mode 100644 index 0000000..3f204bb --- /dev/null +++ b/src/ScriptHandler.php @@ -0,0 +1,94 @@ +getComposer()->getPackage()->getExtra(); + static::validateConfiguration($extra); + + $processor = new Processor($event->getIO()); + $currentDirectory = getcwd(); + + foreach ($extra[static::CONFIG_MAIN_KEY] as $config) { + foreach ($config[static::OPTION_KEY_INPUT] as $value) { + $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}"); + } + + $formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; + + $processor->processFiles($formatter); + } + $processor->saveOutput(); + } + + /** + * @param array $config + * + * @return bool + * @throws \InvalidArgumentException + */ + protected static function validateConfiguration(array $config) + { + if (empty($config[static::CONFIG_MAIN_KEY])) { + 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'); + } + + foreach ($config[static::CONFIG_MAIN_KEY] as $index => $el) { + if (!is_array($el)) { + throw new \InvalidArgumentException("the extra.css-compiler[{$index}]." . static::OPTION_KEY_INPUT . ' array'); + } + + static::validateOptions($el); + } + + return true; + } + + /** + * @param array $config + * + * @return bool + * @throws \InvalidArgumentException + */ + protected static function validateOptions(array $config) + { + foreach (static::$mandatoryOptions as $option) { + if (empty($config[$option])) { + throw new \InvalidArgumentException("The extra.css-compiler[].{$option} required!"); + } + } + if (!is_array($config[static::OPTION_KEY_INPUT])) { + throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_INPUT . ' should be array!'); + } + if (!is_string($config[static::OPTION_KEY_OUTPUT])) { + throw new \InvalidArgumentException('The extra.css-compiler[].' . static::OPTION_KEY_OUTPUT . ' should string!'); + } + + return true; + } +} diff --git a/tests/phpunit/Container/File.php b/tests/phpunit/Container/File.php new file mode 100644 index 0000000..0735415 --- /dev/null +++ b/tests/phpunit/Container/File.php @@ -0,0 +1,7 @@ +setSourceContentFromSourcePath(); (new Processor($this->io))->processFile($file); @@ -80,9 +80,9 @@ public function processFileSASS() */ public function processFileExpectedException() { - $file = (new File(static::getSharedFixturesDirectory() . '/compass/sass/', '')) + $file = (new FileContainer(static::getSharedFixturesDirectory() . '/compass/sass/', '')) ->setSourceContentFromSourcePath() - ->setType(File::TYPE_UNKNOWN); + ->setType(FileContainer::TYPE_UNKNOWN); (new Processor($this->io))->processFile($file); } diff --git a/tests/phpunit/ScriptHandlerTest.php b/tests/phpunit/ScriptHandlerTest.php new file mode 100644 index 0000000..c573537 --- /dev/null +++ b/tests/phpunit/ScriptHandlerTest.php @@ -0,0 +1,134 @@ +invokeMethod(new ScriptHandler(), 'validateConfiguration', [[]]); + } + + /** + * @see ScriptHandler::validateConfiguration + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateConfigurationExpectedExceptionOnNotArray() + { + $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [[ScriptHandler::CONFIG_MAIN_KEY]]); + } + + /** + * @see ScriptHandler::validateConfiguration + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateConfigurationExpectedExceptionOptionIsNotArray() + { + $arr = [ + ScriptHandler::CONFIG_MAIN_KEY => [ + 'string' + ] + ]; + $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [$arr]); + } + + /** + * @see ScriptHandler::validateConfiguration + * @test + */ + function validateConfigurationOnValid() + { + $arr = [ + ScriptHandler::CONFIG_MAIN_KEY => [ + [ + ScriptHandler::OPTION_KEY_INPUT => ['string'], + ScriptHandler::OPTION_KEY_OUTPUT => 'string' + ] + ] + ]; + $result = $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [$arr]); + $this->assertTrue($result); + } + + /*** *************************** OPTIONS VALIDATION *************************** ***/ + /** + * @see ScriptHandler::validateOptions + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateOptionsExpectedExceptionOnMissingInput() + { + $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ScriptHandler::OPTION_KEY_OUTPUT]]); + } + + /** + * @see ScriptHandler::validateOptions + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateOptionsExpectedExceptionOnMissingOutput() + { + $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ScriptHandler::OPTION_KEY_INPUT]]); + } + + /** + * @see ScriptHandler::validateOptions + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateOptionsExpectedExceptionOnInputNotArray() + { + $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ + ScriptHandler::OPTION_KEY_INPUT => 'string', + ScriptHandler::OPTION_KEY_OUTPUT => 'string' + ]]); + } + + /** + * @see ScriptHandler::validateOptions + * @test + * + * @expectedException \InvalidArgumentException + */ + function validateOptionsExpectedExceptionOnOutputNotString() + { + $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ + ScriptHandler::OPTION_KEY_INPUT => ['string'], + ScriptHandler::OPTION_KEY_OUTPUT => ['string'] + ]]); + } + + /** + * @see ScriptHandler::validateOptions + * @test + */ + function validateOptionsOnValid() + { + $result = $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[ + ScriptHandler::OPTION_KEY_INPUT => ['string'], + ScriptHandler::OPTION_KEY_OUTPUT => 'string' + ]]); + + $this->assertTrue($result); + } +}