|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +use Nette\Schema\Context; |
| 4 | +use Nette\Schema\Expect; |
| 5 | +use Nette\Schema\Processor; |
| 6 | +use Tester\Assert; |
| 7 | + |
| 8 | + |
| 9 | +require __DIR__ . '/../bootstrap.php'; |
| 10 | + |
| 11 | + |
| 12 | +test('chaining before, assert, and transform', function () { |
| 13 | + $schema = Expect::string() |
| 14 | + ->before(fn($v) => strtolower($v)) |
| 15 | + ->assert(fn($s) => ctype_alpha($s), 'Must contain only letters') |
| 16 | + ->transform(fn($v) => ucfirst($v)); |
| 17 | + |
| 18 | + Assert::same('Hello', (new Processor)->process($schema, 'HELLO')); |
| 19 | + Assert::same('World', (new Processor)->process($schema, 'world')); |
| 20 | + |
| 21 | + checkValidationErrors(function () use ($schema) { |
| 22 | + (new Processor)->process($schema, 'HELLO123'); |
| 23 | + }, ["Failed assertion 'Must contain only letters' for item with value 'hello123'."]); |
| 24 | +}); |
| 25 | + |
| 26 | + |
| 27 | +test('multiple assertions in sequence', function () { |
| 28 | + $schema = Expect::string() |
| 29 | + ->assert(ctype_digit(...), 'Must be numeric') |
| 30 | + ->assert(fn($s) => strlen($s) >= 3, 'Too short') |
| 31 | + ->assert(fn($s) => strlen($s) <= 10, 'Too long'); |
| 32 | + |
| 33 | + Assert::same('12345', (new Processor)->process($schema, '12345')); |
| 34 | + |
| 35 | + checkValidationErrors(function () use ($schema) { |
| 36 | + (new Processor)->process($schema, 'abc'); |
| 37 | + }, ["Failed assertion 'Must be numeric' for item with value 'abc'."]); |
| 38 | + |
| 39 | + checkValidationErrors(function () use ($schema) { |
| 40 | + (new Processor)->process($schema, '12'); |
| 41 | + }, ["Failed assertion 'Too short' for item with value '12'."]); |
| 42 | + |
| 43 | + checkValidationErrors(function () use ($schema) { |
| 44 | + (new Processor)->process($schema, '12345678901'); |
| 45 | + }, ["Failed assertion 'Too long' for item with value '12345678901'."]); |
| 46 | +}); |
| 47 | + |
| 48 | + |
| 49 | +test('pattern() combined with min/max', function () { |
| 50 | + $schema = Expect::string() |
| 51 | + ->pattern('[a-z]+') |
| 52 | + ->min(3) |
| 53 | + ->max(10); |
| 54 | + |
| 55 | + Assert::same('hello', (new Processor)->process($schema, 'hello')); |
| 56 | + |
| 57 | + checkValidationErrors(function () use ($schema) { |
| 58 | + (new Processor)->process($schema, 'Hello'); |
| 59 | + }, ["The item expects to match pattern '[a-z]+', 'Hello' given."]); |
| 60 | + |
| 61 | + checkValidationErrors(function () use ($schema) { |
| 62 | + (new Processor)->process($schema, 'ab'); |
| 63 | + }, ['The length of item expects to be in range 3..10, 2 bytes given.']); |
| 64 | + |
| 65 | + checkValidationErrors(function () use ($schema) { |
| 66 | + (new Processor)->process($schema, 'abcdefghijk'); |
| 67 | + }, ['The length of item expects to be in range 3..10, 11 bytes given.']); |
| 68 | +}); |
| 69 | + |
| 70 | + |
| 71 | +test('transform with validation using context', function () { |
| 72 | + $schema = Expect::string() |
| 73 | + ->transform(function (string $s, Context $context) { |
| 74 | + if (!ctype_lower($s)) { |
| 75 | + $context->addError('All characters must be lowercase', 'validation.lowercase'); |
| 76 | + return null; |
| 77 | + } |
| 78 | + return strtoupper($s); |
| 79 | + }); |
| 80 | + |
| 81 | + Assert::same('HELLO', (new Processor)->process($schema, 'hello')); |
| 82 | + |
| 83 | + checkValidationErrors(function () use ($schema) { |
| 84 | + (new Processor)->process($schema, 'Hello'); |
| 85 | + }, ['All characters must be lowercase']); |
| 86 | +}); |
| 87 | + |
| 88 | + |
| 89 | +test('multiple transforms in sequence', function () { |
| 90 | + $schema = Expect::string() |
| 91 | + ->transform(fn($s) => trim($s)) |
| 92 | + ->transform(fn($s) => strtolower($s)) |
| 93 | + ->transform(fn($s) => ucwords($s)); |
| 94 | + |
| 95 | + Assert::same('Hello World', (new Processor)->process($schema, ' HELLO WORLD ')); |
| 96 | +}); |
| 97 | + |
| 98 | + |
| 99 | +test('assert() and transform interleaved', function () { |
| 100 | + $schema = Expect::int() |
| 101 | + ->assert(fn($v) => $v > 0, 'Must be positive') |
| 102 | + ->transform(fn($v) => $v * 2) |
| 103 | + ->assert(fn($v) => $v < 100, 'Result too large') |
| 104 | + ->transform(fn($v) => (string) $v); |
| 105 | + |
| 106 | + Assert::same('20', (new Processor)->process($schema, 10)); |
| 107 | + |
| 108 | + checkValidationErrors(function () use ($schema) { |
| 109 | + (new Processor)->process($schema, -5); |
| 110 | + }, ["Failed assertion 'Must be positive' for item with value -5."]); |
| 111 | + |
| 112 | + checkValidationErrors(function () use ($schema) { |
| 113 | + (new Processor)->process($schema, 60); |
| 114 | + }, ["Failed assertion 'Result too large' for item with value 120."]); |
| 115 | +}); |
| 116 | + |
| 117 | + |
| 118 | +test('before() normalization with type conversion', function () { |
| 119 | + $schema = Expect::int() |
| 120 | + ->before(fn($v) => is_string($v) ? (int) $v : $v) |
| 121 | + ->min(1) |
| 122 | + ->max(100); |
| 123 | + |
| 124 | + Assert::same(42, (new Processor)->process($schema, '42')); |
| 125 | + Assert::same(42, (new Processor)->process($schema, 42)); |
| 126 | + |
| 127 | + checkValidationErrors(function () use ($schema) { |
| 128 | + (new Processor)->process($schema, '0'); |
| 129 | + }, ['The item expects to be in range 1..100, 0 given.']); |
| 130 | +}); |
| 131 | + |
| 132 | + |
| 133 | +test('castTo() combined with transformations', function () { |
| 134 | + $schema = Expect::string() |
| 135 | + ->pattern('\d{4}-\d{2}-\d{2}') |
| 136 | + ->castTo(DateTime::class); |
| 137 | + |
| 138 | + $result = (new Processor)->process($schema, '2024-01-15'); |
| 139 | + Assert::type(DateTime::class, $result); |
| 140 | + Assert::same('2024-01-15', $result->format('Y-m-d')); |
| 141 | + |
| 142 | + checkValidationErrors(function () use ($schema) { |
| 143 | + (new Processor)->process($schema, 'invalid-date'); |
| 144 | + }, ["The item expects to match pattern '\\d{4}-\\d{2}-\\d{2}', 'invalid-date' given."]); |
| 145 | +}); |
| 146 | + |
| 147 | + |
| 148 | +test('nullable with all validators', function () { |
| 149 | + $schema = Expect::string() |
| 150 | + ->nullable() |
| 151 | + ->pattern('[a-z]+') |
| 152 | + ->min(3) |
| 153 | + ->transform(fn($v) => $v ? strtoupper($v) : null); |
| 154 | + |
| 155 | + Assert::same('HELLO', (new Processor)->process($schema, 'hello')); |
| 156 | + Assert::same(null, (new Processor)->process($schema, null)); |
| 157 | + |
| 158 | + checkValidationErrors(function () use ($schema) { |
| 159 | + (new Processor)->process($schema, 'ab'); |
| 160 | + }, ['The length of item expects to be in range 3.., 2 bytes given.']); |
| 161 | +}); |
| 162 | + |
| 163 | + |
| 164 | +test('deprecated combined with other validators', function () { |
| 165 | + $schema = Expect::string() |
| 166 | + ->deprecated('Use newField instead') |
| 167 | + ->pattern('\w+'); |
| 168 | + |
| 169 | + $processor = new Processor; |
| 170 | + Assert::same('test', $processor->process($schema, 'test')); |
| 171 | + Assert::same(['Use newField instead'], $processor->getWarnings()); |
| 172 | +}); |
| 173 | + |
| 174 | + |
| 175 | +test('deprecated does not warn when value not provided', function () { |
| 176 | + $schema = Expect::structure([ |
| 177 | + 'oldField' => Expect::string()->deprecated('Use newField instead'), |
| 178 | + 'newField' => Expect::string(), |
| 179 | + ]); |
| 180 | + |
| 181 | + $processor = new Processor; |
| 182 | + Assert::equal( |
| 183 | + (object) ['oldField' => null, 'newField' => 'value'], |
| 184 | + $processor->process($schema, ['newField' => 'value']), |
| 185 | + ); |
| 186 | + Assert::same([], $processor->getWarnings()); |
| 187 | +}); |
| 188 | + |
| 189 | + |
| 190 | +test('complex validation chain on array items', function () { |
| 191 | + $schema = Expect::arrayOf( |
| 192 | + Expect::string() |
| 193 | + ->pattern('[a-z]+') |
| 194 | + ->min(2) |
| 195 | + ->transform(fn($s) => ucfirst($s)), |
| 196 | + ); |
| 197 | + |
| 198 | + Assert::same(['Hello', 'World'], (new Processor)->process($schema, ['hello', 'world'])); |
| 199 | + |
| 200 | + checkValidationErrors(function () use ($schema) { |
| 201 | + (new Processor)->process($schema, ['hello', 'X']); |
| 202 | + }, ["The length of item '1' expects to be in range 2.., 1 bytes given."]); |
| 203 | + |
| 204 | + checkValidationErrors(function () use ($schema) { |
| 205 | + (new Processor)->process($schema, ['hello', 'World']); |
| 206 | + }, ["The item '1' expects to match pattern '[a-z]+', 'World' given."]); |
| 207 | +}); |
0 commit comments