Skip to content

Commit

Permalink
fix(StubFactory) more cross PHPUnit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Jul 30, 2024
1 parent 1d3b768 commit 190e197
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions tests/_support/StubClassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class StubClassFactory
{
public function __construct(%3$s)
{
$this->__phpunit_originalObject = %4$s::getPhpunitOriginalObject(%1$s");
$this->__phpunit_returnValueGeneration = %4$s::getPhpunitReturnValueGeneration("%1$s");
$this->__phpunit_invocationMocker = %4$s::getPhpunitInvocationMocker("%1$s");
%4$s::connectToStub($this, true);
%4$s::assertConstructorConditions("%1$s", func_get_args());
%4$s::setMockForClassName("%1$s", $this);
}
Expand All @@ -26,8 +24,7 @@ public function __construct(%3$s)
{
public function __construct(%3$s)
{
$this->__phpunit_returnValueGeneration = %4$s::getPhpunitReturnValueGeneration("%1$s");
$this->__phpunit_invocationMocker = %4$s::getPhpunitInvocationMocker("%1$s");
%4$s::connectToStub($this, false);
%4$s::assertConstructorConditions("%1$s", func_get_args());
%4$s::setMockForClassName("%1$s", $this);
}
Expand Down Expand Up @@ -56,6 +53,10 @@ public function __construct(%3$s)
* @var array<string,object>
*/
private static array $mockByClassName = [];
/**
* @var<string,array{0:string,1:array<string,mixed>}>
*/
private static array $stubParametersByClassName = [];

public static function setMockForClassName(string $mockClassName, object $mock): void
{
Expand All @@ -69,46 +70,23 @@ public static function tearDown(): void
self::$mockByClassName = [];
}

/**
* @throws ReflectionException
*/
public static function getPhpunitOriginalObject(string $mockClassName): object
{
$value = Property::readPrivate(self::$stubByClassName[$mockClassName], '__phpunit_originalObject');

if (!is_object($value)) {
throw new ReflectionException('No original object found for ' . $mockClassName);
}

return $value;
}

/**
* @throws ReflectionException
*/
public static function getPhpunitReturnValueGeneration(string $mockClassName): object
{
$value = Property::readPrivate(self::$stubByClassName[$mockClassName], '__phpunit_returnValueGeneration');

if (!is_object($value)) {
throw new ReflectionException('No return value generation found for ' . $mockClassName);
}

return $value;
}

/**
* @throws ReflectionException
*/
public static function getPhpunitInvocationMocker(string $mockClassName): object
{
$value = Property::readPrivate(self::$stubByClassName[$mockClassName], '__phpunit_invocationMocker');

if (!is_object($value)) {
throw new ReflectionException('No invocation mocker found for ' . $mockClassName);
public static function connectToStub(object $mock, bool $includeOriginalObject): void{
$mockClassName = get_class($mock);
[$class, $parameters] = self::$stubParametersByClassName[$mockClassName];
$stub = Stub::makeEmpty($class, $parameters);
if($includeOriginalObject){
Property::setPrivateProperties($mock, [
'__phpunit_originalObject' => Property::readPrivate($stub, '__phpunit_originalObject'),
'__phpunit_returnValueGeneration' => Property::readPrivate($stub, '__phpunit_returnValueGeneration'),
'__phpunit_invocationMocker' => Property::readPrivate($stub, '__phpunit_invocationMocker'),
]);
} else {
Property::setPrivateProperties($mock, [
'__phpunit_returnValueGeneration' => Property::readPrivate($stub, '__phpunit_returnValueGeneration'),
'__phpunit_invocationMocker' => Property::readPrivate($stub, '__phpunit_invocationMocker'),
]);
}

return $value;
unset($stub);
}

/**
Expand Down Expand Up @@ -195,6 +173,7 @@ public static function makeEmptyClass(string $class, array $parameters): string
eval($classCode);

self::$stubByClassName[$mockClassName] = $codeceptionStub;
self::$stubParametersByClassName[$mockClassName] = [$class, $parameters];

return $mockClassName;
}
Expand Down

0 comments on commit 190e197

Please sign in to comment.