| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- declare(strict_types=1);
- use Nette\Schema\Expect;
- use Nette\Schema\Processor;
- use Tester\Assert;
- require __DIR__ . '/../bootstrap.php';
- test('', function () {
- $schema = Expect::scalar();
- Assert::same('hello', (new Processor)->process($schema, 'hello'));
- Assert::same(123, (new Processor)->process($schema, 123));
- Assert::same(false, (new Processor)->process($schema, false));
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, null);
- }, ['The item expects to be scalar, null given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, []);
- }, ['The item expects to be scalar, array given.']);
- });
- test('', function () {
- $schema = Expect::string();
- Assert::same('hello', (new Processor)->process($schema, 'hello'));
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, 123);
- }, ['The item expects to be string, 123 given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, null);
- }, ['The item expects to be string, null given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, false);
- }, ['The item expects to be string, false given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, []);
- }, ['The item expects to be string, array given.']);
- });
- test('', function () {
- $schema = Expect::type('string|bool');
- Assert::same('one', (new Processor)->process($schema, 'one'));
- Assert::same(true, (new Processor)->process($schema, true));
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, 123);
- }, ['The item expects to be string or bool, 123 given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, null);
- }, ['The item expects to be string or bool, null given.']);
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, []);
- }, ['The item expects to be string or bool, array given.']);
- });
- test('', function () {
- $schema = Expect::type('string')->nullable();
- Assert::same('one', (new Processor)->process($schema, 'one'));
- checkValidationErrors(function () use ($schema) {
- (new Processor)->process($schema, 123);
- }, ['The item expects to be null or string, 123 given.']);
- Assert::same(null, (new Processor)->process($schema, null));
- });
|