Skip to content

Commit

Permalink
Fixed deprecations in test traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Nov 18, 2024
1 parent b310012 commit c273690
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions web/modules/custom/ys_core/tests/src/Traits/MockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait MockTrait {
*
* @param string $class
* Class name to generate the mock.
* @param array<string,mixed> $methodsMap
* @param array<string,mixed> $methods_map
* Optional array of methods and values, keyed by method name.
* @param array<string,mixed>|bool $args
* Optional array of constructor arguments. If omitted, a constructor will
Expand All @@ -31,34 +31,23 @@ trait MockTrait {
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ElseExpression)
*/
protected function prepareMock(string $class, array $methodsMap = [], array|bool $args = []): MockObject {
$methods = array_keys($methodsMap);
protected function prepareMock(string $class, array $methods_map = [], array|bool $args = []): MockObject {
$methods = array_filter(array_keys($methods_map));

if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class %s does not exist', $class));
}

$reflection_class = new \ReflectionClass($class);

if ($reflection_class->isAbstract()) {
$mock = $this->getMockForAbstractClass(
$class, is_array($args) ? $args : [], '', !empty($args), TRUE, TRUE, $methods
);
$mock = $this->getMockBuilder($class);
if (is_array($args) && !empty($args)) {
$mock = $mock->enableOriginalConstructor()->setConstructorArgs($args);
}
else {
$mock = $this->getMockBuilder($class);
if (is_array($args) && !empty($args)) {
$mock = $mock->enableOriginalConstructor()
->setConstructorArgs($args);
}
elseif ($args === FALSE) {
$mock = $mock->disableOriginalConstructor();
}
$mock = $mock->onlyMethods($methods)
->getMock();
elseif ($args === FALSE) {
$mock = $mock->disableOriginalConstructor();
}
$mock = $mock->onlyMethods($methods)->getMock();

foreach ($methodsMap as $method => $value) {
foreach ($methods_map as $method => $value) {
// Handle callback values differently.
if ($value instanceof Stub) {
$mock->expects($this->any())
Expand Down

0 comments on commit c273690

Please sign in to comment.