Skip to content

Commit df3fd82

Browse files
committed
fix failing test on 8.5
1 parent 30e5340 commit df3fd82

3 files changed

Lines changed: 68 additions & 49 deletions

File tree

‎tests/Fixtures/Php85/CloneWithFunctionalFixtures.php‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@
66

77
use TypePHP\Tests\Fixtures\Domain\Animal;
88

9+
/**
10+
* Executes native PHP 8.5 clone-with operations so test files don't fail parsing on PHP <= 8.4.
11+
*/
12+
class NativeCloneWithRunner
13+
{
14+
public static function clone(object $object, ?array $properties = null): object
15+
{
16+
if ($properties !== null) {
17+
return clone($object, $properties);
18+
}
19+
20+
return clone $object;
21+
}
22+
}
23+
24+
class CloneWithModelFixture
25+
{
26+
/**
27+
* @var positive-int
28+
*/
29+
public int $score = 10;
30+
31+
/**
32+
* @var non-empty-string
33+
*/
34+
public string $name = 'Alice';
35+
}
36+
37+
final readonly class CloneWithReadonlyOrder
38+
{
39+
/**
40+
* @var positive-int
41+
*/
42+
public int $id;
43+
44+
/**
45+
* @var non-empty-string
46+
*/
47+
public string $sku;
48+
49+
public function __construct(int $id, string $sku)
50+
{
51+
$this->id = $id;
52+
$this->sku = $sku;
53+
}
54+
55+
public function withId(int $id): self
56+
{
57+
return clone($this, ['id' => $id]);
58+
}
59+
}
60+
961
/**
1062
* @template T of Animal
1163
*/

‎tests/TypeChecking/Boundaries/FunctionalCloneWithTest.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use TypePHP\Tests\Fixtures\Php85\HookedCloneWithFixture;
1919
use TypePHP\Tests\Fixtures\Php85\MagicCloneWithFixture;
2020
use TypePHP\Tests\Fixtures\Php85\MultiTemplateMapFixture;
21+
use TypePHP\Tests\Fixtures\Php85\NativeCloneWithRunner;
2122
use TypePHP\Tests\Fixtures\Php85\Psr7ResponseWitherFixture;
2223
use TypePHP\Tests\Fixtures\Php85\ShapeCloneWithFixture;
2324
use TypePHP\TypePHP;
@@ -78,7 +79,7 @@
7879
test('throws TypeError when clone override violates generic value type V', function () {
7980
$map = new MultiTemplateMapFixture(['user_1' => 100]);
8081

81-
expect(fn () => clone($map, ['data' => ['user_1' => 'not_an_int']]))
82+
expect(fn () => NativeCloneWithRunner::clone($map, ['data' => ['user_1' => 'not_an_int']]))
8283
->toThrow(TypeError::class, "['user_1'] must be of type int")
8384
;
8485
});
@@ -122,8 +123,7 @@
122123
$model = new MagicCloneWithFixture();
123124
expect($model->counter)->toBe(10);
124125

125-
// In PHP 8.5 RFC: __clone() runs (setting counter to 999), then ['counter' => 50] overwrites it
126-
$cloned = clone($model, ['counter' => 50]);
126+
$cloned = NativeCloneWithRunner::clone($model, ['counter' => 50]);
127127

