From 6918d07e9a09f56d4fd511e91af49fffc142e422 Mon Sep 17 00:00:00 2001 From: Eugene Matvejev Date: Mon, 4 Jul 2016 12:53:38 +0100 Subject: [PATCH] RC10 improved composer handler, now it accepts as well absolute paths --- src/ScriptHandler.php | 14 ++++-- tests/phpunit/Processor/ProcessorTest.php | 3 +- tests/phpunit/ScriptHandlerTest.php | 47 +++++++++++++++++++ .../IntegrationTestSuite.php | 11 +++-- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/ScriptHandler.php b/src/ScriptHandler.php index fd4d575..29b8b7f 100644 --- a/src/ScriptHandler.php +++ b/src/ScriptHandler.php @@ -33,20 +33,26 @@ public static function generateCSS(Event $event) 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]}"); + foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) { + $processor->attachFiles( + static::resolvePath($inputSource, getcwd()), + static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd()) + ); } $formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; - $processor->processFiles($formatter); } $processor->saveOutput(); } + protected static function resolvePath($path, $prefix) + { + return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}"; + } + /** * @param array $config * diff --git a/tests/phpunit/Processor/ProcessorTest.php b/tests/phpunit/Processor/ProcessorTest.php index c5a53ac..16ad1c6 100644 --- a/tests/phpunit/Processor/ProcessorTest.php +++ b/tests/phpunit/Processor/ProcessorTest.php @@ -255,8 +255,9 @@ public function saveOutput() { $processor = new Processor($this->io); - $expectedOutputFile = $this->getRootDirectory() . '/../var/tests/' . __FUNCTION__ . '.css'; + $expectedOutputFile = $this->getCacheDirectory() . '/' . __FUNCTION__ . '.css'; @unlink($expectedOutputFile); + $processor->attachFiles( $this->getSharedFixturesDirectory() . '/scss', $expectedOutputFile diff --git a/tests/phpunit/ScriptHandlerTest.php b/tests/phpunit/ScriptHandlerTest.php index 95dccb2..83fdd80 100644 --- a/tests/phpunit/ScriptHandlerTest.php +++ b/tests/phpunit/ScriptHandlerTest.php @@ -2,6 +2,11 @@ namespace EM\CssCompiler\Tests\PHPUnit; +use Composer\Composer; +use Composer\Config; +use Composer\IO\IOInterface; +use Composer\Package\RootPackage; +use Composer\Script\Event; use EM\CssCompiler\ScriptHandler; use EM\CssCompiler\Tests\Environment\IntegrationTestSuite; @@ -148,4 +153,46 @@ private function validateOptions($config) { return $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[$config]]); } + + /*** *************************** INTEGRATION *************************** ***/ + /** + * @see ScriptHandler::generateCSS + * @test + */ + public function generateCSS() + { + $composer = (new Composer()); + /** @var RootPackage|\PHPUnit_Framework_MockObject_MockObject $rootPackage */ + $rootPackage = $this->getMockBuilder(RootPackage::class) + ->setConstructorArgs(['css-compiler', 'dev-master', 'dev']) + ->setMethods(['getExtra']) + ->getMock(); + /** @var IOInterface|\PHPUnit_Framework_MockObject_MockObject $io */ + $io = $this->getMockBuilder(IOInterface::class)->getMock(); + + $output = $this->getCacheDirectory() . '/' . __FUNCTION__ . '.css'; + @unlink($output); + + $extra = [ + 'css-compiler' => [ + [ + 'format' => 'compact', + 'input' => [ + $this->getSharedFixturesDirectory() . '/less' + ], + 'output' => $output + ] + ] + ]; + + $rootPackage->expects($this->once()) + ->method('getExtra') + ->willReturn($extra); + $composer->setPackage($rootPackage); + + $event = new Event('onInstall', $composer, $io); + + ScriptHandler::generateCSS($event); + $this->assertFileExists($output); + } } diff --git a/tests/shared-enviroment/IntegrationTestSuite.php b/tests/shared-enviroment/IntegrationTestSuite.php index 50f6a23..f09c354 100644 --- a/tests/shared-enviroment/IntegrationTestSuite.php +++ b/tests/shared-enviroment/IntegrationTestSuite.php @@ -30,7 +30,7 @@ protected function invokeMethod($object, $methodName, array $methodArguments = [ /** * @return string */ - public static function getRootDirectory() + protected function getRootDirectory() { return dirname(__DIR__); } @@ -38,7 +38,7 @@ public static function getRootDirectory() /** * @return string */ - public static function getSharedFixturesDirectory() + protected function getSharedFixturesDirectory() { return static::getRootDirectory() . '/shared-fixtures'; } @@ -50,8 +50,13 @@ public static function getSharedFixturesDirectory() * * @return string */ - public static function getSharedFixtureContent(string $filename) + protected function getSharedFixtureContent(string $filename) { return file_get_contents(static::getSharedFixturesDirectory() . "/$filename"); } + + protected function getCacheDirectory() + { + return dirname($this->getRootDirectory()) . '/var/cache/tests'; + } }