Expect.anyOf.phpt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. declare(strict_types=1);
  3. use Nette\Schema\Expect;
  4. use Nette\Schema\Processor;
  5. use Tester\Assert;
  6. require __DIR__ . '/../bootstrap.php';
  7. test('with scalars', function () {
  8. $schema = Expect::anyOf('one', true, Expect::int());
  9. Assert::same('one', (new Processor)->process($schema, 'one'));
  10. Assert::same(true, (new Processor)->process($schema, true));
  11. Assert::same(123, (new Processor)->process($schema, 123));
  12. checkValidationErrors(function () use ($schema) {
  13. (new Processor)->process($schema, false);
  14. }, ["The item expects to be 'one'|true|int, false given."]);
  15. checkValidationErrors(function () use ($schema) {
  16. (new Processor)->process($schema, 'two');
  17. }, ["The item expects to be 'one'|true|int, 'two' given."]);
  18. checkValidationErrors(function () use ($schema) {
  19. (new Processor)->process($schema, null);
  20. }, ["The item expects to be 'one'|true|int, null given."]);
  21. checkValidationErrors(function () use ($schema) {
  22. (new Processor)->process($schema, []);
  23. }, ["The item expects to be 'one'|true|int, array given."]);
  24. });
  25. test('with complex structure', function () {
  26. $schema = Expect::anyOf(Expect::listOf('string'), true, Expect::int());
  27. checkValidationErrors(function () use ($schema) {
  28. (new Processor)->process($schema, false);
  29. }, ['The item expects to be list|true|int, false given.']);
  30. checkValidationErrors(function () use ($schema) {
  31. (new Processor)->process($schema, [123]);
  32. }, ["The item '0' expects to be string, 123 given."]);
  33. Assert::same(['foo'], (new Processor)->process($schema, ['foo']));
  34. });
  35. test('with asserts', function () {
  36. $schema = Expect::anyOf(Expect::string()->assert('strlen'), true);
  37. checkValidationErrors(function () use ($schema) {
  38. (new Processor)->process($schema, '');
  39. }, ["Failed assertion strlen() for item with value ''."]);
  40. checkValidationErrors(function () use ($schema) {
  41. (new Processor)->process($schema, 123);
  42. }, ['The item expects to be string|true, 123 given.']);
  43. Assert::same('foo', (new Processor)->process($schema, 'foo'));
  44. });
  45. test('no default value', function () {
  46. $schema = Expect::structure([
  47. 'key1' => Expect::anyOf(Expect::string(), Expect::int()),
  48. 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int()),
  49. 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int()),
  50. ]);
  51. Assert::equal(
  52. (object) ['key1' => null, 'key2' => null, 'key3' => null],
  53. (new Processor)->process($schema, [])
  54. );
  55. });
  56. test('required', function () {
  57. $schema = Expect::structure([
  58. 'key1' => Expect::anyOf(Expect::string(), Expect::int())->required(),
  59. 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(),
  60. 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(),
  61. 'key4' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  62. 'key5' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  63. ]);
  64. checkValidationErrors(function () use ($schema) {
  65. (new Processor)->process($schema, []);
  66. }, [
  67. "The mandatory item 'key1' is missing.",
  68. "The mandatory item 'key2' is missing.",
  69. "The mandatory item 'key3' is missing.",
  70. "The mandatory item 'key4' is missing.",
  71. "The mandatory item 'key5' is missing.",
  72. ]);
  73. });
  74. test('required as argument', function () {
  75. $schema = Expect::structure([
  76. 'key1' => Expect::anyOf(Expect::string(), Expect::int())->required(),
  77. 'key1nr' => Expect::anyOf(Expect::string(), Expect::int())->required(false),
  78. 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(),
  79. 'key2nr' => Expect::anyOf(Expect::string('default'), true, Expect::int())->required(false),
  80. 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(),
  81. 'key3nr' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(false),
  82. 'key4' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  83. 'key4nr' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(false),
  84. 'key5' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  85. 'key5nr' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required(false)->nullable(),
  86. ]);
  87. checkValidationErrors(function () use ($schema) {
  88. (new Processor)->process($schema, []);
  89. }, [
  90. "The mandatory item 'key1' is missing.",
  91. "The mandatory item 'key2' is missing.",
  92. "The mandatory item 'key3' is missing.",
  93. "The mandatory item 'key4' is missing.",
  94. "The mandatory item 'key5' is missing.",
  95. ]);
  96. });
  97. test('not nullable', function () {
  98. $schema = Expect::structure([
  99. 'key1' => Expect::anyOf(Expect::string(), Expect::int()),
  100. 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int()),
  101. 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int()),
  102. ]);
  103. checkValidationErrors(function () use ($schema) {
  104. (new Processor)->process($schema, ['key1' => null, 'key2' => null, 'key3' => null]);
  105. }, [
  106. "The item 'key1' expects to be string|int, null given.",
  107. "The item 'key2' expects to be string|true|int, null given.",
  108. "The item 'key3' expects to be true|string|int, null given.",
  109. ]);
  110. });
  111. test('required & nullable', function () {
  112. $schema = Expect::structure([
  113. 'key1' => Expect::anyOf(Expect::string()->nullable(), Expect::int())->required(),
  114. 'key2' => Expect::anyOf(Expect::string('default'), true, Expect::int(), null)->required(),
  115. 'key3' => Expect::anyOf(true, Expect::string('default'), Expect::int())->required()->nullable(),
  116. ]);
  117. Assert::equal(
  118. (object) ['key1' => null, 'key2' => null, 'key3' => null],
  119. (new Processor)->process($schema, ['key1' => null, 'key2' => null, 'key3' => null])
  120. );
  121. });
  122. test('deprecated item', function () {
  123. $schema = Expect::anyOf('one', true, Expect::int()->deprecated());
  124. $processor = new Processor;
  125. Assert::same('one', $processor->process($schema, 'one'));
  126. Assert::same([], $processor->getWarnings());
  127. Assert::same(true, $processor->process($schema, true));
  128. Assert::same([], $processor->getWarnings());
  129. Assert::same(123, $processor->process($schema, 123));
  130. Assert::same(['The item is deprecated.'], $processor->getWarnings());
  131. });
  132. test('nullable anyOf', function () {
  133. $schema = Expect::anyOf(Expect::string(), true)->nullable();
  134. Assert::same('one', (new Processor)->process($schema, 'one'));
  135. Assert::same(null, (new Processor)->process($schema, null));
  136. checkValidationErrors(function () use ($schema) {
  137. (new Processor)->process($schema, false);
  138. }, ['The item expects to be string|true|null, false given.']);
  139. });
  140. test('processing', function () {
  141. $schema = Expect::anyOf(Expect::string(), true)->nullable();
  142. $processor = new Processor;
  143. Assert::same('one', $processor->process($schema, 'one'));
  144. Assert::same(null, $processor->process($schema, null));
  145. checkValidationErrors(function () use ($schema, $processor) {
  146. $processor->process($schema, false);
  147. }, ['The item expects to be string|true|null, false given.']);
  148. Assert::same('two', $processor->processMultiple($schema, ['one', 'two']));
  149. Assert::same(null, $processor->processMultiple($schema, ['one', null]));
  150. });
  151. test('Schema as default value', function () {
  152. $default = Expect::structure([
  153. 'key2' => Expect::string(),
  154. ])->castTo('array');
  155. $schema = Expect::structure([
  156. 'key1' => Expect::anyOf(false, $default)->default($default),
  157. ])->castTo('array');
  158. Assert::same(['key1' => ['key2' => null]], (new Processor)->process($schema, null));
  159. });
  160. test('First is default', function () {
  161. $schema = Expect::structure([
  162. 'key' => Expect::anyOf(
  163. Expect::string('def'),
  164. false
  165. )->firstIsDefault(),
  166. ])->castTo('array');
  167. Assert::same(['key' => 'def'], (new Processor)->process($schema, null));
  168. });
  169. test('Empty set', function () {
  170. Assert::exception(function () {
  171. Expect::anyOf();
  172. }, Nette\InvalidStateException::class, 'The enumeration must not be empty.');
  173. });
  174. test('normalization', function () {
  175. $schema = Expect::anyOf(
  176. Expect::string()->before(function ($v) { return (string) $v; })
  177. );
  178. Assert::same('1', (new Processor)->process($schema, 1));
  179. });