• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

The-oGlow / ezlogging / 21788760591

07 Feb 2026 11:36PM UTC coverage: 81.481% (+0.5%) from 81.0%
21788760591

push

github

ollily
#1: add  / tested

45 of 54 new or added lines in 10 files covered. (83.33%)

52 existing lines in 4 files now uncovered.

374 of 459 relevant lines covered (81.48%)

12.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.74
/src/PHPUnit/Framework/EasyGoingTestCase.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of ezlogging
7
 *
8
 * (c) 2025 Oliver Glowa, coding.glowa.com
9
 *
10
 * This source file is subject to the Apache-2.0 license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace PHPUnit\Framework;
15

16
use Monolog\ConsoleLogger;
17
use Psr\Log\LoggerInterface;
18

19
abstract class EasyGoingTestCase extends TestCase
20
{
21
    /** var string Separator for static access */
22
    public const    C_STATIC_SEP = '::';
23

24
    /** @var string All primitive datatypes */
25
    protected const C_PRIMITIVES = 'int|integer|bool|boolean|float';
26

27
    /** @var mixed The object which will be tested. */
28
    protected $o2t;
29

30
    /**
31
     * @var bool TRUE=Execute a constants cross check (Default: FALSE)
32
     *
33
     * @see EasyGoingTestCase::expectedConstsCount
34
     */
35
    private static $withConstCrossCheck = false;
36

37
    /**
38
     * @var int Set the correct count of constants in child classes. Only used if {@link EasyGoingTestCase::crossCheckConsts}=true.
39
     *
40
     * @see EasyGoingTestCase::$withConstCrossCheck
41
     * @see EasyGoingTestCase::testAllConstants()
42
     */
43
    private static $expectedConstsCount = 0;
44

45
    /** @var mixed[] Array of the names of all constants in the class. */
46
    private static $actualConsts = [];
47

48
    /** @var LoggerInterface */
49
    private static $logger;
50

51
    /**
52
     * @param bool $withConstCrossCheck
53
     * @param int  $expectedConstsCount
54
     *
55
     * @see EasyGoingTestCase::$withConstCrossCheck
56
     * @see EasyGoingTestCase::$expectedConstsCount
57
     */
UNCOV
58
    public static function setUpBeforeClass(bool $withConstCrossCheck = false, int $expectedConstsCount = 0): void
×
59
    {
UNCOV
60
        self::$logger->debug('START');
×
61

UNCOV
62
        parent::setUpBeforeClass();
×
UNCOV
63
        self::$actualConsts        = [];
×
UNCOV
64
        self::$withConstCrossCheck = $withConstCrossCheck;
×
65
        self::$expectedConstsCount = $expectedConstsCount;
×
NEW
66
        self::$logger->notice('withConstCrossCheck,expectedConstCount', [self::$withConstCrossCheck, self::$expectedConstsCount]);
×
67

68
        self::$logger->debug('END');
×
69
    }
70

UNCOV
71
    public static function tearDownAfterClass(): void
×
72
    {
UNCOV
73
        self::$logger->debug('START');
×
74

UNCOV
75
        parent::tearDownAfterClass();
×
UNCOV
76
        self::crossCheckConstants(get_class(static::prepareO2t()), self::$actualConsts);
×
77
        self::$actualConsts        = [];
×
78
        self::$withConstCrossCheck = false;
×
79
        self::$expectedConstsCount = 0;
×
80

UNCOV
81
        self::$logger->debug('END');
×
82
    }
83

84
    /**
85
     * @return mixed
86
     */
87
    abstract protected static function prepareO2t();
88

89
    /**
90
     * @return mixed
91
     */
92
    abstract protected function getCasto2t();
93

94
    /**
95
     * @param mixed   $name
96
     * @param mixed[] $data
97
     * @param string  $dataName
98
     */
99
    public function __construct($name = null, $data = [], $dataName = '')
15✔
100
    {
101
        self::$logger = new ConsoleLogger(EasyGoingTestCase::class);
15✔
102
        self::$logger->debug('START');
15✔
103

104
        parent::__construct($name, $data, $dataName);
15✔
105

106
        self::$logger->debug('END');
15✔
107
    }
108

109
    public function setUp(): void
15✔
110
    {
111
        self::$logger->debug('START');
15✔
112

113
        parent::setUp();
15✔
114
        $this->o2t = static::prepareO2t();
15✔
115

116
        self::$logger->debug('END');
15✔
117
    }
118

119
    /**
120
     * @param mixed[] $constants
121
     */
122
    protected function verifyConstAllExists(array $constants = []): void
1✔
123
    {
124
        self::$logger->debug('START');
1✔
125

126
        foreach ($constants as $constant) {
1✔
127
            $this->verifyConstExists($constant);
1✔
128
        }
129

130
        self::$logger->debug('END');
1✔
131
    }
132

133
    /**
134
     * @param mixed[] $constants
135
     */
UNCOV
136
    protected function verifyConstArrayAllExists(array $constants = []): void
×
137
    {
NEW
138
        self::$logger->debug('START');
×
139

UNCOV
140
        foreach ($constants as $constant => $expectedSize) {
×
UNCOV
141
            $this->verifyConstExists($constant);
×
UNCOV
142
            $this->verifyConstArraySize($constant, $expectedSize);
×
143
        }
144

NEW
145
        self::$logger->debug('END');
×
146
    }
147

UNCOV
148
    protected function verifyConstArraySize(string $constantName, int $expectedSize): void
×
149
    {
NEW
150
        self::$logger->debug('START');
×
151

UNCOV
152
        $constantValue = self::getConstValue($this->o2t, $constantName);
×
UNCOV
153
        static::assertIsArray($constantValue);
×
UNCOV
154
        static::assertCount($expectedSize, $constantValue);
×
155

NEW
156
        self::$logger->debug('END');
×
157
    }
158

159
    /**
160
     * @param string $constantName
161
     *
162
     * @SuppressWarnings("PHPMD.ElseExpression")
163
     */
164
    protected function verifyConstExists(string $constantName): void
1✔
165
    {
166
        self::$logger->debug('START');
1✔
167

168
        $isDefined = self::isConstExist($this->o2t, $constantName);
1✔
169
        if ($isDefined) {
1✔
170
            $constantValue = self::getConstValue($this->o2t, $constantName);
1✔
171
            self::$logger->info("Checking '$constantName'=" . print_r($constantValue, true));
1✔
172
            if (static::isPrimitive($constantValue)) {
1✔
UNCOV
173
                static::assertGreaterThan(0, strlen("$constantValue"), "The primitive '$constantName'='$constantValue'");
×
174
            } else {
175
                static::assertNotEmpty($constantValue);
1✔
176
            }
177
        } else {
UNCOV
178
            static::fail(sprintf("FAIL: Constant '%s' not exists", $constantName));
×
179
        }
180

181
        self::$logger->debug('END');
1✔
182
    }
183

184
    /**
185
     * @param mixed $var
186
     *
187
     * @return bool
188
     */
189
    protected static function isPrimitive($var): bool
1✔
190
    {
191
        $primitive = false;
1✔
192

193
        if (isset($var) && strpos(self::C_PRIMITIVES, gettype($var)) > 0) {
1✔
UNCOV
194
            $primitive = true;
×
195
        }
196

197
        return $primitive;
1✔
198
    }
199

200
    /**
201
     * @param mixed $clazz
202
     *
203
     * @return mixed[]
204
     */
205
    protected static function getAllDefinedConsts($clazz): array
5✔
206
    {
207
        $clazz = new \ReflectionClass($clazz);
5✔
208

209
        return $clazz->getConstants(); // NOSONAR php:S3011
5✔
210
    }
211

212
    /**
213
     * @param mixed  $clazz
214
     * @param string $constantName
215
     *
216
     * @return bool
217
     */
218
    protected static function isConstExist($clazz, string $constantName): bool
6✔
219
    {
220
        self::$logger->debug('START');
6✔
221

222
        try {
223
            $isDefined = defined($constantName);
6✔
224
            self::$logger->debug('Check existence by defined()', [$constantName]);
6✔
UNCOV
225
        } catch (\Throwable $e) {
×
NEW
226
            self::$logger->info('Cannot check existence by defined()', [$constantName]);
×
UNCOV
227
            $isDefined = false;
×
228
        }
229
        if (!$isDefined) {
6✔
230
            $allConsts  = self::getAllDefinedConsts($clazz);
4✔
231
            $splitClazz = explode(self::C_STATIC_SEP, $constantName);
4✔
232
            $isDefined  = isset($allConsts[$splitClazz[count($splitClazz) - 1]]);
4✔
233
            self::$logger->debug('Verify existence by reflection', [$constantName]);
4✔
234
        }
235

236
        self::$logger->debug('END');
6✔
237

238
        return $isDefined;
6✔
239
    }
240

241
    /**
242
     * @param mixed   $clazz
243
     * @param mixed[] $actualConsts
244
     *
245
     * @see EasyGoingTestCase::$withConstCrossCheck
246
     */
UNCOV
247
    protected static function crossCheckConstants($clazz, $actualConsts): void
×
248
    {
NEW
249
        self::$logger->debug('START');
×
250

UNCOV
251
        if (self::$withConstCrossCheck) {
×
NEW
252
            self::$logger->notice('CrossCheck is active');
×
UNCOV
253
            $expected = self::getAllDefinedConsts($clazz);
×
UNCOV
254
            ksort($expected);
×
UNCOV
255
            $expected = array_keys($expected);
×
256

257
            $callback = /**
258
             * @param mixed $value
259
             *
260
             * @return string
261
             */
262
                function ($value): string {
UNCOV
263
                    $res = '';
×
UNCOV
264
                    if (is_string($value) && str_contains($value, self::C_STATIC_SEP)) {
×
265
                        try {
UNCOV
266
                            $startPos = ((int)strpos($value, self::C_STATIC_SEP)) + strlen(self::C_STATIC_SEP);
×
UNCOV
267
                            $res      = substr($value, $startPos);
×
UNCOV
268
                        } catch (\Throwable $exception) {
×
UNCOV
269
                            self::$logger->error(sprintf("%s: '%s'", $exception->getMessage(), $value));
×
270
                        }
271
                    } else {
UNCOV
272
                        self::$logger->error(sprintf("Value has no '%s': '%s'", self::C_STATIC_SEP, $value));
×
273
                    }
274

UNCOV
275
                    return $res;
×
276
                };
277
            /** @var string[] */
UNCOV
278
            $actual = array_map($callback, $actualConsts);
×
UNCOV
279
            $actual = array_flip($actual);
×
UNCOV
280
            ksort($actual);
×
UNCOV
281
            $actual = array_keys($actual);
×
UNCOV
282
            static::assertEqualsCanonicalizing(
×
UNCOV
283
                $expected,
×
UNCOV
284
                $actual,
×
UNCOV
285
                'You have forgotten to check: ' . print_r(array_diff($expected, $actual), true)
×
UNCOV
286
            );
×
287
        }
288

NEW
289
        self::$logger->debug('END');
×
290
    }
291

292
    /**
293
     * @param null|mixed[] $checkedConsts
294
     *
295
     * @see EasyGoingTestCase::$withConstCrossCheck
296
     */
297
    protected static function updateActualConsts($checkedConsts): void
1✔
298
    {
299
        if (self::$withConstCrossCheck && !is_null($checkedConsts)) {
1✔
UNCOV
300
            self::$actualConsts = array_merge(self::$actualConsts, $checkedConsts);
×
301
        }
302
    }
303

304
    /**
305
     * @param int     $expectedCount
306
     * @param mixed[] $allDefinedConsts
307
     *
308
     * @return array<mixed>
309
     */
310
    protected static function checkConstantsCount(int $expectedCount, $allDefinedConsts)
1✔
311
    {
312
        self::$logger->debug('START');
1✔
313

314
        $allCount = sizeof($allDefinedConsts);
1✔
315
        if (self::$withConstCrossCheck) {
1✔
UNCOV
316
            $result = $expectedCount == $allCount;
×
317
        } else {
318
            $result = true;
1✔
319
        }
320

321
        self::$logger->debug('END');
1✔
322

323
        return [$result, $allCount];
1✔
324
    }
325

326
    /**
327
     * @param mixed  $clazz
328
     * @param string $constantName
329
     *
330
     * @return mixed
331
     */
332
    protected static function getConstValue($clazz, string $constantName)
6✔
333
    {
334
        self::$logger->debug('START');
6✔
335

336
        try {
337
            $constantValue = constant($constantName);
6✔
338
            self::$logger->debug('Recieved by constant()', [$constantName]);
3✔
339
        } catch (\Throwable $e) {
4✔
340
            self::$logger->info('Cannot get value by constant()', [$constantName]);
4✔
341
        }
342
        if (!isset($constantValue)) {
6✔
343
            $clazz         = new \ReflectionClass($clazz);
4✔
344
            $splitClazz    = explode(self::C_STATIC_SEP, $constantName);
4✔
345
            $constantValue = $clazz->getConstant($splitClazz[count($splitClazz) - 1]); // NOSONAR php:S3011
4✔
346
            self::$logger->debug('Recieved by reflection', [$constantName]);
4✔
347
        }
348

349
        self::$logger->debug('END');
6✔
350

351
        return $constantValue;
6✔
352
    }
353

354
    public function testInit(): void
1✔
355
    {
356
        self::$logger->debug('START');
1✔
357

358
        static::assertNotEmpty($this->o2t);
1✔
359
        static::assertIsObject($this->o2t);
1✔
360
        static::assertInstanceOf(get_class($this->o2t), static::prepareO2t());
1✔
361

362
        self::$logger->debug('END');
1✔
363
    }
364

365
    /**
366
     * @see EasyGoingTestCase::$withConstCrossCheck
367
     * @see EasyGoingTestCase::$expectedConstsCount
368
     */
369
    public function testAllConstants(): void
1✔
370
    {
371
        self::$logger->debug('START');
1✔
372

373
        $allDefinedConsts = self::getAllDefinedConsts(get_class(static::prepareO2t()));
1✔
374
        ksort($allDefinedConsts);
1✔
375
        [$actual, $actualConstsCount] = self::checkConstantsCount(self::$expectedConstsCount, $allDefinedConsts);
1✔
376

377
        static::assertTrue(
1✔
378
            $actual,
1✔
379
            sprintf('Constants, expected count is not reached by actual count [%s, %s] ', self::$expectedConstsCount, $actualConstsCount)
1✔
380
        );
1✔
381

382
        self::$logger->debug('END');
1✔
383
    }
384
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc