2626describe ('Functional Type-Checking for PHP 8.5 "Clone With" ' , function () {
2727 describe ('1. Generic Bound Preservation & Validation (@template T of Animal) ' , function () {
2828 test ('preserves generic template T in WeakMap after wither method clone ' , function () {
29- $ dog = new Dog ();
29+ $ dog = new Dog (' Rover ' );
3030 $ box = new BoundedGenericBoxFixture ($ dog );
3131
3232 expect (TypePHP ::getGenericType ($ box ))->toBe (Dog::class);
3333
34- $ newDog = new Dog ();
34+ $ newDog = new Dog (' Max ' );
3535 $ clonedBox = $ box ->withItem ($ newDog );
3636
3737 expect ($ clonedBox )->not ()->toBe ($ box )
4141 });
4242
4343 test ('throws TypeError when wither method attempts to clone with incompatible type ' , function () {
44- $ box = new BoundedGenericBoxFixture (new Dog ());
44+ $ box = new BoundedGenericBoxFixture (new Dog (' Rover ' ));
4545
4646 expect (fn () => $ box ->withItem (new Car ()))
4747 ->toThrow (TypeError::class, 'must be of type ' . Dog::class)
4848 ;
4949 });
5050
5151 test ('validates external clone with on public generic property ' , function () {
52- $ box = new BoundedGenericBoxFixture (new Dog ());
52+ $ box = new BoundedGenericBoxFixture (new Dog (' Rover ' ));
5353
54- $ validClone = clone ($ box , ['item ' => new Dog ()]);
54+ $ validClone = NativeCloneWithRunner :: cloneWith ($ box , ['item ' => new Dog (' Buddy ' )]);
5555 expect (TypePHP ::getGenericType ($ validClone ))->toBe (Dog::class);
5656
57- expect (fn () => clone ($ box , ['item ' => new Car ()]))
57+ expect (fn () => NativeCloneWithRunner :: cloneWith ($ box , ['item ' => new Car ()]))
5858 ->toThrow (TypeError::class, 'must be of type ' . Dog::class)
5959 ;
6060 });
7979 test ('throws TypeError when clone override violates generic value type V ' , function () {
8080 $ map = new MultiTemplateMapFixture (['user_1 ' => 100 ]);
8181
82- expect (fn () => NativeCloneWithRunner ::clone ($ map , ['data ' => ['user_1 ' => 'not_an_int ' ]]))
82+ expect (fn () => NativeCloneWithRunner ::cloneWith ($ map , ['data ' => ['user_1 ' => 'not_an_int ' ]]))
8383 ->toThrow (TypeError::class, "['user_1'] must be of type int " )
8484 ;
8585 });
123123 $ model = new MagicCloneWithFixture ();
124124 expect ($ model ->counter )->toBe (10 );
125125
126- $ cloned = NativeCloneWithRunner ::clone ($ model , ['counter ' => 50 ]);
126+ $ cloned = NativeCloneWithRunner ::cloneWith ($ model , ['counter ' => 50 ]);
127127
128128 expect ($ cloned ->magicCloneExecuted )->toBeTrue ()
129129 ->and ($ cloned ->counter )->toBe (50 )
133133 test ('validates final overridden state even when __clone() mutated the object first ' , function () {
134134 $ model = new MagicCloneWithFixture ();
135135
136- expect (fn () => NativeCloneWithRunner ::clone ($ model , ['counter ' => -100 ]))
136+ expect (fn () => NativeCloneWithRunner ::cloneWith ($ model , ['counter ' => -100 ]))
137137 ->toThrow (TypeError::class, 'positive-int ' )
138138 ;
139139 });
143143 test ('triggers set hook during property write and enforces contract ' , function () {
144144 $ order = new HookedCloneWithFixture ();
145145
146- $ cloned = NativeCloneWithRunner ::clone ($ order , ['total ' => 250 ]);
146+ $ cloned = NativeCloneWithRunner ::cloneWith ($ order , ['total ' => 250 ]);
147147 expect ($ cloned ->total )->toBe (250 );
148148
149- expect (fn () => NativeCloneWithRunner ::clone ($ order , ['total ' => -50 ]))
149+ expect (fn () => NativeCloneWithRunner ::cloneWith ($ order , ['total ' => -50 ]))
150150 ->toThrow (TypeError::class, 'positive-int ' )
151151 ;
152152 });
156156 test ('validates array shape properties on cloned instance ' , function () {
157157 $ fixture = new ShapeCloneWithFixture ();
158158
159- $ cloned = NativeCloneWithRunner ::clone ($ fixture , [
159+ $ cloned = NativeCloneWithRunner ::cloneWith ($ fixture , [
160160 'session ' => ['id ' => 42 , 'role ' => 'admin ' ],
161161 ]);
162162
166166 test ('throws TypeError when array shape id violates positive-int ' , function () {
167167 $ fixture = new ShapeCloneWithFixture ();
168168
169- expect (fn () => NativeCloneWithRunner ::clone ($ fixture , ['session ' => ['id ' => -1 , 'role ' => 'user ' ]]))
169+ expect (fn () => NativeCloneWithRunner ::cloneWith ($ fixture , ['session ' => ['id ' => -1 , 'role ' => 'user ' ]]))
170170 ->toThrow (TypeError::class, "['id'] must be of type positive-int " )
171171 ;
172172 });
173173
174174 test ('throws TypeError when array shape role violates literal union ' , function () {
175175 $ fixture = new ShapeCloneWithFixture ();
176176
177- expect (fn () => NativeCloneWithRunner ::clone ($ fixture , ['session ' => ['id ' => 10 , 'role ' => 'superuser ' ]]))
177+ expect (fn () => NativeCloneWithRunner ::cloneWith ($ fixture , ['session ' => ['id ' => 10 , 'role ' => 'superuser ' ]]))
178178 ->toThrow (TypeError::class, "['role'] must be of type ('admin' | 'user') " )
179179 ;
180180 });
206206 Config ::set (['on_violation ' => 'report ' ]);
207207
208208 $ user = new ShapeCloneWithFixture ();
209- $ cloned = NativeCloneWithRunner ::clone ($ user , ['session ' => ['id ' => -999 , 'role ' => 'user ' ]]);
209+ $ cloned = NativeCloneWithRunner ::cloneWith ($ user , ['session ' => ['id ' => -999 , 'role ' => 'user ' ]]);
210210
211211 expect ($ cloned )->toBeInstanceOf (ShapeCloneWithFixture::class);
212212
217217 ;
218218 });
219219 });
220- });
220+ });
0 commit comments