1. <?php
  2. /**
  3. * Function to assert conditions of text blocks with retry logic for non-production use.
  4. *
  5. * @param callable $condition_function A callable that accepts a text block and returns true if the condition is met, false otherwise.
  6. * @param int $max_retries The maximum number of retry attempts.
  7. * @param int $retry_delay The delay in seconds between retry attempts.
  8. * @param string $error_message The error message to return if the condition is not met after all retries.
  9. * @param string $text_block The text block to test.
  10. * @return bool True if the condition is met, false otherwise (after all retries).
  11. */
  12. function assertTextCondition(callable $condition_function, int $max_retries, int $retry_delay, string $error_message, string $text_block): bool
  13. {
  14. $retries = 0;
  15. while ($retries < $max_retries) {
  16. try {
  17. if ($condition_function($text_block)) {
  18. return true; // Condition met, return true
  19. } else {
  20. $retries++;
  21. if ($retries < $max_retries) {
  22. sleep($retry_delay); // Wait before retrying
  23. }
  24. }
  25. } catch (Exception $e) {
  26. $retries++;
  27. if ($retries < $max_retries) {
  28. sleep($retry_delay);
  29. }
  30. }
  31. }
  32. // Condition not met after all retries
  33. return false;
  34. }
  35. // Example Usage (replace with your actual condition and text block)
  36. /*
  37. $condition = function (string $text) {
  38. return strpos($text, 'expected_text') !== false;
  39. };
  40. $text = "This is a test text with expected_text.";
  41. $maxRetries = 3;
  42. $retryDelay = 2;
  43. $errorMessage = "Condition not met after multiple retries.";
  44. if (assertTextCondition($condition, $maxRetries, $retryDelay, $errorMessage, $text)) {
  45. echo "Condition met!";
  46. } else {
  47. echo $errorMessage;
  48. }
  49. */
  50. ?>

Add your comment