Expect.assert.phpt 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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('single assertion', function () {
  8. $schema = Expect::string()->assert('is_file');
  9. checkValidationErrors(function () use ($schema) {
  10. (new Processor)->process($schema, 'hello');
  11. }, ["Failed assertion is_file() for item with value 'hello'."]);
  12. Assert::same(__FILE__, (new Processor)->process($schema, __FILE__));
  13. });
  14. test('multiple assertions', function () {
  15. $schema = Expect::string()->assert('ctype_digit')->assert(function ($s) { return strlen($s) >= 3; });
  16. checkValidationErrors(function () use ($schema) {
  17. (new Processor)->process($schema, '');
  18. }, ["Failed assertion ctype_digit() for item with value ''."]);
  19. checkValidationErrors(function () use ($schema) {
  20. (new Processor)->process($schema, '1');
  21. }, ["Failed assertion #1 for item with value '1'."]);
  22. Assert::same('123', (new Processor)->process($schema, '123'));
  23. });
  24. test('multiple assertions with custom descriptions', function () {
  25. $schema = Expect::string()
  26. ->assert('ctype_digit', 'Is number')
  27. ->assert(function ($s) { return strlen($s) >= 3; }, 'Minimal lenght');
  28. checkValidationErrors(function () use ($schema) {
  29. (new Processor)->process($schema, '');
  30. }, ["Failed assertion 'Is number' for item with value ''."]);
  31. checkValidationErrors(function () use ($schema) {
  32. (new Processor)->process($schema, '1');
  33. }, ["Failed assertion 'Minimal lenght' for item with value '1'."]);
  34. Assert::same('123', (new Processor)->process($schema, '123'));
  35. });