Expect.list.phpt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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('without default value', function () {
  8. $schema = Expect::list();
  9. Assert::same([], (new Processor)->process($schema, []));
  10. Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c']));
  11. checkValidationErrors(function () use ($schema) {
  12. Assert::same(['key' => 'val'], (new Processor)->process($schema, ['key' => 'val']));
  13. }, ['The item expects to be list, array given.']);
  14. checkValidationErrors(function () use ($schema) {
  15. (new Processor)->process($schema, 'one');
  16. }, ["The item expects to be list, 'one' given."]);
  17. checkValidationErrors(function () use ($schema) {
  18. (new Processor)->process($schema, true);
  19. }, ['The item expects to be list, true given.']);
  20. checkValidationErrors(function () use ($schema) {
  21. (new Processor)->process($schema, 123);
  22. }, ['The item expects to be list, 123 given.']);
  23. Assert::same([], (new Processor)->process($schema, null));
  24. });
  25. test('not merging', function () {
  26. $schema = Expect::list([1, 2, 3])->mergeDefaults(false);
  27. Assert::same([], (new Processor)->process($schema, []));
  28. Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c']));
  29. Assert::same([], (new Processor)->process($schema, null));
  30. });
  31. test('merging', function () {
  32. $schema = Expect::list([1, 2, 3]);
  33. Assert::same([1, 2, 3], (new Processor)->process($schema, []));
  34. Assert::same([1, 2, 3, 'a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c']));
  35. Assert::same([1, 2, 3], (new Processor)->process($schema, null));
  36. });
  37. test('merging & other items validation', function () {
  38. $schema = Expect::list([1, 2, 3])->items('string');
  39. Assert::same([1, 2, 3], (new Processor)->process($schema, []));
  40. Assert::same([1, 2, 3, 'a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c']));
  41. checkValidationErrors(function () use ($schema) {
  42. (new Processor)->process($schema, [1, 2, 3]);
  43. }, [
  44. "The item '0' expects to be string, 1 given.",
  45. "The item '1' expects to be string, 2 given.",
  46. "The item '2' expects to be string, 3 given.",
  47. ]);
  48. Assert::same([1, 2, 3], (new Processor)->process($schema, null));
  49. });
  50. test('listOf() & scalar', function () {
  51. $schema = Expect::listOf('string');
  52. Assert::same([], (new Processor)->process($schema, []));
  53. checkValidationErrors(function () use ($schema) {
  54. (new Processor)->process($schema, [1, 2, 3]);
  55. }, [
  56. "The item '0' expects to be string, 1 given.",
  57. "The item '1' expects to be string, 2 given.",
  58. "The item '2' expects to be string, 3 given.",
  59. ]);
  60. Assert::same(['val', 'val'], (new Processor)->process($schema, ['val', 'val']));
  61. checkValidationErrors(function () use ($schema) {
  62. (new Processor)->process($schema, ['key' => 'val']);
  63. }, ['The item expects to be list, array given.']);
  64. });
  65. test('listOf() & error', function () {
  66. Assert::exception(function () {
  67. Expect::listOf(['a' => Expect::string()]);
  68. }, TypeError::class);
  69. });