Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for datetimeinterface as typehint #58

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}