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

The-oGlow / ezlogging / 19988247912

06 Dec 2025 12:08PM UTC coverage: 80.965%. Remained the same
19988247912

push

github

ollily
#1: add  / tested

3 of 4 new or added lines in 1 file covered. (75.0%)

302 of 373 relevant lines covered (80.97%)

12.84 hits per line

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

34.29
/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 PHPStan\BetterReflection\Reflection\ReflectionClass;
18
use PHPUnit\Framework\Error\Warning;
19

20
abstract class EasyGoingTestCase extends TestCase
21
{
22
    public const    C_STATIC_SEP = '::';
23

24
    protected const C_PRIMITIVES = 'int|integer|bool|boolean|float';
25

26
    /** @var \Monolog\Logger */
27
    protected $logger;
28

29
    /** @var mixed */
30
    protected $o2t;
31

32
    public function setUp(): void
12✔
33
    {
34
        parent::setUp();
12✔
35
        $this->logger = new ConsoleLogger(static::class);
12✔
36
        $this->o2t    = $this->prepareO2t();
12✔
37
    }
38

39
    /**
40
     * @return mixed
41
     */
42
    abstract protected function prepareO2t();
43

44
    /**
45
     * @return mixed
46
     */
47
    abstract protected function getCasto2t();
48

49
    /**
50
     * @param mixed[] $constants
51
     */
52
    protected function verifyConstAllExists(array $constants = []): void
×
53
    {
54
        foreach ($constants as $constant) {
×
55
            $this->verifyConstExists($constant);
×
56
        }
57
    }
58

59
    /**
60
     * @param mixed[] $constants
61
     */
62
    protected function verifyConstArrayAllExists(array $constants = []): void
×
63
    {
64
        foreach ($constants as $constant => $expectedSize) {
×
65
            $this->verifyConstExists($constant);
×
66
            $this->verifyConstArraySize($constant, $expectedSize);
×
67
        }
68
    }
69

70
    protected function verifyConstArraySize(string $constantName, int $expectedSize): void
×
71
    {
72
        $constantValue = self::getConstValue($this->o2t, $constantName);
×
73
        static::assertIsArray($constantValue);
×
74
        static::assertCount($expectedSize, $constantValue);
×
75
    }
76

77
    /**
78
     * @param string $constantName
79
     *
80
     * @SuppressWarnings("PHPMD.ElseExpression")
81
     */
82
    protected function verifyConstExists(string $constantName): void
×
83
    {
84
        $isDefined = self::isConstExist($this->o2t, $constantName);
×
85
        if ($isDefined) {
×
86
            $constantValue = self::getConstValue($this->o2t, $constantName);
×
87
            $this->logger->debug("Checking '$constantName'=" . print_r($constantValue, true));
×
88
            if (!static::isPrimitive($constantValue)) {
×
89
                static::assertNotEmpty($constantValue);
×
90
            } else {
91
                static::assertGreaterThan(0, strlen("$constantValue"), "The primitive '$constantName'='$constantValue'");
×
92
            }
93
        } else {
94
            static::fail(sprintf("FAIL: Constant '%s' not exists", $constantName));
×
95
        }
96
    }
97

98
    /**
99
     * @param mixed $var
100
     *
101
     * @return bool
102
     */
103
    protected static function isPrimitive($var): bool
×
104
    {
105
        $primitive = false;
×
106

107
        if (isset($var) && strpos(self::C_PRIMITIVES, gettype($var)) > 0) {
×
108
            $primitive = true;
×
109
        }
110

111
        return $primitive;
×
112
    }
113

114
    /**
115
     * @param mixed $clazz
116
     *
117
     * @return mixed[]
118
     */
119
    protected static function getAllDefinedConsts($clazz): array
2✔
120
    {
121
        $clazz = new \ReflectionClass($clazz);
2✔
122

123
        return $clazz->getConstants(); // NOSONAR php:S3011
2✔
124
    }
125

126
    /**
127
     * @param mixed  $clazz
128
     * @param string $constantName
129
     *
130
     * @return bool
131
     */
132
    protected static function isConstExist($clazz, string $constantName): bool
5✔
133
    {
134
        try {
135
            $isDefined = defined($constantName);
5✔
NEW
136
        } catch (\Throwable $e) {
×
137
            $isDefined = false;
×
138
        }
139
        if (!$isDefined) {
5✔
140
            echo "\nisConstExist(): By constant() '$constantName' not found!";
2✔
141
            $allConsts  = self::getAllDefinedConsts($clazz);
2✔
142
            $splitClazz = explode(self::C_STATIC_SEP, $constantName);
2✔
143
            $isDefined  = isset($allConsts[$splitClazz[count($splitClazz) - 1]]);
2✔
144
        }
145

146
        return $isDefined;
5✔
147
    }
148

149
    /**
150
     * @param mixed   $clazz
151
     * @param mixed[] $actualConsts
152
     */
153
    public static function crossCheckConstants($clazz, array $actualConsts): void
×
154
    {
155
        $expected = self::getAllDefinedConsts($clazz);
×
156
        ksort($expected);
×
157
        $expected = array_keys($expected);
×
158

159
        $callback = /**
160
         * @param mixed $value
161
         *
162
         * @return string
163
         */
164
            function ($value): string {
165
                $startPos = ((int)strpos($value, self::C_STATIC_SEP)) + strlen(self::C_STATIC_SEP);
×
166

167
                return substr((string)$value, $startPos);
×
168
            };
169
        /** @var string[] */
170
        $actual = array_map($callback, $actualConsts);
×
171
        $actual = array_flip($actual);
×
172
        ksort($actual);
×
173
        $actual = array_keys($actual);
×
174

175
        //        print_r($expected);
176
        //        print_r($actual);
177
        static::assertEqualsCanonicalizing(
×
178
            $expected,
×
179
            $actual,
×
180
            'You have forgotten to check: ' . print_r(array_diff($expected, $actual), true)
×
181
        );
×
182
    }
183

184
    /**
185
     * @param mixed  $clazz
186
     * @param string $constantName
187
     *
188
     * @return mixed
189
     */
190
    protected static function getConstValue($clazz, string $constantName)
5✔
191
    {
192
        try {
193
            $constantValue = constant($constantName);
5✔
194
        } catch (\Throwable $e) {
2✔
195
            echo "\ngetConstValue(): By constant() '$constantName' cannot get value!";
2✔
196
        }
197
        if (!isset($constantValue)) {
5✔
198
            $clazz         = new \ReflectionClass($clazz);
2✔
199
            $splitClazz    = explode(self::C_STATIC_SEP, $constantName);
2✔
200
            $constantValue = $clazz->getConstant($splitClazz[count($splitClazz) - 1]); // NOSONAR php:S3011
2✔
201
            // echo "\ngetConstValue(): By Reflection '$constantName'='$constantValue'";
202
            // } else {
203
            // echo "\ngetConstValue(): By constant() '$constantName'='$constantValue'";
204
        }
205

206
        return $constantValue;
5✔
207
    }
208

209
    public function testInit(): void
×
210
    {
211
        static::assertNotEmpty($this->o2t);
×
212
        static::assertIsObject($this->o2t);
×
213
        static::assertInstanceOf(get_class($this->o2t), $this->prepareO2t());
×
214
    }
215
}
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