Changed parsing of login_extra_fields in guzzle auth

This commit is contained in:
Bertrand Dunogier 2017-01-22 00:42:05 +01:00
parent 5b914b0422
commit 662db41bae
2 changed files with 28 additions and 2 deletions

View file

@ -54,7 +54,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
'loginUri' => $config->login_uri ?: null, 'loginUri' => $config->login_uri ?: null,
'usernameField' => $config->login_username_field ?: null, 'usernameField' => $config->login_username_field ?: null,
'passwordField' => $config->login_password_field ?: null, 'passwordField' => $config->login_password_field ?: null,
'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [], 'extraFields' => $this->processExtraFields($config->login_extra_fields),
'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
]; ];
@ -65,4 +65,30 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
return new SiteConfig($parameters); return new SiteConfig($parameters);
} }
/**
* Processes login_extra_fields config, transforming an '=' separated array of strings
* into a key/value array.
*
* @param array|mixed $extraFieldsStrings
*
* @return array
*/
protected function processExtraFields($extraFieldsStrings)
{
if (!is_array($extraFieldsStrings)) {
return [];
}
$extraFields = [];
foreach ($extraFieldsStrings as $extraField) {
if (strpos($extraField, '=') === false) {
continue;
}
list($fieldName, $fieldValue) = explode('=', $extraField, 2);
$extraFields[$fieldName] = $fieldValue;
}
return $extraFields;
}
} }

View file

@ -24,7 +24,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
$grabySiteConfig->login_uri = 'http://example.com/login'; $grabySiteConfig->login_uri = 'http://example.com/login';
$grabySiteConfig->login_username_field = 'login'; $grabySiteConfig->login_username_field = 'login';
$grabySiteConfig->login_password_field = 'password'; $grabySiteConfig->login_password_field = 'password';
$grabySiteConfig->login_extra_fields = ['field' => 'value']; $grabySiteConfig->login_extra_fields = ['field=value'];
$grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
$grabyConfigBuilderMock $grabyConfigBuilderMock