|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* @license GPL-3.0 |
|
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Exception; |
|
12
|
|
|
|
|
13
|
|
|
use ErrorException; |
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
|
16
|
|
|
|
|
17
|
|
|
final class FailedRDMAssertionException extends ErrorException implements FailedRDMAssertionExceptionInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $type; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array<mixed> |
|
27
|
|
|
*/ |
|
28
|
|
|
private $parameters; |
|
29
|
|
|
|
|
30
|
10 |
|
public function __construct(string $message, string $type, array $parameters) |
|
31
|
|
|
{ |
|
32
|
10 |
|
parent::__construct($message); |
|
33
|
|
|
|
|
34
|
10 |
|
$this->type = $type; |
|
35
|
10 |
|
$this->parameters = $parameters; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function getType(): string |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->type; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function getParameters(): array |
|
44
|
|
|
{ |
|
45
|
1 |
|
return $this->parameters; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param mixed $expectedService |
|
50
|
|
|
* @param mixed $actualService |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public static function expectedDifferentService( |
|
53
|
|
|
string $serviceId, |
|
54
|
|
|
ReflectionClass $classReflection, |
|
55
|
|
|
$expectedService, |
|
56
|
|
|
$actualService |
|
57
|
|
|
): FailedRDMAssertionException { |
|
58
|
|
|
/** @var string $actualDescription */ |
|
59
|
3 |
|
$actualDescription = self::generateDescriptionForValue($actualService); |
|
60
|
|
|
|
|
61
|
|
|
/** @var string $expectedDescription */ |
|
62
|
3 |
|
$expectedDescription = self::generateDescriptionForValue($expectedService); |
|
63
|
|
|
|
|
64
|
3 |
|
return new self( |
|
65
|
3 |
|
sprintf( |
|
66
|
|
|
"Expected service '%s' (%s) on entity %s, was %s instead!", |
|
67
|
|
|
$serviceId, |
|
68
|
|
|
$expectedDescription, |
|
69
|
3 |
|
$classReflection->getName(), |
|
70
|
|
|
$actualDescription |
|
71
|
|
|
), |
|
72
|
|
|
"EXPECTED_DIFFERENT_SERVICE", |
|
73
|
3 |
|
[$serviceId, $expectedService, $actualService] |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
public static function expectedInstanceOf( |
|
78
|
|
|
string $expectedClassName, |
|
79
|
|
|
string $actualClassName, |
|
80
|
|
|
string $declarationOrigin |
|
81
|
|
|
): FailedRDMAssertionException { |
|
82
|
2 |
|
return new self( |
|
83
|
2 |
|
sprintf( |
|
84
|
|
|
"Expected instance of %s instead of %s as specified in %s!", |
|
85
|
|
|
$expectedClassName, |
|
86
|
|
|
$actualClassName, |
|
87
|
|
|
$declarationOrigin |
|
88
|
|
|
), |
|
89
|
|
|
"EXPECTED_INSTNACE_OF", |
|
90
|
2 |
|
[$expectedClassName, $actualClassName, $declarationOrigin] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param mixed $actualValue |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public static function expectedArray( |
|
98
|
|
|
$actualValue, |
|
99
|
|
|
string $declarationOrigin |
|
100
|
|
|
): FailedRDMAssertionException { |
|
101
|
|
|
|
|
102
|
|
|
/** @var string $description */ |
|
103
|
3 |
|
$description = self::generateDescriptionForValue($actualValue); |
|
104
|
|
|
|
|
105
|
3 |
|
return new self( |
|
106
|
3 |
|
sprintf( |
|
107
|
|
|
"Expected array, got %s as specified in %s!", |
|
108
|
|
|
$description, |
|
109
|
|
|
$declarationOrigin |
|
110
|
|
|
), |
|
111
|
|
|
"EXPECTED_INSTNACE_OF", |
|
112
|
3 |
|
[$actualValue, $declarationOrigin] |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param mixed $value |
|
118
|
|
|
*/ |
|
119
|
6 |
|
private static function generateDescriptionForValue($value): string |
|
120
|
|
|
{ |
|
121
|
|
|
/** @var string $description */ |
|
122
|
6 |
|
$description = null; |
|
123
|
|
|
|
|
124
|
6 |
|
if (is_object($value)) { |
|
125
|
3 |
|
$description = get_class($value) . '#' . spl_object_hash($value); |
|
126
|
|
|
|
|
127
|
|
|
} else { |
|
128
|
4 |
|
$description = gettype($value); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
6 |
|
return $description; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|