<?php declare(strict_types=1); use Nette\Schema\Expect; use Nette\Schema\Processor; use Tester\Assert; require __DIR__ . '/../bootstrap.php'; test('without default value', function () { $schema = Expect::list(); Assert::same([], (new Processor)->process($schema, [])); Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c'])); checkValidationErrors(function () use ($schema) { Assert::same(['key' => 'val'], (new Processor)->process($schema, ['key' => 'val'])); }, ['The item expects to be list, array given.']); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, 'one'); }, ["The item expects to be list, 'one' given."]); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, true); }, ['The item expects to be list, true given.']); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, 123); }, ['The item expects to be list, 123 given.']); Assert::same([], (new Processor)->process($schema, null)); }); test('not merging', function () { $schema = Expect::list([1, 2, 3])->mergeDefaults(false); Assert::same([], (new Processor)->process($schema, [])); Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c'])); Assert::same([], (new Processor)->process($schema, null)); }); test('merging', function () { $schema = Expect::list([1, 2, 3]); Assert::same([1, 2, 3], (new Processor)->process($schema, [])); Assert::same([1, 2, 3, 'a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c'])); Assert::same([1, 2, 3], (new Processor)->process($schema, null)); }); test('merging & other items validation', function () { $schema = Expect::list([1, 2, 3])->items('string'); Assert::same([1, 2, 3], (new Processor)->process($schema, [])); Assert::same([1, 2, 3, 'a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c'])); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, [1, 2, 3]); }, [ "The item '0' expects to be string, 1 given.", "The item '1' expects to be string, 2 given.", "The item '2' expects to be string, 3 given.", ]); Assert::same([1, 2, 3], (new Processor)->process($schema, null)); }); test('listOf() & scalar', function () { $schema = Expect::listOf('string'); Assert::same([], (new Processor)->process($schema, [])); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, [1, 2, 3]); }, [ "The item '0' expects to be string, 1 given.", "The item '1' expects to be string, 2 given.", "The item '2' expects to be string, 3 given.", ]); Assert::same(['val', 'val'], (new Processor)->process($schema, ['val', 'val'])); checkValidationErrors(function () use ($schema) { (new Processor)->process($schema, ['key' => 'val']); }, ['The item expects to be list, array given.']); }); test('listOf() & error', function () { Assert::exception(function () { Expect::listOf(['a' => Expect::string()]); }, TypeError::class); });
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
fixtures | Folder | 0755 |
|
|
Expect.anyOf.phpt | File | 8.17 KB | 0644 |
|
Expect.array.phpt | File | 8.14 KB | 0644 |
|
Expect.assert.phpt | File | 1.61 KB | 0644 |
|
Expect.before.phpt | File | 2.26 KB | 0644 |
|
Expect.castTo.phpt | File | 635 B | 0644 |
|
Expect.dynamic.phpt | File | 1.87 KB | 0644 |
|
Expect.from.php74.phpt | File | 906 B | 0644 |
|
Expect.from.php80.phpt | File | 951 B | 0644 |
|
Expect.from.phpt | File | 2.34 KB | 0644 |
|
Expect.list.phpt | File | 3.08 KB | 0644 |
|
Expect.minmax.phpt | File | 3.91 KB | 0644 |
|
Expect.pattern.phpt | File | 722 B | 0644 |
|
Expect.scalars.phpt | File | 2.46 KB | 0644 |
|
Expect.structure.phpt | File | 14.15 KB | 0644 |
|
Helpers.formatValue().phpt | File | 644 B | 0644 |
|
Helpers.getPropertyType.php74.phpt | File | 1.49 KB | 0644 |
|
Helpers.getPropertyType.phpt | File | 974 B | 0644 |
|
Helpers.merge.phpt | File | 2.05 KB | 0644 |
|
Helpers.parseAnnotation().phpt | File | 1.06 KB | 0644 |
|
Processor.context.phpt | File | 828 B | 0644 |
|
heterogenous.phpt | File | 1.34 KB | 0644 |
|