File: Validators.assert%28%29.phpt

package info (click to toggle)
php-nette-utils 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,420 kB
  • sloc: php: 4,069; xml: 12; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 1,987 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

/**
 * Test: Nette\Utils\Validators::assert()
 */

declare(strict_types=1);

use Nette\Utils\Validators;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


Assert::exception(
	fn() => Validators::assert(true, 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, bool given.',
);

Assert::exception(
	fn() => Validators::assert('', 'int'),
	Nette\Utils\AssertionException::class,
	"The variable expects to be int, string '' given.",
);

Assert::exception(
	fn() => Validators::assert(str_repeat('x', 1000), 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, string given.',
);

Assert::exception(
	fn() => Validators::assert('1.0', 'int|float'),
	Nette\Utils\AssertionException::class,
	"The variable expects to be int or float, string '1.0' given.",
);

Assert::exception(
	fn() => Validators::assert(null, 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, null given.',
);

Assert::exception(
	fn() => Validators::assert(1.0, 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, float 1.0 given.',
);

Assert::exception(
	fn() => Validators::assert(1, 'float'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be float, int 1 given.',
);

Assert::exception(
	fn() => Validators::assert([], 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, array given.',
);

Assert::exception(
	fn() => Validators::assert(new stdClass, 'int'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be int, object stdClass given.',
);

Assert::exception(
	fn() => Validators::assert(1, 'string|integer:2..5', 'variable'),
	Nette\Utils\AssertionException::class,
	'The variable expects to be string or integer in range 2..5, int 1 given.',
);

Assert::exception(
	fn() => Validators::assert('x', '?int'),
	Nette\Utils\AssertionException::class,
	"The variable expects to be ?int, string 'x' given.",
);