128128
expect($cloned->magicCloneExecuted)->toBeTrue()
129129
->and($cloned->counter)->toBe(50)
@@ -133,7 +133,7 @@
133133
test('validates final overridden state even when __clone() mutated the object first', function () {
134134
$model = new MagicCloneWithFixture();
135135

136-
expect(fn () => clone($model, ['counter' => -100]))
136+
expect(fn () => NativeCloneWithRunner::clone($model, ['counter' => -100]))
137137
->toThrow(TypeError::class, 'positive-int')
138138
;
139139
});
@@ -143,10 +143,10 @@
143143
test('triggers set hook during property write and enforces contract', function () {
144144
$order = new HookedCloneWithFixture();
145145

146-
$cloned = clone($order, ['total' => 250]);
146+
$cloned = NativeCloneWithRunner::clone($order, ['total' => 250]);
147147
expect($cloned->total)->toBe(250);
148148

149-
expect(fn () => clone($order, ['total' => -50]))
149+
expect(fn () => NativeCloneWithRunner::clone($order, ['total' => -50]))
150150
->toThrow(TypeError::class, 'positive-int')
151151
;
152152
});
@@ -156,7 +156,7 @@
156156
test('validates array shape properties on cloned instance', function () {
157157
$fixture = new ShapeCloneWithFixture();
158158

159-
$cloned = clone($fixture, [
159+
$cloned = NativeCloneWithRunner::clone($fixture, [
160160
'session' => ['id' => 42, 'role' => 'admin'],
161161
]);
162162

@@ -166,15 +166,15 @@
166166
test('throws TypeError when array shape id violates positive-int', function () {
167167
$fixture = new ShapeCloneWithFixture();
168168

169-
expect(fn () => clone($fixture, ['session' => ['id' => -1, 'role' => 'user']]))
169+
expect(fn () => NativeCloneWithRunner::clone($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 () => clone($fixture, ['session' => ['id' => 10, 'role' => 'superuser']]))
177+
expect(fn () => NativeCloneWithRunner::clone($fixture, ['session' => ['id' => 10, 'role' => 'superuser']]))
178178
->toThrow(TypeError::class, "['role'] must be of type ('admin' | 'user')")
179179
;
180180
});
@@ -206,7 +206,7 @@
206206
Config::set(['on_violation' => 'report']);
207207

208208
$user = new ShapeCloneWithFixture();
209-
$cloned = clone($user, ['session' => ['id' => -999, 'role' => 'user']]);
209+
$cloned = NativeCloneWithRunner::clone($user, ['session' => ['id' => -999, 'role' => 'user']]);
210210

211211
expect($cloned)->toBeInstanceOf(ShapeCloneWithFixture::class);
212212

‎tests/TypeChecking/Boundaries/Php85CloneWithTest.php‎

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,18 @@
88
return;
99
}
1010

11+
require_once __DIR__ . '/../../Fixtures/Php85/CloneWithFunctionalFixtures.php';
12+
1113
use TypePHP\Exception\TypeError;
1214
use TypePHP\Internal\Io\StreamWrapper;
1315
use TypePHP\Internal\RuntimeTypeChecker;
1416
use TypePHP\Tests\Fixtures\Domain\Car;
1517
use TypePHP\Tests\Fixtures\Domain\Dog;
1618
use TypePHP\Tests\Fixtures\Generics\GenericBox;
19+
use TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture;
20+
use TypePHP\Tests\Fixtures\Php85\CloneWithReadonlyOrder;
1721
use TypePHP\TypePHP;
1822

19-
class CloneWithModelFixture
20-
{
21-
/**
22-
* @var positive-int
23-
*/
24-
public int $score = 10;
25-
26-
/**
27-
* @var non-empty-string
28-
*/
29-
public string $name = 'Alice';
30-
}
31-
32-
final readonly class CloneWithReadonlyOrder
33-
{
34-
/**
35-
* @var positive-int
36-
*/
37-
public int $id;
38-
39-
/**
40-
* @var non-empty-string
41-
*/
42-
public string $sku;
43-
44-
public function __construct(int $id, string $sku)
45-
{
46-
$this->id = $id;
47-
$this->sku = $sku;
48-
}
49-
50-
public function withId(int $id): self
51-
{
52-
return clone($this, ['id' => $id]);
53-
}
54-
}
55-
5623
describe('PHP 8.5 "Clone With" Syntax Support (TDD)', function () {
5724
describe('1. AST Transformation (nikic/php-parser FuncCall interception)', function () {
5825
test('transforms PHP 8.5 clone($obj, [...]) function call into RuntimeTypeChecker::cloneWith wrapper', function () {
@@ -168,10 +135,10 @@ public function withId(int $id): self
168135
$source = <<<'PHP'
169136
<?php
170137
171-
$order = new \TypePHP\Tests\TypeChecking\Boundaries\CloneWithReadonlyOrder(42, 'SKU-VALID');
138+
$order = new \TypePHP\Tests\Fixtures\Php85\CloneWithReadonlyOrder(42, 'SKU-VALID');
172139
$validOrder = $order->withId(100);
173140
174-
$user = new \TypePHP\Tests\TypeChecking\Boundaries\CloneWithModelFixture();
141+
$user = new \TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture();
175142
$validUser = clone($user, ['score' => 50]);
176143
177144
$badUser = clone($user, ['score' => -10]);

0 commit comments

Comments
 (0)