Skip to content

Commit

Permalink
Add support for datetimeinterface as typehint (#58)
Browse files Browse the repository at this point in the history
- It's not allowed to create your own class implementing this interface
  • Loading branch information
bram123 authored Jul 15, 2024
1 parent 897e05a commit a1be063
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Constraint/ValueProvider/Compound/InstanceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace DigitalRevolution\AccessorPairConstraint\Tests\Integration\data\success\Regular\Types\CompoundTypes;

use DateTimeInterface;

class InstanceDateTimeInterfaceProperty
{
/** @var DateTimeInterface */
private $property;

public function getProperty(): DateTimeInterface
{
return $this->property;
}

public function setProperty(DateTimeInterface $property): self
{
$this->property = $property;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@ class InstanceProviderTest extends AbstractValueProviderTestCase
/**
* @covers ::__construct
* @covers ::getValues
* @covers ::getMockObject
* @throws Exception
*/
public function testGetValues(): void
Expand All @@ -36,6 +38,7 @@ public function testGetValues(): void
* When the requested class has a constructor requirement
* @covers ::__construct
* @covers ::getValues
* @covers ::getMockObject
*
* @throws Exception
*/
Expand Down Expand Up @@ -66,6 +69,7 @@ public function testGetValuesError(): void
*
* @covers ::__construct
* @covers ::getValues
* @covers ::getMockObject
* @throws Exception
* @requires PHP >= 8.1
*/
Expand All @@ -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);
}
}
}

0 comments on commit a1be063

Please sign in to comment.