Expect.from.php80.phpt 992 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * @phpversion 8.0
  4. */
  5. declare(strict_types=1);
  6. use Nette\Schema\Elements\Structure;
  7. use Nette\Schema\Expect;
  8. use Nette\Schema\Processor;
  9. use Tester\Assert;
  10. require __DIR__ . '/../bootstrap.php';
  11. Assert::with(Structure::class, function () {
  12. $schema = Expect::from($obj = new class {
  13. public string $dsn = 'mysql';
  14. public ?string $user;
  15. public ?string $password = null;
  16. public array|int $options = [];
  17. public bool $debugger = true;
  18. public mixed $mixed;
  19. public array $arr = [1];
  20. });
  21. Assert::type(Structure::class, $schema);
  22. Assert::equal([
  23. 'dsn' => Expect::string('mysql'),
  24. 'user' => Expect::type('?string')->required(),
  25. 'password' => Expect::type('?string'),
  26. 'options' => Expect::type('array|int')->default([]),
  27. 'debugger' => Expect::bool(true),
  28. 'mixed' => Expect::mixed()->required(),
  29. 'arr' => Expect::type('array')->default([1]),
  30. ], $schema->items);
  31. Assert::type($obj, (new Processor)->process($schema, ['user' => '', 'mixed' => '']));
  32. });