@@ -22,76 +22,70 @@ Assert::with(Structure::class, function () {
2222
2323Assert::with (Structure::class, function () {
2424 $ schema = Expect::from ($ obj = new class {
25- /** @var string */
26- public $ dsn = 'mysql ' ;
27-
28- /** @var string|null */
29- public $ user ;
30-
31- /** @var ?string */
32- public $ password ;
33-
34- /** @var string[] */
35- public $ options = [1 ];
36-
37- /** @var bool */
38- public $ debugger = true ;
39- public $ mixed ;
40-
41- /** @var array|null */
42- public $ arr ;
43-
44- /** @var string */
45- public $ required ;
25+ public string $ dsn = 'mysql ' ;
26+ public ?string $ user ;
27+ public ?string $ password = null ;
28+ public array |int $ options = [];
29+ public bool $ debugger = true ;
30+ public mixed $ mixed ;
31+ public array $ arr = [1 ];
4632 });
4733
4834 Assert::type (Structure::class, $ schema );
4935 Assert::equal ([
5036 'dsn ' => Expect::string ('mysql ' ),
51- 'user ' => Expect::type ('string|null ' ),
37+ 'user ' => Expect::type ('? string' )-> required ( ),
5238 'password ' => Expect::type ('?string ' ),
53- 'options ' => Expect::type ('string[] ' )->default ([1 ]),
39+ 'options ' => Expect::type ('array|int ' )->default ([]),
5440 'debugger ' => Expect::bool (true ),
55- 'mixed ' => Expect::mixed (),
56- 'arr ' => Expect::type ('array|null ' )->default (null ),
57- 'required ' => Expect::type ('string ' )->required (),
41+ 'mixed ' => Expect::mixed ()->required (),
42+ 'arr ' => Expect::type ('array ' )->default ([1 ]),
5843 ], $ schema ->items );
59- Assert::type ($ obj , (new Processor )->process ($ schema , ['required ' => '' ]));
44+ Assert::type ($ obj , (new Processor )->process ($ schema , ['user ' => '' , ' mixed ' => '' ]));
6045});
6146
6247
63- Assert::exception (function () {
64- Expect::from (new class {
65- /** @var Unknown */
66- public $ unknown ;
48+ Assert::with (Structure::class, function () { // constructor injection
49+ $ schema = Expect::from ($ obj = new class ('' ) {
50+ public function __construct (
51+ public ?string $ user ,
52+ public ?string $ password = null ,
53+ ) {
54+ }
6755 });
68- }, Nette \NotImplementedException::class, 'Anonymous classes are not supported. ' );
56+
57+ Assert::type (Structure::class, $ schema );
58+ Assert::equal ([
59+ 'user ' => Expect::type ('?string ' )->required (),
60+ 'password ' => Expect::type ('?string ' ),
61+ ], $ schema ->items );
62+ Assert::equal (
63+ new $ obj ('foo ' , 'bar ' ),
64+ (new Processor )->process ($ schema , ['user ' => 'foo ' , 'password ' => 'bar ' ]),
65+ );
66+ });
6967
7068
7169Assert::with (Structure::class, function () { // overwritten item
7270 $ schema = Expect::from (new class {
73- /** @var string */
74- public $ dsn = 'mysql ' ;
71+ public string $ dsn = 'mysql ' ;
7572
76- /** @var string|null */
77- public $ user ;
73+ public ?string $ user ;
7874 }, ['dsn ' => Expect::int (123 )]);
7975
8076 Assert::equal ([
8177 'dsn ' => Expect::int (123 ),
82- 'user ' => Expect::type ('string|null ' ),
78+ 'user ' => Expect::type ('? string' )-> required ( ),
8379 ], $ schema ->items );
8480});
8581
8682
8783Assert::with (Structure::class, function () { // nested object
8884 $ obj = new class {
89- /** @var object */
90- public $ inner ;
85+ public object $ inner ;
9186 };
9287 $ obj ->inner = new class {
93- /** @var string */
94- public $ name ;
88+ public string $ name ;
9589 };
9690
9791 $ schema = Expect::from ($ obj );
0 commit comments