Skip to content

Commit 384e3a8

Browse files
Merge pull request #27 from learn-symfony/master
sync with main repo
2 parents 2b2b53d + 1dd9507 commit 384e3a8

File tree

2 files changed

+60
-87
lines changed

2 files changed

+60
-87
lines changed

src/ScriptHandler.php

+29-69
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class ScriptHandler
1818
const OPTION_KEY_FORMATTER = 'format';
1919
const DEFAULT_OPTION_FORMATTER = 'compact';
2020
protected static $mandatoryOptions = [
21-
self::OPTION_KEY_INPUT,
22-
self::OPTION_KEY_OUTPUT
21+
self::OPTION_KEY_INPUT => 'array',
22+
self::OPTION_KEY_OUTPUT => 'string'
2323
];
2424

2525
/**
26+
* @api
27+
*
2628
* @param Event $event
2729
*
2830
* @throws \InvalidArgumentException
@@ -34,20 +36,26 @@ public static function generateCSS(Event $event)
3436

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

37-
foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
38-
foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) {
39+
foreach ($extra[static::CONFIG_MAIN_KEY] as $options) {
40+
foreach ($options[static::OPTION_KEY_INPUT] as $inputSource) {
3941
$processor->attachFiles(
4042
static::resolvePath($inputSource, getcwd()),
41-
static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd())
43+
static::resolvePath($options[static::OPTION_KEY_OUTPUT], getcwd())
4244
);
4345
}
4446

45-
$formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
47+
$formatter = array_key_exists(static::OPTION_KEY_FORMATTER, $options) ? $options[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
4648
$processor->processFiles($formatter);
4749
}
4850
$processor->saveOutput();
4951
}
5052

53+
/**
54+
* @param string $path
55+
* @param string $prefix
56+
*
57+
* @return string
58+
*/
5159
protected static function resolvePath($path, $prefix)
5260
{
5361
return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}";
@@ -56,92 +64,44 @@ protected static function resolvePath($path, $prefix)
5664
/**
5765
* @param array $config
5866
*
59-
* @return bool
6067
* @throws \InvalidArgumentException
6168
*/
6269
protected static function validateConfiguration(array $config)
6370
{
64-
if (empty($config[static::CONFIG_MAIN_KEY])) {
71+
if (!array_key_exists(static::CONFIG_MAIN_KEY, $config)) {
6572
throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
6673
}
6774

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

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)) {
85-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
79+
foreach ($config[static::CONFIG_MAIN_KEY] as $index => $options) {
80+
if (!is_array($options)) {
81+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index] should be an array");
8682
}
8783

88-
static::validateMandatoryOptions($option);
84+
static::validateMandatoryOptions($options, $index);
8985
}
90-
91-
return true;
9286
}
9387

9488
/**
95-
* @param array $config
89+
* @param array $options
90+
* @param int $index
9691
*
97-
* @return bool
9892
* @throws \InvalidArgumentException
9993
*/
100-
protected static function validateMandatoryOptions(array $config)
94+
protected static function validateMandatoryOptions(array $options, $index)
10195
{
102-
foreach (static::$mandatoryOptions as $option) {
103-
if (empty($config[$option])) {
104-
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
96+
foreach (static::$mandatoryOptions as $optionIndex => $type) {
97+
if (!array_key_exists($optionIndex, $options)) {
98+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} is required!");
10599
}
106100

107-
switch ($option) {
108-
case static::OPTION_KEY_INPUT:
109-
static::validateIsArray($config[$option]);
110-
break;
111-
case static::OPTION_KEY_OUTPUT:
112-
static::validateIsString($config[$option]);
113-
break;
101+
$callable = "is_{$type}";
102+
if (!$callable($options[$optionIndex])) {
103+
throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} should be {$type}!");
114104
}
115105
}
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;
146106
}
147107
}

tests/phpunit/ScriptHandlerTest.php

+31-18
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, 1]);
155168
}
156-
169+
157170
/*** *************************** INTEGRATION *************************** ***/
158171
/**
159172
* @see ScriptHandler::generateCSS

0 commit comments

Comments
 (0)