-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various new traits #5
base: main
Are you sure you want to change the base?
Conversation
larowlan
commented
Jul 18, 2024
- SpinTrait for retrying in a test
- Extra random methods (url, email, location, address)
- Constraint violation asserts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just a few nits. Also the other existing traits have README entries. It's a hassle though, do we think it's worthwhile?
/** | ||
* Generates a random URL. | ||
* | ||
* @return string | ||
* Random URL. | ||
*/ | ||
protected function randomUrl(): string { | ||
$random = new Random(); | ||
return \sprintf('https://%s.com/%s', $random->name(), $random->name()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always felt we should use example.com
as the domain name. Turns out there is now a reserved .example TLD.
/** | |
* Generates a random URL. | |
* | |
* @return string | |
* Random URL. | |
*/ | |
protected function randomUrl(): string { | |
$random = new Random(); | |
return \sprintf('https://%s.com/%s', $random->name(), $random->name()); | |
} | |
/** | |
* Generates a random URL. | |
* | |
* @return string | |
* Random URL. | |
*/ | |
protected function randomUrl(): string { | |
$random = new Random(); | |
return \sprintf('https://%s.example/%s', $random->name(), $random->name()); | |
} |
*/ | ||
protected function randomEmail(): string { | ||
$random = new Random(); | ||
return \sprintf('%s@%s.com', $random->name(), $random->name()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return \sprintf('%s@%s.com', $random->name(), $random->name()); | |
return \sprintf('%s@%s.example', $random->name(), $random->name()); |
protected function randomLatLonPair(): string { | ||
$lon = $this->randomPoint(-180, 180); | ||
$lat = $this->randomPoint(-84, 84); | ||
return \sprintf('POINT(%s %s)', $lon, $lat); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: floats are not strings
return \sprintf('POINT(%s %s)', $lon, $lat); | |
return \sprintf('POINT(%.05n %.05n)', $lon, $lat); |
* Executes a callable until it returns TRUE. | ||
* | ||
* Executes executing a task until a condition is met. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Executes a callable until it returns TRUE. | |
* | |
* Executes executing a task until a condition is met. | |
* Executes a callable until it returns TRUE. |
* @param int $count | ||
* (optional) Number of times to try, defaults to 10. | ||
* @param bool $throw | ||
* (optional) Throw, TRUE to throw if the condition is not met. | ||
* | ||
* @return bool | ||
* TRUE if lambda evaluated true. | ||
* | ||
* @throws \Exception | ||
* When the condition is not met. | ||
*/ | ||
protected function spin(callable $lambda, $count = 10, $throw = TRUE): bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
* @param int $count | |
* (optional) Number of times to try, defaults to 10. | |
* @param bool $throw | |
* (optional) Throw, TRUE to throw if the condition is not met. | |
* | |
* @return bool | |
* TRUE if lambda evaluated true. | |
* | |
* @throws \Exception | |
* When the condition is not met. | |
*/ | |
protected function spin(callable $lambda, $count = 10, $throw = TRUE): bool { | |
* @param non-negative-int $count | |
* (optional) Number of times to try, defaults to 10. | |
* @param bool $throw | |
* (optional) Throw, TRUE to throw if the condition is not met. | |
* | |
* @return bool | |
* TRUE if lambda evaluated true. | |
* | |
* @throws \Exception | |
* When the condition is not met. | |
*/ | |
protected function spin(callable $lambda, int $count = 10, bool $throw = TRUE): bool { |
catch (\Exception $e) { | ||
// Do nothing. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-capturing catch:
catch (\Exception $e) { | |
// Do nothing. | |
} | |
catch (\Exception) { | |
// Do nothing. | |
} |
} | ||
// Max reached. | ||
if ($throw) { | ||
throw new \Exception(\sprintf('Condition was not met after %d attempts', $count)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a more specific exception, maybe \Behat\Mink\Exception\ExpectationException
.
/** | ||
* Generates a random lat/lon point. | ||
*/ | ||
private function randomPoint(int $min, int $max): float { | ||
$number = \mt_rand($min, $max); | ||
$decimals = \mt_rand(1, \pow(10, 5)) / \pow(10, 5); | ||
return \round($number + $decimals, 5); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also require PHP 8.3 and use Randomizer::getFloat.