MOBILE-4069 behat: Support time transformation in find step

main
Dani Palou 2023-10-09 11:18:37 +02:00
parent 2db13af8dd
commit 7c16d5818f
1 changed files with 35 additions and 2 deletions

View File

@ -275,13 +275,13 @@ class behat_app_helper extends behat_base {
); );
$locator = [ $locator = [
'text' => str_replace('\\"', '"', $matches[1]), 'text' => $this->transform_time_to_string(str_replace('\\"', '"', $matches[1])),
'selector' => $matches[2] ?? null, 'selector' => $matches[2] ?? null,
]; ];
if (!empty($matches[3])) { if (!empty($matches[3])) {
$locator[$matches[3]] = (object) [ $locator[$matches[3]] = (object) [
'text' => str_replace('\\"', '"', $matches[4]), 'text' => $this->transform_time_to_string(str_replace('\\"', '"', $matches[4])),
'selector' => $matches[5] ?? null, 'selector' => $matches[5] ?? null,
]; ];
} }
@ -608,4 +608,37 @@ EOF;
$this->getSession()->getDriver()->resizeWindow($width + $offset['x'], $height + $offset['y']); $this->getSession()->getDriver()->resizeWindow($width + $offset['x'], $height + $offset['y']);
} }
/**
* Given a string, search if it contains a time with the ## format and convert it to a timestamp or readable time.
* Only allows 1 occurence, if the text contains more than one time sub-string it won't work as expected.
* This function is similar to the arg_time_to_string transformation, but it allows the time to be a sub-text of the string.
*
* @param string $text
* @return string Transformed text.
*/
protected function transform_time_to_string(string $text): string {
if (!preg_match('/##(.*)##/', $text, $matches)) {
// No time found, return the original text.
return $text;
}
$timepassed = explode('##', $matches[1]);
// If not a valid time string, then just return what was passed.
if ((($timestamp = strtotime($timepassed[0])) === false)) {
return $text;
}
$count = count($timepassed);
if ($count === 2) {
// If timestamp with specified strftime format, then return formatted date string.
return str_replace($matches[0], userdate($timestamp, $timepassed[1]), $text);
} else if ($count === 1) {
return str_replace($matches[0], $timestamp, $text);
} else {
// If not a valid time string, then just return what was passed.
return $text;
}
}
} }