Skip to content

Commit 6359c0b

Browse files
committed
Add tests for RuntimeTypeChecker's cloneWith method and property validation
1 parent d23df3c commit 6359c0b

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

‎tests/Internal/RuntimeTypeCheckerTest.php‎

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use TypePHP\Internal\Checker\ParamChecker;
99
use TypePHP\Internal\Checker\SelfOutChecker;
1010
use TypePHP\Internal\Diagnostic\ErrorMessage;
11+
use TypePHP\Internal\Generics\TemplateManager;
1112
use TypePHP\Internal\RuntimeTypeChecker;
1213
use TypePHP\Internal\Util\Config;
1314
use TypePHP\Internal\Validator\TypeValidatorRegistry;
@@ -17,6 +18,10 @@
1718
use TypePHP\Tests\Fixtures\Services\UserService;
1819
use TypePHP\Tests\Fixtures\Types\ConfiguredProperty;
1920

21+
if (PHP_VERSION_ID >= 80500) {
22+
require_once __DIR__ . '/../Fixtures/Php85/CloneWithFunctionalFixtures.php';
23+
}
24+
2025
class StaticPropFixture
2126
{
2227
/**
@@ -559,4 +564,156 @@ function vendorAction(): void
559564

560565
expect(RuntimeTypeChecker::checkReturn('runtimeUnconstrainedReturn', 100))->toBe(100);
561566
});
567+
568+
test('returns cloned value untouched when cloned or source is not an object', function () {
569+
expect(RuntimeTypeChecker::cloneWith(123, new stdClass()))->toBe(123);
570+
expect(RuntimeTypeChecker::cloneWith('string', null))->toBe('string');
571+
expect(RuntimeTypeChecker::cloneWith(null, null))->toBeNull();
572+
573+
TemplateManager::$pendingCloneSource = null;
574+
$obj = new stdClass();
575+
expect(RuntimeTypeChecker::cloneWith($obj, null))->toBe($obj);
576+
expect(RuntimeTypeChecker::cloneWith($obj, 456))->toBe($obj);
577+
});
578+
579+
test('copies generic bindings using explicit original object argument', function () {
580+
$original = new Container(new Dog());
581+
TemplateManager::bindInstance($original, Container::class . '<' . Dog::class . '>');
582+
583+
$cloned = new Container(new Dog());
584+
$result = RuntimeTypeChecker::cloneWith($cloned, $original);
585+
586+
expect($result)->toBe($cloned)
587+
->and(TypePHP\TypePHP::getGenericType($cloned))->toBe(Dog::class)
588+
;
589+
});
590+
591+
test('falls back to TemplateManager::$pendingCloneSource and resets it to null', function () {
592+
$original = new Container(new Dog());
593+
TemplateManager::bindInstance($original, Container::class . '<' . Dog::class . '>');
594+
595+
TemplateManager::$pendingCloneSource = $original;
596+
597+
$cloned = new Container(new Dog());
598+
$result = RuntimeTypeChecker::cloneWith($cloned, original: null);
599+
600+
expect($result)->toBe($cloned)
601+
->and(TypePHP\TypePHP::getGenericType($cloned))->toBe(Dog::class)
602+
->and(TemplateManager::$pendingCloneSource)->toBeNull()
603+
;
604+
});
605+
606+
test('skips property validation when overriddenProperties is null or empty array', function () {
607+
$obj = new stdClass();
608+
609+
expect(RuntimeTypeChecker::cloneWith($obj, $obj, null))->toBe($obj)
610+
->and(RuntimeTypeChecker::cloneWith($obj, $obj, []))->toBe($obj)
611+
;
612+
});
613+
614+
test('validates valid property overrides on CloneWithModelFixture (PHP 8.5+)', function () {
615+
if (PHP_VERSION_ID < 80500) {
616+
expect(true)->toBeTrue();
617+
618+
return;
619+
}
620+
621+
$model = new TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture();
622+
$cloned = clone $model;
623+
624+
$result = RuntimeTypeChecker::cloneWith(
625+
$cloned,
626+
$model,
627+
['score' => 50, 'name' => 'Bob'],
628+
__FILE__,
629+
100
630+
);
631+
632+
expect($result)->toBe($cloned);
633+
});
634+
635+
test('throws TypeError when overridden property violates positive-int on CloneWithModelFixture (PHP 8.5+)', function () {
636+
if (PHP_VERSION_ID < 80500) {
637+
expect(true)->toBeTrue();
638+
639+
return;
640+
}
641+
642+
$model = new TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture();
643+
$cloned = clone $model;
644+
645+
expect(function () use ($cloned, $model) {
646+
RuntimeTypeChecker::cloneWith(
647+
$cloned,
648+
$model,
649+
['score' => -25],
650+
__FILE__,
651+
42
652+
);
653+
})->toThrow(function (TypeError $e) {
654+
expect($e->getMessage())->toContain('positive-int')
655+
->and($e->getFile())->toBe(__FILE__)
656+
->and($e->getLine())->toBe(42)
657+
;
658+
});
659+
});
660+
661+
test('throws TypeError when overridden property violates non-empty-string on CloneWithModelFixture (PHP 8.5+)', function () {
662+
if (PHP_VERSION_ID < 80500) {
663+
expect(true)->toBeTrue();
664+
665+
return;
666+
}
667+
668+
$model = new TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture();
669+
$cloned = clone $model;
670+
671+
expect(function () use ($cloned, $model) {
672+
RuntimeTypeChecker::cloneWith(
673+
$cloned,
674+
$model,
675+
['name' => ''],
676+
__FILE__,
677+
50
678+
);
679+
})->toThrow(TypeError::class, 'non-empty-string');
680+
});
681+
682+
test('validates overridden properties on CloneWithReadonlyOrder (PHP 8.5+)', function () {
683+
if (PHP_VERSION_ID < 80500) {
684+
expect(true)->toBeTrue();
685+
686+
return;
687+
}
688+
689+
$order = new TypePHP\Tests\Fixtures\Php85\CloneWithReadonlyOrder(100, 'SKU-ORIGINAL');
690+
$cloned = clone $order;
691+
692+
expect(function () use ($cloned, $order) {
693+
RuntimeTypeChecker::cloneWith(
694+
$cloned,
695+
$order,
696+
['id' => -999],
697+
__FILE__,
698+
60
699+
);
700+
})->toThrow(TypeError::class, 'positive-int');
701+
});
702+
703+
test('skips overridden property validation when inline properties are disabled in config (PHP 8.5+)', function () {
704+
if (PHP_VERSION_ID < 80500) {
705+
expect(true)->toBeTrue();
706+
707+
return;
708+
}
709+
710+
Config::set(['inline_vars' => ['properties' => false]]);
711+
712+
$model = new TypePHP\Tests\Fixtures\Php85\CloneWithModelFixture();
713+
$cloned = clone $model;
714+
715+
$result = RuntimeTypeChecker::cloneWith($cloned, $model, ['score' => -50]);
716+
717+
expect($result)->toBe($cloned);
718+
});
562719
});

0 commit comments

Comments
 (0)