Expect.minmax.phpt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. use Nette\Schema\Expect;
  4. use Nette\Schema\Processor;
  5. use Tester\Assert;
  6. require __DIR__ . '/../bootstrap.php';
  7. test('int & min', function () {
  8. $schema = Expect::int()->min(10);
  9. Assert::same(10, (new Processor)->process($schema, 10));
  10. checkValidationErrors(function () use ($schema) {
  11. (new Processor)->process($schema, 9);
  12. }, ['The item expects to be in range 10.., 9 given.']);
  13. });
  14. test('int & max', function () {
  15. $schema = Expect::int()->max(20);
  16. Assert::same(20, (new Processor)->process($schema, 20));
  17. checkValidationErrors(function () use ($schema) {
  18. (new Processor)->process($schema, 21);
  19. }, ['The item expects to be in range ..20, 21 given.']);
  20. });
  21. test('int & min & max', function () {
  22. $schema = Expect::int()->min(10)->max(20);
  23. Assert::same(10, (new Processor)->process($schema, 10));
  24. Assert::same(20, (new Processor)->process($schema, 20));
  25. checkValidationErrors(function () use ($schema) {
  26. (new Processor)->process($schema, 9);
  27. }, ['The item expects to be in range 10..20, 9 given.']);
  28. checkValidationErrors(function () use ($schema) {
  29. (new Processor)->process($schema, 21);
  30. }, ['The item expects to be in range 10..20, 21 given.']);
  31. });
  32. test('nullable int & min & max', function () {
  33. $schema = Expect::int()->min(10)->max(20)->nullable();
  34. Assert::same(null, (new Processor)->process($schema, null));
  35. Assert::same(15, (new Processor)->process($schema, 15));
  36. checkValidationErrors(function () use ($schema) {
  37. (new Processor)->process($schema, 9);
  38. }, ['The item expects to be in range 10..20, 9 given.']);
  39. });
  40. test('string', function () {
  41. $schema = Expect::string()->min(1)->max(5);
  42. Assert::same('hello', (new Processor)->process($schema, 'hello'));
  43. Assert::same('x', (new Processor)->process($schema, 'x'));
  44. checkValidationErrors(function () use ($schema) {
  45. (new Processor)->process($schema, '');
  46. }, ['The length of item expects to be in range 1..5, 0 bytes given.']);
  47. checkValidationErrors(function () use ($schema) {
  48. (new Processor)->process($schema, 'foobar');
  49. }, ['The length of item expects to be in range 1..5, 6 bytes given.']);
  50. });
  51. test('unicode', function () {
  52. $schema = Expect::unicode()->min(2)->max(4);
  53. Assert::same('žšáé', (new Processor)->process($schema, 'žšáé'));
  54. Assert::same('žš', (new Processor)->process($schema, 'žš'));
  55. checkValidationErrors(function () use ($schema) {
  56. (new Processor)->process($schema, 'ž');
  57. }, ['The length of item expects to be in range 2..4, 1 characters given.']);
  58. checkValidationErrors(function () use ($schema) {
  59. (new Processor)->process($schema, 'žšáéx');
  60. }, ['The length of item expects to be in range 2..4, 5 characters given.']);
  61. });
  62. test('array', function () {
  63. $schema = Expect::array()->min(1)->max(3);
  64. Assert::same([1], (new Processor)->process($schema, [1]));
  65. Assert::same([1, 2, 3], (new Processor)->process($schema, [1, 2, 3]));
  66. checkValidationErrors(function () use ($schema) {
  67. (new Processor)->process($schema, []);
  68. }, ['The length of item expects to be in range 1..3, 0 items given.']);
  69. checkValidationErrors(function () use ($schema) {
  70. (new Processor)->process($schema, [1, 2, 3, 4]);
  71. }, ['The length of item expects to be in range 1..3, 4 items given.']);
  72. });
  73. test('structure', function () {
  74. $schema = Expect::structure([])->otherItems('int')->min(1)->max(3);
  75. Assert::equal((object) [1], (new Processor)->process($schema, [1]));
  76. Assert::equal((object) [1, 2, 3], (new Processor)->process($schema, [1, 2, 3]));
  77. checkValidationErrors(function () use ($schema) {
  78. (new Processor)->process($schema, []);
  79. }, ['The length of item expects to be in range 1..3, 0 items given.']);
  80. checkValidationErrors(function () use ($schema) {
  81. (new Processor)->process($schema, [1, 2, 3, 4]);
  82. }, ['The length of item expects to be in range 1..3, 4 items given.']);
  83. });