diff --git a/src/Constraint/ValueProvider/Compound/InstanceProvider.php b/src/Constraint/ValueProvider/Compound/InstanceProvider.php index 5f881f2..3562cbb 100644 --- a/src/Constraint/ValueProvider/Compound/InstanceProvider.php +++ b/src/Constraint/ValueProvider/Compound/InstanceProvider.php @@ -3,6 +3,9 @@ namespace DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Compound; +use DateTime; +use DateTimeImmutable; +use DateTimeInterface; use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\ValueProvider; use LogicException; use PHPUnit\Framework\MockObject\Generator\Generator; @@ -35,23 +38,33 @@ public function getValues(): array return $enum::cases(); } + // It's not allowed to make mocks of the DateTimeInterface class itself + if ($this->typehint === DateTimeInterface::class) { + return [$this->getMockObject(DateTime::class), $this->getMockObject(DateTimeImmutable::class)]; + } + + return [$this->getMockObject($this->typehint)]; + } + + private function getMockObject(string $typehint): object + { if (class_exists('PHPUnit\Framework\MockObject\Generator\Generator')) { /** @var \PHPUnit\Framework\MockObject\Generator $mockGenerator */ $mockGenerator = new Generator(); if (method_exists($mockGenerator, 'testDouble')) { if (method_exists(Version::class, 'majorVersionNumber') && Version::majorVersionNumber() >= 11) { - $instance = $mockGenerator->testDouble($this->typehint, true, true, [], [], '', false); + $instance = $mockGenerator->testDouble($typehint, true, true, [], [], '', false); } else { - $instance = $mockGenerator->testDouble($this->typehint, true, [], [], '', false); + $instance = $mockGenerator->testDouble($typehint, true, [], [], '', false); } } else { - $instance = $mockGenerator->getMock($this->typehint, [], [], '', false); + $instance = $mockGenerator->getMock($typehint, [], [], '', false); } } else { $mockGenerator = new \PHPUnit\Framework\MockObject\Generator(); - $instance = $mockGenerator->getMock($this->typehint, [], [], '', false); + $instance = $mockGenerator->getMock($typehint, [], [], '', false); } - return [$instance]; + return $instance; } } diff --git a/tests/Integration/data/success/Regular/Types/CompoundTypes/InstanceDateTimeInterfaceProperty.php b/tests/Integration/data/success/Regular/Types/CompoundTypes/InstanceDateTimeInterfaceProperty.php new file mode 100644 index 0000000..01c784c --- /dev/null +++ b/tests/Integration/data/success/Regular/Types/CompoundTypes/InstanceDateTimeInterfaceProperty.php @@ -0,0 +1,24 @@ +property; + } + + public function setProperty(DateTimeInterface $property): self + { + $this->property = $property; + + return $this; + } +} diff --git a/tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php b/tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php index a1b912a..405a323 100644 --- a/tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php +++ b/tests/Unit/Constraint/ValueProvider/Compound/InstanceProviderTest.php @@ -3,6 +3,7 @@ namespace DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\Compound; +use DateTimeInterface; use DigitalRevolution\AccessorPairConstraint\Constraint\ValueProvider\Compound\InstanceProvider; use DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\TestEnum; use DigitalRevolution\AccessorPairConstraint\Tests\Unit\Constraint\ValueProvider\AbstractValueProviderTestCase; @@ -18,6 +19,7 @@ class InstanceProviderTest extends AbstractValueProviderTestCase /** * @covers ::__construct * @covers ::getValues + * @covers ::getMockObject * @throws Exception */ public function testGetValues(): void @@ -36,6 +38,7 @@ public function testGetValues(): void * When the requested class has a constructor requirement * @covers ::__construct * @covers ::getValues + * @covers ::getMockObject * * @throws Exception */ @@ -66,6 +69,7 @@ public function testGetValuesError(): void * * @covers ::__construct * @covers ::getValues + * @covers ::getMockObject * @throws Exception * @requires PHP >= 8.1 */ @@ -79,4 +83,23 @@ public function testGetEnumValues(): void static::assertInstanceOf(TestEnum::class, $value); } } + + /** + * Test getting test cases from a DateTimeInterface + * + * @covers ::__construct + * @covers ::getValues + * @covers ::getMockObject + * @throws Exception + */ + public function testGetDateTimeInterfaceValues(): void + { + $valueProvider = new InstanceProvider(DateTimeInterface::class); + $values = $valueProvider->getValues(); + + static::assertNotEmpty($values); + foreach ($values as $value) { + static::assertInstanceOf(DateTimeInterface::class, $value); + } + } }