-
Notifications
You must be signed in to change notification settings - Fork 1
/
MockTrait.php
74 lines (62 loc) · 2.3 KB
/
MockTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace YourNamespace\App\Tests\Traits;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Trait MockTrait.
*
* This trait provides a method to prepare class mock.
*
* @phpstan-ignore trait.unused
*/
trait MockTrait {
/**
* Helper to prepare class or trait mock.
*
* @param class-string $class
* Class or trait name to generate the mock.
* @param array<string, scalar|\Closure> $methods
* Optional array of methods and values, keyed by method name. Array
* elements can be return values, callbacks created with
* $this->willReturnCallback(), or closures.
* @param bool|array<mixed> $args
* Optional array of constructor arguments or FALSE to disable the original
* constructor. If omitted, an original constructor will be called.
*
* @return \PHPUnit\Framework\MockObject\MockObject
* Mocked class.
*
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
*/
protected function prepareMock(string $class, array $methods = [], array|bool $args = []): MockObject {
$methods = array_filter($methods, fn($value, $key): bool => !is_numeric($key), ARRAY_FILTER_USE_BOTH);
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class %s does not exist', $class));
}
$builder = $this->getMockBuilder($class);
if (is_array($args) && !empty($args)) {
$builder->enableOriginalConstructor()->setConstructorArgs($args);
}
elseif ($args === FALSE) {
$builder->disableOriginalConstructor();
}
$method_names = array_values(array_filter(array_keys($methods), fn($method): bool => !empty($method)));
if (!empty($method_names)) {
$builder->onlyMethods($method_names);
}
$mock = $builder->getMock();
foreach ($methods as $method => $value) {
// Handle callback value differently based on its type.
if (is_object($value) && str_contains($value::class, 'Callback')) {
$mock->expects($this->any())->method($method)->willReturnCallback($value);
}
elseif (is_object($value) && str_contains($value::class, 'Closure')) {
$mock->expects($this->any())->method($method)->willReturnCallback($value);
}
else {
$mock->expects($this->any())->method($method)->willReturn($value);
}
}
return $mock;
}
}