Validators.assert().phpt 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Test: Nette\Utils\Validators::assert()
  4. */
  5. declare(strict_types=1);
  6. use Nette\Utils\Validators;
  7. use Tester\Assert;
  8. require __DIR__ . '/../bootstrap.php';
  9. Assert::exception(
  10. fn() => Validators::assert(true, 'int'),
  11. Nette\Utils\AssertionException::class,
  12. 'The variable expects to be int, bool given.',
  13. );
  14. Assert::exception(
  15. fn() => Validators::assert('', 'int'),
  16. Nette\Utils\AssertionException::class,
  17. "The variable expects to be int, string '' given.",
  18. );
  19. Assert::exception(
  20. fn() => Validators::assert(str_repeat('x', 1000), 'int'),
  21. Nette\Utils\AssertionException::class,
  22. 'The variable expects to be int, string given.',
  23. );
  24. Assert::exception(
  25. fn() => Validators::assert('1.0', 'int|float'),
  26. Nette\Utils\AssertionException::class,
  27. "The variable expects to be int or float, string '1.0' given.",
  28. );
  29. Assert::exception(
  30. fn() => Validators::assert(null, 'int'),
  31. Nette\Utils\AssertionException::class,
  32. 'The variable expects to be int, null given.',
  33. );
  34. Assert::exception(
  35. fn() => Validators::assert(1.0, 'int'),
  36. Nette\Utils\AssertionException::class,
  37. 'The variable expects to be int, float 1.0 given.',
  38. );
  39. Assert::exception(
  40. fn() => Validators::assert(1, 'float'),
  41. Nette\Utils\AssertionException::class,
  42. 'The variable expects to be float, int 1 given.',
  43. );
  44. Assert::exception(
  45. fn() => Validators::assert([], 'int'),
  46. Nette\Utils\AssertionException::class,
  47. 'The variable expects to be int, array given.',
  48. );
  49. Assert::exception(
  50. fn() => Validators::assert(new stdClass, 'int'),
  51. Nette\Utils\AssertionException::class,
  52. 'The variable expects to be int, object stdClass given.',
  53. );
  54. Assert::exception(
  55. fn() => Validators::assert(1, 'string|integer:2..5', 'variable'),
  56. Nette\Utils\AssertionException::class,
  57. 'The variable expects to be string or integer in range 2..5, int 1 given.',
  58. );
  59. Assert::exception(
  60. fn() => Validators::assert('x', '?int'),
  61. Nette\Utils\AssertionException::class,
  62. "The variable expects to be ?int, string 'x' given.",
  63. );