-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTestProxy.php
126 lines (110 loc) · 3.27 KB
/
TestProxy.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace setasign\tests;
use setasign\SetaFpdf\SetaFpdf;
/**
* Class TestProxy
*
* @mixin \setasign\Fpdi\Fpdi
*/
class TestProxy extends \PHPUnit\Framework\TestCase
{
private $instances;
public function __construct($instances)
{
$this->instances = $instances;
parent::__construct();
}
/**
* @param $name
* @param $arguments
* @return mixed
* @throws \Throwable
*/
public function __call($name, $arguments)
{
return $this->assertResult(function ($instance) use ($name, $arguments) {
return call_user_func_array([$instance, $name], $arguments);
}, '__call: ' . $name);
}
/**
* @param $name
* @return mixed
* @throws \Throwable
*/
public function __get($name)
{
return $this->assertResult(function ($instance) use ($name) {
return $instance->{$name};
}, '__get: ' . $name);
}
/** @noinspection MagicMethodsValidityInspection */
/**
* @param mixed $name
* @param mixed $value
*/
public function __set($name, $value)
{
foreach ($this->instances as $instance) {
$instance->{$name} = $value;
}
}
/**
* @param callable $callback
* @return mixed
* @throws \RuntimeException
* @throws \Throwable
*/
protected function assertResult($callback, $method)
{
$results = array_map(function ($instance) use ($callback) {
try {
return \call_user_func($callback, $instance);
// required for php5.6
} catch (\Exception $e) {
return $e;
} catch (\Throwable $e) {
return $e;
}
}, $this->instances);
if (count($results) === 0) {
throw new \BadMethodCallException('No result found! Probably because of no configured instances.');
}
$isFirst = true;
$expectedResult = null;
foreach ($results as $result) {
if ($isFirst) {
$expectedResult = $result;
$isFirst = false;
continue;
}
$resultIsError = (
is_object($result) && (
$result instanceof \Exception || $result instanceof \Throwable
));
$expectedResultIsError = (is_object($expectedResult) && (
$expectedResult instanceof \Exception || $expectedResult instanceof \Throwable
));
if ($resultIsError || $expectedResultIsError) {
$this->assertEquals(
$expectedResultIsError,
$resultIsError,
$resultIsError ? (string) $result : (string) $expectedResult
);
} else {
$this->assertEquals($expectedResult, $result, 'Different result: '. var_export($method, true));
}
}
/** @noinspection NotOptimalIfConditionsInspection */
if ($expectedResult instanceof \Exception || $expectedResult instanceof \Throwable) {
throw $expectedResult;
}
return $expectedResult;
}
/**
* @return \FPDF[]|SetaFpdf[]
*/
public function getInstances()
{
return $this->instances;
}
}