| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- declare(strict_types=1);
- use Nette\Schema\Context;
- use Nette\Schema\Expect;
- use Tester\Assert;
- require __DIR__ . '/../bootstrap.php';
- test('', function () {
- $schema = Expect::array()
- ->before(function ($val) { return explode(',', $val); });
- Assert::same(
- ['1', '2', '3'],
- $schema->normalize('1,2,3', new Context)
- );
- });
- test('structure property', function () {
- $schema = Expect::structure([
- 'key' => Expect::string()->before('strrev'),
- ]);
- Assert::same(
- ['key' => '3,2,1'],
- $schema->normalize(['key' => '1,2,3'], new Context)
- );
- });
- test('order in structure', function () {
- $schema = Expect::structure([
- 'a' => Expect::string()->before(function ($val) use (&$order) {
- $order[] = 'a';
- return $val;
- }),
- 'b' => Expect::string()->before(function ($val) use (&$order) {
- $order[] = 'b';
- return $val;
- }),
- ])
- ->otherItems(Expect::string()->before(function ($val) use (&$order) {
- $order[] = 'other';
- return $val;
- }))
- ->before(function ($val) use (&$order) {
- $order[] = 'struct';
- return $val;
- });
- $order = [];
- Assert::null($schema->normalize(null, new Context));
- Assert::same(['struct'], $order);
- $order = [];
- Assert::same(['a' => 1], $schema->normalize(['a' => 1], new Context));
- Assert::same(['struct', 'a'], $order);
- $order = [];
- Assert::same(['a' => '1'], $schema->normalize(['a' => '1'], new Context));
- Assert::same(['struct', 'a'], $order);
- $order = [];
- Assert::same(['a' => 1, 'c' => 1], $schema->normalize(['a' => 1, 'c' => 1], new Context));
- Assert::same(['struct', 'a', 'other'], $order);
- $order = [];
- Assert::same(['c' => 1], $schema->normalize(['c' => 1], new Context));
- Assert::same(['struct', 'other'], $order);
- });
- test('order in array', function () {
- $schema = Expect::array()
- ->items(Expect::string()->before(function ($val) use (&$order) {
- $order[] = 'item';
- return $val;
- }))
- ->before(function ($val) use (&$order) {
- $order[] = 'array';
- return $val;
- });
- $order = [];
- Assert::null($schema->normalize(null, new Context));
- Assert::same(['array'], $order);
- $order = [];
- Assert::same(['a' => 1], $schema->normalize(['a' => 1], new Context));
- Assert::same(['array', 'item'], $order);
- });
|