1414namespace Zenstruck \Foundry \Tests \Unit \Persistence ;
1515
1616use PHPUnit \Framework \Attributes \DataProvider ;
17+ use PHPUnit \Framework \Attributes \IgnoreDeprecations ;
1718use PHPUnit \Framework \Attributes \Test ;
1819use PHPUnit \Framework \TestCase ;
1920use Zenstruck \Foundry \Persistence \ProxyGenerator ;
21+ use Zenstruck \Foundry \Test \Factories ;
2022
2123/**
2224 * @author Nicolas PHILIPPE <[email protected] > 25+ * @group legacy
2326 */
27+ #[IgnoreDeprecations]
2428final class ProxyGeneratorTest extends TestCase
2529{
30+ use Factories;
31+
2632 /**
2733 * @test
2834 * @dataProvider classWithUnserializeMagicMethodProvider
@@ -40,6 +46,37 @@ public static function classWithUnserializeMagicMethodProvider(): iterable
4046 yield 'not type hinted __unserialize method ' => [new ClassWithNoTypeHintInUnserialize ()];
4147 yield 'type hinted __unserialize method ' => [new ClassWithTypeHintedUnserialize ()];
4248 }
49+
50+ /**
51+ * @test
52+ */
53+ #[Test]
54+ public function it_can_generate_proxy_for_class_with_self_return_type (): void
55+ {
56+ $ proxyfiedObj = ProxyGenerator::wrap ($ obj = new ClassWithSelfReturnType ());
57+ self ::assertSame ($ obj , $ proxyfiedObj ->returnsSelf ()->_real ()); // @phpstan-ignore method.notFound
58+ }
59+
60+ /**
61+ * @test
62+ */
63+ #[Test]
64+ public function it_can_generate_proxy_for_class_with_method_with_nullable_return_type (): void
65+ {
66+ $ proxyfiedObj = ProxyGenerator::wrap (new ClassWithNullableReturnType ());
67+ self ::assertNull ($ proxyfiedObj ->returnsNullable (null ));
68+ self ::assertSame (1 , $ proxyfiedObj ->returnsNullable (1 ));
69+ }
70+
71+ /**
72+ * @test
73+ */
74+ #[Test]
75+ public function it_can_generate_proxy_for_class_with_method_with_no_return_type (): void
76+ {
77+ $ proxyfiedObj = ProxyGenerator::wrap (new ClassWithoutReturnType ());
78+ self ::assertSame (1 , $ proxyfiedObj ->returnsSeomthing ());
79+ }
4380}
4481
4582class ClassWithNoTypeHintInUnserialize
@@ -55,3 +92,27 @@ public function __unserialize(array $array)
5592 {
5693 }
5794}
95+
96+ class ClassWithNullableReturnType
97+ {
98+ public function returnsNullable (?int $ int ): ?int
99+ {
100+ return $ int ;
101+ }
102+ }
103+
104+ class ClassWithoutReturnType
105+ {
106+ public function returnsSeomthing () // @phpstan-ignore missingType.return
107+ {
108+ return 1 ;
109+ }
110+ }
111+
112+ class ClassWithSelfReturnType
113+ {
114+ public function returnsSelf (): self
115+ {
116+ return $ this ;
117+ }
118+ }
0 commit comments