Strings.Regexp.errors.backtrack.phpt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Strings and RegexpException run-time error.
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Strings;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. ini_set('pcre.backtrack_limit', '3'); // forces PREG_BACKTRACK_LIMIT_ERROR
  10. ini_set('pcre.jit', '0');
  11. Assert::exception(
  12. fn() => Strings::split('0123456789', '#.*\d\d#'),
  13. Nette\Utils\RegexpException::class,
  14. 'Backtrack limit exhausted (pattern: #.*\d\d#)',
  15. );
  16. Assert::exception(
  17. fn() => Strings::match('0123456789', '#.*\d\d#'),
  18. Nette\Utils\RegexpException::class,
  19. 'Backtrack limit exhausted (pattern: #.*\d\d#)',
  20. );
  21. Assert::exception(
  22. fn() => Strings::matchAll('0123456789', '#.*\d\d#'),
  23. Nette\Utils\RegexpException::class,
  24. 'Backtrack limit exhausted (pattern: #.*\d\d#)',
  25. );
  26. Assert::exception(
  27. fn() => Strings::replace('0123456789', '#.*\d\d#', 'x'),
  28. Nette\Utils\RegexpException::class,
  29. 'Backtrack limit exhausted (pattern: #.*\d\d#)',
  30. );
  31. function cb()
  32. {
  33. return 'x';
  34. }
  35. Assert::exception(
  36. fn() => Strings::replace('0123456789', '#.*\d\d#', Closure::fromCallable('cb')),
  37. Nette\Utils\RegexpException::class,
  38. 'Backtrack limit exhausted (pattern: #.*\d\d#)',
  39. );