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

The-oGlow / ezlogging / 19977401419

05 Dec 2025 10:10PM UTC coverage: 76.011%. Remained the same
19977401419

push

github

ollily
#1: add  / tested

0 of 1 new or added line in 1 file covered. (0.0%)

282 of 371 relevant lines covered (76.01%)

11.56 hits per line

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

5.88
/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
2✔
33
    {
34
        parent::setUp();
2✔
35
        $this->logger = new ConsoleLogger(static::class);
2✔
36
        $this->o2t    = $this->prepareO2t();
2✔
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
×
120
    {
121
        $clazz = new \ReflectionClass($clazz);
×
122

123
        return $clazz->getConstants();
×
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
×
133
    {
134
        $isDefined = defined($constantName);
×
135
        if (!$isDefined) {
×
136
            echo "\nisConstExist(): '$constantName' not public!";
×
137
            $allConsts  = self::getAllDefinedConsts($clazz);
×
138
            $splitClazz = explode(self::C_STATIC_SEP, $constantName);
×
139
            $isDefined  = isset($allConsts[$splitClazz[count($splitClazz) - 1]]);
×
140
        }
141

142
        return $isDefined;
×
143
    }
144

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

155
        $callback = /**
156
         * @param mixed $value
157
         *
158
         * @return string
159
         */
160
            function ($value): string {
161
                $startPos = ((int)strpos($value, self::C_STATIC_SEP)) + strlen(self::C_STATIC_SEP);
×
162

163
                return substr((string)$value, $startPos);
×
164
            };
165
        /** @var string[] */
166
        $actual = array_map($callback, $actualConsts);
×
167
        $actual = array_flip($actual);
×
168
        ksort($actual);
×
169
        $actual = array_keys($actual);
×
170

171
        //        print_r($expected);
172
        //        print_r($actual);
173
        static::assertEqualsCanonicalizing(
×
174
            $expected,
×
175
            $actual,
×
176
            'You have forgotten to check: ' . print_r(array_diff($expected, $actual), true)
×
177
        );
×
178
    }
179

180
    /**
181
     * @param mixed  $clazz
182
     * @param string $constantName
183
     *
184
     * @return mixed
185
     */
186
    protected static function getConstValue($clazz, string $constantName)
×
187
    {
188
        try {
189
            $constantValue = constant($constantName);
×
NEW
190
        } catch (Error $e) { // @phpstan-ignore catch.neverThrown
×
191
            echo "\ngetConstValue(): '" . $constantName . "' cannot get value!";
×
192
        }
193
        if (!isset($constantValue)) {
×
194
            $clazz         = new \ReflectionClass($clazz);
×
195
            $splitClazz    = explode(self::C_STATIC_SEP, $constantName);
×
196
            $constantValue = $clazz->getConstant($splitClazz[count($splitClazz) - 1]);
×
197
        }
198

199
        return $constantValue;
×
200
    }
201

202
    public function testInit(): void
×
203
    {
204
        static::assertNotEmpty($this->o2t);
×
205
        static::assertIsObject($this->o2t);
×
206
        static::assertInstanceOf(get_class($this->o2t), $this->prepareO2t());
×
207
    }
208
}
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