-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantCheckTestCase.php
More file actions
273 lines (227 loc) · 9.35 KB
/
Copy pathConstantCheckTestCase.php
File metadata and controls
273 lines (227 loc) · 9.35 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace PHPUnit\Framework;
use Monolog\EasyGoingLogger;
use Psr\Log\LoggerInterface;
abstract class ConstantCheckTestCase extends EasyGoingTestCase
{
public const int DIFF_ZERO = 0;
public const int INIT_CONST_COUNT = 0;
public const bool INIT_CROSSCHECK = false;
/**
* @var bool TRUE=Execute a constants cross check (Default: FALSE)
*
* @see ConstantCheckTestCase::expectedConstsCount
* @see ConstantCheckTestCase::CHECK_INIT
*/
private static bool $withConstCrossCheck = self::INIT_CROSSCHECK;
/**
* @var int Set the expected and correct count of constants in child classes. Only used if {@link ConstantCheckTestCase::$withConstCrossCheck}=true.
*
* @see ConstantCheckTestCase::$withConstCrossCheck
* @see ConstantCheckTestCase::testAllConstants()
*/
private static int $expectedConstsCount = self::INIT_CONST_COUNT;
/** @var array<mixed,mixed> Array of the names of all constants in the class. */
private static array $actualConsts = [];
private static LoggerInterface $logger;
/**
* Inits the constants crosscheck.
*
* @see ConstantCheckTestCase::$withConstCrossCheck
* @see ConstantCheckTestCase::$expectedConstsCount
*/
#[\Override]
public static function setUpBeforeClass(bool $withConstCrossCheck = self::INIT_CROSSCHECK, int $expectedConstsCount = self::INIT_CONST_COUNT): void
{
self::$logger = EasyGoingLogger::init(ConstantCheckTestCase::class);
self::$logger->debug('START');
parent::setUpBeforeClass();
$testInfo = [self::$withConstCrossCheck, self::$expectedConstsCount, self::get_called_clazz()];
self::$actualConsts = [];
self::$withConstCrossCheck = $withConstCrossCheck;
self::$expectedConstsCount = $expectedConstsCount;
self::$logger->notice('withConstCrossCheck,expectedConstCount,calledClazz', $testInfo);
self::$logger->debug('END');
}
/**
* Performs the constants crosscheck at the end.
*/
#[\Override]
public static function tearDownAfterClass(): void
{
self::$logger->debug('START');
parent::tearDownAfterClass();
self::crossCheckConstants(get_class(static::prepareO2t()), self::$actualConsts);
self::$actualConsts = [];
self::$withConstCrossCheck = self::INIT_CROSSCHECK;
self::$expectedConstsCount = self::INIT_CONST_COUNT;
self::$logger->debug('END');
}
// Static functions
/**
* @return bool TRUE=constant crosscheck is activated, else FALSE
*/
public static function isWithConstCrossCheck(): bool
{
return self::$withConstCrossCheck;
}
/**
* Executes the constant crosscheck and fails if a constant is not found or not expected to exist.
*
* @param mixed $clazz the clazz having the constants to check
* @param array<mixed,mixed> $actualConsts an array of the already found constants
*
* @see ConstantCheckTestCase::$withConstCrossCheck
*/
protected static function crossCheckConstants(mixed $clazz, array $actualConsts): void
{
self::$logger->debug('START');
if (self::$withConstCrossCheck) {
self::$logger->notice('CrossCheck is active', [$clazz]);
$expected = self::getAllDefinedConsts($clazz);
ksort($expected);
$expected = array_keys($expected);
$callback =
function (mixed $value): string {
$res = '';
if (is_string($value) && str_contains($value, self::C_STATIC_SEP)) {
try {
$startPos = ((int)strpos($value, self::C_STATIC_SEP)) + strlen(self::C_STATIC_SEP);
$res = substr($value, $startPos);
} catch (\Throwable $exception) {
self::$logger->error(sprintf("%s: '%s'", $exception->getMessage(), $value));
}
} else {
self::$logger->error(sprintf("Value has no '%s': '%s'", self::C_STATIC_SEP, $value));
}
return $res;
};
self::$logger->info(' * Remove clazz prefix');
$actual = array_map($callback, $actualConsts);
self::$logger->info(' * Flip constants');
$actual = array_flip($actual);
self::$logger->info(' * Sort constants');
ksort($actual);
self::$logger->info(' * Get array keys');
$actual = array_keys($actual);
self::$logger->info(' * Verify expected vs actual constants');
$difference = array_merge(array_diff($expected, $actual), array_diff($actual, $expected));
self::$logger->debug(' * expected :', [$expected]);
self::$logger->debug(' * actual :', [$actual]);
self::$logger->debug(' * difference:', [count($difference), $difference]);
self::$logger->info('CrossCheck ended', [count($difference) == self::DIFF_ZERO, $clazz]);
self::assertCount(
self::DIFF_ZERO,
$difference,
'You have forgotten to check: ' . var_export(array_diff($expected, $actual), true)
);
}
self::$logger->debug('END');
}
/**
* Adds an array of constants which have been found.
*
* @param null|array<mixed, mixed> $checkedConsts array of found constants
*
* @see ConstantCheckTestCase::$withConstCrossCheck
*/
protected static function updateActualConsts(?array $checkedConsts): void
{
if (self::$withConstCrossCheck && !is_null($checkedConsts)) {
self::$actualConsts = array_merge(self::$actualConsts, $checkedConsts);
}
}
/**
* Checks, if {@link $allDefinedConsts) has the size of {@link $expectedCount}.
*
* @param int $expectedCount count of constants which must exists
* @param array<mixed,mixed> $allDefinedConsts an array with all defined constants
*
* @return array<mixed> [true|false, count($allDefinedConsts)]
*
* @see ConstantCheckTestCase::$withConstCrossCheck
*/
protected static function checkConstantsCount(int $expectedCount, array $allDefinedConsts): array
{
self::$logger->debug('START');
$allCount = count($allDefinedConsts);
if (self::$withConstCrossCheck) {
$result = $expectedCount == $allCount;
} else {
$result = true;
}
self::$logger->debug('END');
return [$result, $allCount];
}
// Test functions
/**
* Checks, if the count of found constants matches to the exepected count of constants.
*
* @see ConstantCheckTestCase::$expectedConstsCount
*/
public function testAllConstants(): void
{
self::$logger->debug('START');
$allDefinedConsts = self::getAllDefinedConsts(get_class(static::prepareO2t()));
ksort($allDefinedConsts);
[$actual, $actualConstsCount] = self::checkConstantsCount(self::$expectedConstsCount, $allDefinedConsts);
self::assertTrue(
$actual,
sprintf('Constants, expected count is not reached by actual count [%s, %s] ', self::$expectedConstsCount, $actualConstsCount)
);
self::$logger->debug('END');
}
// Misc functions
/**
* Checks, if all constants exists.
* <code>['CONST1','CONST2',...]</code>.
*
* @param array<mixed,mixed> $constants an array with constants to check
*/
protected function verifyConstAllExists(array $constants = []): void
{
self::$logger->debug('START');
foreach ($constants as $constant) {
$this->verifyConstExists($constant);
}
self::$logger->debug('END');
}
/**
* Checks, if all constants exists are arrays and have the expected size.
* <code>['CONST1'=>3,'CONST2'=>10,...]</code>.
*
* @param array<mixed,mixed> $constants an array with constants and expected sizes to check
*/
protected function verifyConstArrayAllExists(array $constants = []): void
{
self::$logger->debug('START');
foreach ($constants as $constant => $expectedSize) {
$this->verifyConstExists($constant);
$this->verifyConstArraySize($constant, $expectedSize);
}
self::$logger->debug('END');
}
/**
* Checks, if a constant is an array and has the expected size.
*
* @param string $constantName Name of the constant
* @param int $expectedSize expected size of the constant
*/
protected function verifyConstArraySize(string $constantName, int $expectedSize): void
{
self::$logger->debug('START');
$constantValue = self::getConstValue($this->o2t, $constantName);
self::assertIsArray($constantValue, sprintf('Constant \'%s\' is not an array!', $constantName));
self::assertCount($expectedSize, $constantValue, sprintf('Constant \'%s\' array size is not matching!', $constantName));
self::$logger->debug('END');
}
}