Skip to content

Commit 6918d07

Browse files
RC10 improved composer handler, now it accepts as well absolute paths
1 parent bfe139c commit 6918d07

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

src/ScriptHandler.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,26 @@ public static function generateCSS(Event $event)
3333
static::validateConfiguration($extra);
3434

3535
$processor = new Processor($event->getIO());
36-
$currentDirectory = getcwd();
3736

3837
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
39-
foreach ($config[static::OPTION_KEY_INPUT] as $value) {
40-
$processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}");
38+
foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) {
39+
$processor->attachFiles(
40+
static::resolvePath($inputSource, getcwd()),
41+
static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd())
42+
);
4143
}
4244

4345
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
44-
4546
$processor->processFiles($formatter);
4647
}
4748
$processor->saveOutput();
4849
}
4950

51+
protected static function resolvePath($path, $prefix)
52+
{
53+
return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}";
54+
}
55+
5056
/**
5157
* @param array $config
5258
*

tests/phpunit/Processor/ProcessorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,9 @@ public function saveOutput()
255255
{
256256
$processor = new Processor($this->io);
257257

258-
$expectedOutputFile = $this->getRootDirectory() . '/../var/tests/' . __FUNCTION__ . '.css';
258+
$expectedOutputFile = $this->getCacheDirectory() . '/' . __FUNCTION__ . '.css';
259259
@unlink($expectedOutputFile);
260+
260261
$processor->attachFiles(
261262
$this->getSharedFixturesDirectory() . '/scss',
262263
$expectedOutputFile

tests/phpunit/ScriptHandlerTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace EM\CssCompiler\Tests\PHPUnit;
44

5+
use Composer\Composer;
6+
use Composer\Config;
7+
use Composer\IO\IOInterface;
8+
use Composer\Package\RootPackage;
9+
use Composer\Script\Event;
510
use EM\CssCompiler\ScriptHandler;
611
use EM\CssCompiler\Tests\Environment\IntegrationTestSuite;
712

@@ -148,4 +153,46 @@ private function validateOptions($config)
148153
{
149154
return $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[$config]]);
150155
}
156+
157+
/*** *************************** INTEGRATION *************************** ***/
158+
/**
159+
* @see ScriptHandler::generateCSS
160+
* @test
161+
*/
162+
public function generateCSS()
163+
{
164+
$composer = (new Composer());
165+
/** @var RootPackage|\PHPUnit_Framework_MockObject_MockObject $rootPackage */
166+
$rootPackage = $this->getMockBuilder(RootPackage::class)
167+
->setConstructorArgs(['css-compiler', 'dev-master', 'dev'])
168+
->setMethods(['getExtra'])
169+
->getMock();
170+
/** @var IOInterface|\PHPUnit_Framework_MockObject_MockObject $io */
171+
$io = $this->getMockBuilder(IOInterface::class)->getMock();
172+
173+
$output = $this->getCacheDirectory() . '/' . __FUNCTION__ . '.css';
174+
@unlink($output);
175+
176+
$extra = [
177+
'css-compiler' => [
178+
[
179+
'format' => 'compact',
180+
'input' => [
181+
$this->getSharedFixturesDirectory() . '/less'
182+
],
183+
'output' => $output
184+
]
185+
]
186+
];
187+
188+
$rootPackage->expects($this->once())
189+
->method('getExtra')
190+
->willReturn($extra);
191+
$composer->setPackage($rootPackage);
192+
193+
$event = new Event('onInstall', $composer, $io);
194+
195+
ScriptHandler::generateCSS($event);
196+
$this->assertFileExists($output);
197+
}
151198
}

tests/shared-enviroment/IntegrationTestSuite.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ protected function invokeMethod($object, $methodName, array $methodArguments = [
3030
/**
3131
* @return string
3232
*/
33-
public static function getRootDirectory()
33+
protected function getRootDirectory()
3434
{
3535
return dirname(__DIR__);
3636
}
3737

3838
/**
3939
* @return string
4040
*/
41-
public static function getSharedFixturesDirectory()
41+
protected function getSharedFixturesDirectory()
4242
{
4343
return static::getRootDirectory() . '/shared-fixtures';
4444
}
@@ -50,8 +50,13 @@ public static function getSharedFixturesDirectory()
5050
*
5151
* @return string
5252
*/
53-
public static function getSharedFixtureContent(string $filename)
53+
protected function getSharedFixtureContent(string $filename)
5454
{
5555
return file_get_contents(static::getSharedFixturesDirectory() . "/$filename");
5656
}
57+
58+
protected function getCacheDirectory()
59+
{
60+
return dirname($this->getRootDirectory()) . '/var/cache/tests';
61+
}
5762
}

0 commit comments

Comments
 (0)