Skip to content
Snippets Groups Projects

Issue #3315119: Implement a getter for mocked method in MockObject

2 files
+ 90
0
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 28
0
@@ -12,6 +12,8 @@ use Drupal\Core\Entity\Query\ConditionInterface as EntityQueryConditionInterface
@@ -12,6 +12,8 @@ use Drupal\Core\Entity\Query\ConditionInterface as EntityQueryConditionInterface
use Drupal\test_helpers\Stub\ModuleHandlerStub;
use Drupal\test_helpers\Stub\ModuleHandlerStub;
use Drupal\test_helpers\Stub\TokenStub;
use Drupal\test_helpers\Stub\TokenStub;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Assert;
 
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
 
use PHPUnit\Framework\MockObject\MethodNameNotConfiguredException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Yaml;
@@ -63,6 +65,32 @@ class UnitTestHelpers {
@@ -63,6 +65,32 @@ class UnitTestHelpers {
return $property->getValue($class);
return $property->getValue($class);
}
}
 
/**
 
* Gets a mocked method from the Mock object to replace return value.
 
*
 
* This allows to replace the return value of the already defined method via
 
* `$mockedMethod->willReturn('New Value')`.
 
*
 
* It's not possible with PHPUnit API, but here is a feature request about it:
 
* https://github.com/sebastianbergmann/phpunit/issues/5070 - please vote!
 
*/
 
public static function getMockedMethod(MockObject $mock, string $method) {
 
$invocationHandler = $mock->__phpunit_getInvocationHandler();
 
$configurableMethods = self::getProtectedProperty($invocationHandler, 'configurableMethods');
 
$matchers = self::getProtectedProperty($invocationHandler, 'matchers');
 
foreach ($matchers as $matcher) {
 
$methodNameRuleObject = self::getProtectedProperty($matcher, 'methodNameRule');
 
if ($methodNameRuleObject->matchesName($method)) {
 
return new InvocationMocker(
 
$invocationHandler,
 
$matcher,
 
...$configurableMethods
 
);
 
}
 
}
 
throw new MethodNameNotConfiguredException();
 
}
 
/**
/**
* Parses the annotation for a class and gets the definition.
* Parses the annotation for a class and gets the definition.
*/
*/
Loading