pastebin

Paste Search Dynamic
Recent pastes
Expect anyOf
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. use NetteSchemaExpect;
  6. use NetteSchemaProcessor;
  7. use TesterAssert;
  8.  
  9.  
  10. require __DIR__ . '/../bootstrap.php';
  11.  
  12.  
  13. test('with scalars', function () {
  14.         $schema = Expect::anyOf('one', true, Expect::int());
  15.  
  16.         assert::same('one', (new Processor)->process($schema, 'one'));
  17.  
  18.         assert::same(true, (new Processor)->process($schema, true));
  19.  
  20.         assert::same(123, (new Processor)->process($schema, 123));
  21.  
  22.         checkValidationErrors(function () use ($schema) {
  23.                 (new Processor)->process($schema, false);
  24.         }, ["The item expects to be 'one'|true|int, false given."]);
  25.  
  26.         checkValidationErrors(function () use ($schema) {
  27.                 (new Processor)->process($schema, 'two');
  28.         }, ["The item expects to be 'one'|true|int, 'two' given."]);
  29.  
  30.         checkValidationErrors(function () use ($schema) {
  31.                 (new Processor)->process($schema, null);
  32.         }, ["The item expects to be 'one'|true|int, null given."]);
  33.  
  34.         checkValidationErrors(function () use ($schema) {
  35.                 (new Processor)->process($schema, []);
  36.         }, ["The item expects to be 'one'|true|int, array given."]);
  37. });
  38.  
  39.  
  40. test('with complex structure', function () {
  41.         $schema = Expect::anyOf(Expect::listOf('string'), true, Expect::int());
  42.  
  43.         checkValidationErrors(function () use ($schema) {
  44.                 (new Processor)->process($schema, false);
  45.         }, ['The item expects to be list|true|int, false given.']);
  46.  
  47.         checkValidationErrors(function () use ($schema) {
  48.                 (new Processor)->process($schema, [123]);
  49.         }, ["The item '0' expects to be string, 123 given."]);
  50.  
  51.         assert::same(['foo'], (new Processor)->process($schema, ['foo']));
  52. });
  53.  
  54.  
  55. test('with asserts', function () {
  56.         $schema = Expect::anyOf(Expect::string()->assert('strlen'), true);
  57.  
  58.         checkValidationErrors(function () use ($schema) {
  59.                 (new Processor)->process($schema, '');
  60.         }, ["Failed assertion strlen() for item with value ''."]);
  61.  
  62.         checkValidationErrors(function () use ($schema) {
  63.                 (new Processor)->process($schema, 123);
  64.         }, ['The item expects to be string|true, 123 given.']);
  65.  
  66.         assert::same('foo', (new Processor)->process($schema, 'foo'));
  67. });
  68.  
  69.  
  70. test('no default value', function () {
  71.         $schema = Expect::structure([
  72.                 'key1' => Expect::anyOf(Expect::string(), Expect::int()),
  73.                 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int()),
  74.                 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int()),
  75.         ]);
  76.  
  77.         assert::equal(
  78.                 (object) ['key1' => null, 'key2' => null, 'key3' => null],
  79.                 (new Processor)->process($schema, [])
  80.         );
  81. });
  82.  
  83.  
  84. test('required', function () {
  85.         $schema = Expect::structure([
  86.                 'key1' => Expect::anyOf(Expect::string(), Expect::int())->required(),
  87.                 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(),
  88.                 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(),
  89.                 'key4' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  90.                 'key5' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  91.         ]);
  92.  
  93.         checkValidationErrors(function () use ($schema) {
  94.                 (new Processor)->process($schema, []);
  95.         }, [
  96.                 "The mandatory item 'key1' is missing.",
  97.                 "The mandatory item 'key2' is missing.",
  98.                 "The mandatory item 'key3' is missing.",
  99.                 "The mandatory item 'key4' is missing.",
  100.                 "The mandatory item 'key5' is missing.",
  101.         ]);
  102. });
  103.  
  104.  
  105. test('required as argument', function () {
  106.         $schema = Expect::structure([
  107.                 'key1' => Expect::anyOf(Expect::string(), Expect::int())->required(),
  108.                 'key1nr' => Expect::anyOf(Expect::string(), Expect::int())->required(false),
  109.                 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(),
  110.                 'key2nr' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(false),
  111.                 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(),
  112.                 'key3nr' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(false),
  113.                 'key4' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  114.                 'key4nr' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(false),
  115.                 'key5' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  116.                 'key5nr' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(false)->nullable(),
  117.         ]);
  118.  
  119.         checkValidationErrors(function () use ($schema) {
  120.                 (new Processor)->process($schema, []);
  121.         }, [
  122.                 "The mandatory item 'key1' is missing.",
  123.                 "The mandatory item 'key2' is missing.",
  124.                 "The mandatory item 'key3' is missing.",
  125.                 "The mandatory item 'key4' is missing.",
  126.                 "The mandatory item 'key5' is missing.",
  127.         ]);
  128. });
  129.  
  130.  
  131. test('not nullable', function () {
  132.         $schema = Expect::structure([
  133.                 'key1' => Expect::anyOf(Expect::string(), Expect::int()),
  134.                 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int()),
  135.                 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int()),
  136.         ]);
  137.  
  138.         checkValidationErrors(function () use ($schema) {
  139.                 (new Processor)->process($schema, ['key1' => null, 'key2' => null, 'key3' => null]);
  140.         }, [
  141.                 "The item 'key1' expects to be string|int, null given.",
  142.                 "The item 'key2' expects to be string|true|int, null given.",
  143.                 "The item 'key3' expects to be true|string|int, null given.",
  144.         ]);
  145. });
  146.  
  147.  
  148. test('required & nullable', function () {
  149.         $schema = Expect::structure([
  150.                 'key1' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  151.                 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int(), null)->required(),
  152.                 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  153.         ]);
  154.  
  155.         assert::equal(
  156.                 (object) ['key1' => null, 'key2' => null, 'key3' => null],
  157.                 (new Processor)->process($schema, ['key1' => null, 'key2' => null, 'key3' => null])
  158.         );
  159. });
  160.  
  161.  
  162. test('deprecated item', function () {
  163.         $schema = Expect::anyOf('one', true, Expect::int()->deprecated());
  164.  
  165.         $processor = new Processor;
  166.         assert::same('one', $processor->process($schema, 'one'));
  167.         assert::same([], $processor->getWarnings());
  168.  
  169.         assert::same(true, $processor->process($schema, true));
  170.         assert::same([], $processor->getWarnings());
  171.  
  172.         assert::same(123, $processor->process($schema, 123));
  173.         assert::same(['The item is deprecated.'], $processor->getWarnings());
  174. });
  175.  
  176.  
  177. test('nullable anyOf', function () {
  178.         $schema = Expect::anyOf(Expect::string(), true)->nullable();
  179.  
  180.         assert::same('one', (new Processor)->process($schema, 'one'));
  181.  
  182.         assert::same(null, (new Processor)->process($schema, null));
  183.  
  184.         checkValidationErrors(function () use ($schema) {
  185.                 (new Processor)->process($schema, false);
  186.         }, ['The item expects to be string|true|null, false given.']);
  187. });
  188.  
  189.  
  190. test('processing', function () {
  191.         $schema = Expect::anyOf(Expect::string(), true)->nullable();
  192.         $processor = new Processor;
  193.  
  194.         assert::same('one', $processor->process($schema, 'one'));
  195.         assert::same(null, $processor->process($schema, null));
  196.  
  197.         checkValidationErrors(function () use ($schema, $processor) {
  198.                 $processor->process($schema, false);
  199.         }, ['The item expects to be string|true|null, false given.']);
  200.  
  201.  
  202.         assert::same('two', $processor->processMultiple($schema, ['one', 'two']));
  203.         assert::same(null, $processor->processMultiple($schema, ['one', null]));
  204. });
  205.  
  206.  
  207. test('Schema as default value', function () {
  208.         $default = Expect::structure([
  209.                 'key2' => Expect::string(),
  210.         ])->castTo('array');
  211.  
  212.         $schema = Expect::structure([
  213.                 'key1' => Expect::anyOf(false, $default)->default($default),
  214.         ])->castTo('array');
  215.  
  216.         assert::same(['key1' => ['key2' => null]], (new Processor)->process($schema, null));
  217. });
  218.  
  219.  
  220. test('First is default', function () {
  221.         $schema = Expect::structure([
  222.                 'key' => Expect::anyOf(
  223.                         Expect::string('def'),
  224.                         false
  225.                 )->firstIsDefault(),
  226.         ])->castTo('array');
  227.  
  228.         assert::same(['key' => 'def'], (new Processor)->process($schema, null));
  229. });
  230.  
  231.  
  232. test('Empty set', function () {
  233.         assert::exception(function () {
  234.                 Expect::anyOf();
  235.         }, NetteInvalidStateException::class, 'The enumeration must not be empty.');
  236. });
  237.  
  238.  
  239. test('normalization', function () {
  240.         $schema = Expect::anyOf(
  241.                 Expect::string()->before(function ($v) { return (string) $v; })
  242.         );
  243.         assert::same('1', (new Processor)->process($schema, 1));
  244. });
Parsed in 0.451 seconds