From 3e0f2a0346085427ae0e82e7436a05cae3dd2b77 Mon Sep 17 00:00:00 2001 From: Exanlv <51094537+Exanlv@users.noreply.github.com> Date: Tue, 14 Jul 2026 20:16:06 +0200 Subject: [PATCH 1/2] Return default empty value for generator return type --- src/MockedClass.php | 5 +++++ tests/Components/TestDefaultReturns.php | 6 ++++++ tests/Documentation/DefaultReturnValuesTest.php | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/MockedClass.php b/src/MockedClass.php index 44775a0..70e1bc4 100644 --- a/src/MockedClass.php +++ b/src/MockedClass.php @@ -12,6 +12,7 @@ use Exan\Moock\Dto\MethodCall; use Exan\Moock\Expector\MockExpector; use Exan\Moock\Properties\MockPropertyValue; +use Generator; use ReflectionClass; use ReflectionMethod; use ReflectionNamedType; @@ -161,6 +162,10 @@ private function getDefault(string $method): mixed DateTimeImmutable::class => fn () => new DateTimeImmutable(), DateInterval::class => fn () => DateInterval::createFromDateString('1 day'), DatePeriod::class => fn () => DatePeriod::createFromISO8601String('R1337/2026-02-24T00:00:00Z/P1Y'), + + Generator::class => function (): Generator { + yield from []; + } ]; if (isset($returns[$plainType])) { diff --git a/tests/Components/TestDefaultReturns.php b/tests/Components/TestDefaultReturns.php index e87debd..e96a8b2 100644 --- a/tests/Components/TestDefaultReturns.php +++ b/tests/Components/TestDefaultReturns.php @@ -8,6 +8,7 @@ use DatePeriod; use DateTime; use DateTimeImmutable; +use Generator; use stdClass; class TestDefaultReturns @@ -126,4 +127,9 @@ public function returnUserServiceInterface(): UserServiceInterface { return new UserService(); } + + public function returnGenerator(): Generator + { + yield 'value'; + } } diff --git a/tests/Documentation/DefaultReturnValuesTest.php b/tests/Documentation/DefaultReturnValuesTest.php index b874cbf..29ca724 100644 --- a/tests/Documentation/DefaultReturnValuesTest.php +++ b/tests/Documentation/DefaultReturnValuesTest.php @@ -52,6 +52,8 @@ public function it_returns_hardcoded_values_for_basic_types(): void $this->assertInstanceOf(DateTimeImmutable::class, $mock->returnDateTimeImmutable()); $this->assertInstanceOf(DateInterval::class, $mock->returnDateInterval()); $this->assertInstanceOf(DatePeriod::class, $mock->returnDatePeriod()); + + $this->assertEmpty(iterator_to_array($mock->returnGenerator())); } #[Example('Returning mocked objects', 'If a method has a return type of an object, a mocked instance will be returned. It will return the same mock each time, so it can be used for assertions too.')] From 0d6f0d890f47379eef8e2857711081d10fc5dd7b Mon Sep 17 00:00:00 2001 From: Exanlv <51094537+Exanlv@users.noreply.github.com> Date: Tue, 14 Jul 2026 20:17:14 +0200 Subject: [PATCH 2/2] Move assertion to better place --- tests/Documentation/DefaultReturnValuesTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Documentation/DefaultReturnValuesTest.php b/tests/Documentation/DefaultReturnValuesTest.php index 29ca724..fd98ab1 100644 --- a/tests/Documentation/DefaultReturnValuesTest.php +++ b/tests/Documentation/DefaultReturnValuesTest.php @@ -35,6 +35,7 @@ public function it_returns_hardcoded_values_for_basic_types(): void $this->assertEquals([], $mock->returnArray()); $this->assertEquals([], $mock->returnIterable()); + $this->assertEmpty(iterator_to_array($mock->returnGenerator())); // These objects do not have any properties $this->assertInstanceOf(stdClass::class, $mock->returnObject()); @@ -52,8 +53,6 @@ public function it_returns_hardcoded_values_for_basic_types(): void $this->assertInstanceOf(DateTimeImmutable::class, $mock->returnDateTimeImmutable()); $this->assertInstanceOf(DateInterval::class, $mock->returnDateInterval()); $this->assertInstanceOf(DatePeriod::class, $mock->returnDatePeriod()); - - $this->assertEmpty(iterator_to_array($mock->returnGenerator())); } #[Example('Returning mocked objects', 'If a method has a return type of an object, a mocked instance will be returned. It will return the same mock each time, so it can be used for assertions too.')]