Skip to content

Commit 0583d11

Browse files
RC11 improve composer handler
1 parent 6918d07 commit 0583d11

File tree

2 files changed

+52
-73
lines changed

2 files changed

+52
-73
lines changed

src/ScriptHandler.php

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static function generateCSS(Event $event)
4848
$processor->saveOutput();
4949
}
5050

51+
/**
52+
* @param string $path
53+
* @param string $prefix
54+
*
55+
* @return string
56+
*/
5157
protected static function resolvePath($path, $prefix)
5258
{
5359
return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}";
@@ -69,79 +75,39 @@ protected static function validateConfiguration(array $config)
6975
throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
7076
}
7177

72-
return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
73-
}
74-
75-
/**
76-
* @param array $config
77-
*
78-
* @return bool
79-
* @throws \InvalidArgumentException
80-
*/
81-
protected static function validateOptions(array $config)
82-
{
83-
foreach ($config as $option) {
84-
if (!is_array($option)) {
78+
foreach ($config[static::CONFIG_MAIN_KEY] as $options) {
79+
if (!is_array($options)) {
8580
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
8681
}
8782

88-
static::validateMandatoryOptions($option);
83+
static::validateMandatoryOptions($options);
8984
}
90-
91-
return true;
9285
}
9386

9487
/**
95-
* @param array $config
88+
* @param array $options
9689
*
97-
* @return bool
9890
* @throws \InvalidArgumentException
9991
*/
100-
protected static function validateMandatoryOptions(array $config)
92+
protected static function validateMandatoryOptions(array $options)
10193
{
102-
foreach (static::$mandatoryOptions as $option) {
103-
if (empty($config[$option])) {
104-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
94+
foreach (static::$mandatoryOptions as $optionIndex) {
95+
if (!isset($options[$optionIndex])) {
96+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} is required!");
10597
}
10698

107-
switch ($option) {
99+
switch ($optionIndex) {
108100
case static::OPTION_KEY_INPUT:
109-
static::validateIsArray($config[$option]);
101+
if (!is_array($options[$optionIndex])) {
102+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should be array!");
103+
}
110104
break;
111105
case static::OPTION_KEY_OUTPUT:
112-
static::validateIsString($config[$option]);
106+
if (!is_string($options[$optionIndex])) {
107+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$optionIndex} should string!");
108+
}
113109
break;
114110
}
115111
}
116-
117-
return true;
118-
}
119-
120-
/**
121-
* @param array $option
122-
*
123-
* @return bool
124-
*/
125-
protected static function validateIsArray($option)
126-
{
127-
if (!is_array($option)) {
128-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
129-
}
130-
131-
return true;
132-
}
133-
134-
/**
135-
* @param string $option
136-
*
137-
* @return bool
138-
*/
139-
protected static function validateIsString($option)
140-
{
141-
if (!is_string($option)) {
142-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
143-
}
144-
145-
return true;
146112
}
147113
}

tests/phpunit/ScriptHandlerTest.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,88 +72,101 @@ public function validateConfigurationOnValid()
7272
]
7373
];
7474

75-
$this->assertTrue($this->validateConfiguration($args));
75+
$this->assertNull($this->validateConfiguration($args));
7676
}
7777

78+
/**
79+
* @see ScriptHandler::validateConfiguration
80+
*
81+
* @param $args
82+
*
83+
* @return bool
84+
*/
7885
private function validateConfiguration($args)
7986
{
8087
return $this->invokeMethod(new ScriptHandler(), 'validateConfiguration', [$args]);
8188
}
8289
/*** *************************** OPTIONS VALIDATION *************************** ***/
8390
/**
84-
* @see ScriptHandler::validateOptions
91+
* @see ScriptHandler::validateMandatoryOptions
8592
* @test
8693
*
8794
* @expectedException \InvalidArgumentException
8895
*/
8996
public function validateOptionsExpectedExceptionOnMissingInput()
9097
{
91-
$this->validateOptions([[ScriptHandler::OPTION_KEY_OUTPUT => 'output']]);
98+
$this->validateMandatoryOptions([[ScriptHandler::OPTION_KEY_OUTPUT => 'output']]);
9299
}
93100

94101
/**
95-
* @see ScriptHandler::validateOptions
102+
* @see ScriptHandler::validateMandatoryOptions
96103
* @test
97104
*
98105
* @expectedException \InvalidArgumentException
99106
*/
100107
public function validateOptionsExpectedExceptionOnMissingOutput()
101108
{
102-
$this->validateOptions([ScriptHandler::OPTION_KEY_INPUT => 'input']);
109+
$this->validateMandatoryOptions([ScriptHandler::OPTION_KEY_INPUT => 'input']);
103110
}
104111

105112
/**
106-
* @see ScriptHandler::validateOptions
113+
* @see ScriptHandler::validateMandatoryOptions
107114
* @test
108115
*
109116
* @expectedException \InvalidArgumentException
110117
*/
111118
public function validateOptionsExpectedExceptionOnInputNotArray()
112119
{
113-
$this->validateOptions([
120+
$this->validateMandatoryOptions([
114121
ScriptHandler::OPTION_KEY_INPUT => 'string',
115122
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
116123
]);
117124
}
118125

119126
/**
120-
* @see ScriptHandler::validateOptions
127+
* @see ScriptHandler::validateMandatoryOptions
121128
* @test
122129
*
123130
* @expectedException \InvalidArgumentException
124131
*/
125132
public function validateOptionsExpectedExceptionOnOutputNotString()
126133
{
127-
$this->validateOptions([
134+
$this->validateMandatoryOptions([
128135
ScriptHandler::OPTION_KEY_INPUT => ['string'],
129136
ScriptHandler::OPTION_KEY_OUTPUT => ['string']
130137
]);
131138
}
132139

133140
/**
134-
* @see ScriptHandler::validateOptions
141+
* @see ScriptHandler::validateMandatoryOptions
135142
* @test
143+
*
144+
* @group tester
136145
*/
137146
public function validateOptionsOnValid()
138147
{
139-
$this->assertTrue(
140-
$this->validateOptions([
141-
ScriptHandler::OPTION_KEY_INPUT => ['string'],
142-
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
143-
])
148+
$this->assertNull(
149+
$this->validateMandatoryOptions(
150+
[
151+
ScriptHandler::OPTION_KEY_INPUT => ['string'],
152+
ScriptHandler::OPTION_KEY_OUTPUT => 'string'
153+
]
154+
)
144155
);
145156
}
146157

147158
/**
159+
* @see ScriptHandler::validateMandatoryOptions
160+
*
148161
* @param array $config
149162
*
150163
* @return bool
151164
*/
152-
private function validateOptions($config)
165+
private function validateMandatoryOptions($config)
153166
{
154-
return $this->invokeMethod(new ScriptHandler(), 'validateOptions', [[$config]]);
167+
return $this->invokeMethod(new ScriptHandler(), 'validateMandatoryOptions', [$config]);
155168
}
156-
169+
157170
/*** *************************** INTEGRATION *************************** ***/
158171
/**
159172
* @see ScriptHandler::generateCSS

0 commit comments

Comments
 (0)