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

JBZoo / Codestyle / 9220966151

26 Mar 2024 08:28PM UTC coverage: 87.66% (-0.3%) from 87.967%
9220966151

push

github

web-flow
Update getFixerConfig method in PhpCsFixerCodingStandard (#50)

11 of 14 new or added lines in 1 file covered. (78.57%)

51 existing lines in 5 files now uncovered.

547 of 624 relevant lines covered (87.66%)

10.39 hits per line

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

97.66
/src/PHPUnit/TraitGithubActions.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - Codestyle.
5
 *
6
 * This file is part of the JBZoo Toolbox project.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @see        https://github.com/JBZoo/Codestyle
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\Codestyle\PHPUnit;
18

19
use function JBZoo\Data\yml;
20
use function JBZoo\PHPUnit\isSame;
21

22
/**
23
 * @phan-file-suppress PhanUndeclaredProperty
24
 */
25
trait TraitGithubActions
26
{
27
    public function testGithubActionsWorkflow(): void
28
    {
29
        $mailYmlPath = PROJECT_ROOT . '/.github/workflows/main.yml';
6✔
30
        $actual      = yml($mailYmlPath)->getArrayCopy();
6✔
31

32
        isSame(120, $actual['env']['COLUMNS']);
6✔
33
        isSame('Hyper', $actual['env']['TERM_PROGRAM']);
6✔
34
        unset($actual['env']);
6✔
35

36
        // General Parameters
37
        $expectedOs = 'ubuntu-latest';
6✔
38

39
        // Expected
40
        $expected = [
6✔
41
            'name' => 'CI',
3✔
42
            'on'   => [
3✔
43
                'pull_request' => ['branches' => ['*']],
3✔
44
                'push'         => ['branches' => ['master']],
3✔
45
                'schedule'     => [['cron' => $this->getScheduleMinute()]],
6✔
46
            ],
3✔
47

48
            'jobs' => [
3✔
49
                'phpunit' => [
3✔
50
                    'name'     => 'PHPUnit',
3✔
51
                    'runs-on'  => $expectedOs,
3✔
52
                    'env'      => ['JBZOO_COMPOSER_UPDATE_FLAGS' => '${{ matrix.composer_flags }}'],
3✔
53
                    'strategy' => [
3✔
54
                        'matrix' => [
3✔
55
                            'php-version'    => static::phpVersions(),
6✔
56
                            'coverage'       => ['xdebug', 'none'],
3✔
57
                            'composer_flags' => ['--prefer-lowest', ''],
3✔
58
                        ],
3✔
59
                    ],
3✔
60
                    'steps' => [
3✔
61
                        static::checkoutStep('phpunit'),
6✔
62
                        static::setupPhpStep('phpunit'),
6✔
63
                        static::buildStep('phpunit'),
6✔
64
                        static::stepBeforeTests(),
6✔
65
                        [
3✔
66
                            'name' => '๐Ÿงช PHPUnit Tests',
3✔
67
                            'run'  => 'make test --no-print-directory',
3✔
68
                        ],
3✔
69
                        static::stepAfterTests(),
6✔
70
                        [
3✔
71
                            'name'              => 'Uploading coverage to coveralls',
3✔
72
                            'if'                => '${{ matrix.coverage == \'xdebug\' }}',
3✔
73
                            'continue-on-error' => true,
3✔
74
                            'env'               => ['COVERALLS_REPO_TOKEN' => '${{ secrets.GITHUB_TOKEN }}'],
3✔
75
                            'run'               => 'make report-coveralls --no-print-directory || true',
3✔
76
                        ],
3✔
77
                        static::uploadArtifactsStep('PHPUnit - ${{ matrix.php-version }} - ${{ matrix.coverage }}'),
6✔
78
                    ],
3✔
79
                ],
3✔
80
                'linters' => [
3✔
81
                    'name'     => 'Linters',
3✔
82
                    'runs-on'  => $expectedOs,
3✔
83
                    'strategy' => ['matrix' => ['php-version' => static::phpVersions()]],
6✔
84
                    'steps'    => [
3✔
85
                        static::checkoutStep('linters'),
6✔
86
                        static::setupPhpStep('linters'),
6✔
87
                        static::buildStep('linters'),
6✔
88
                        [
3✔
89
                            'name' => '๐Ÿ‘ Code Quality',
3✔
90
                            'run'  => 'make codestyle --no-print-directory',
3✔
91
                        ],
3✔
92
                        static::uploadArtifactsStep('Linters - ${{ matrix.php-version }}'),
6✔
93
                    ],
3✔
94
                ],
3✔
95
                'report' => [
3✔
96
                    'name'     => 'Reports',
3✔
97
                    'runs-on'  => $expectedOs,
3✔
98
                    'strategy' => ['matrix' => ['php-version' => static::phpVersions()]],
6✔
99
                    'steps'    => [
3✔
100
                        static::checkoutStep('report'),
6✔
101
                        static::setupPhpStep('report'),
6✔
102
                        static::buildStep('report'),
6✔
103
                        [
3✔
104
                            'name' => '๐Ÿ“ Build Reports',
3✔
105
                            'run'  => 'make report-all --no-print-directory',
3✔
106
                        ],
3✔
107
                        static::uploadArtifactsStep('Reports - ${{ matrix.php-version }}'),
6✔
108
                    ],
3✔
109
                ],
3✔
110
            ],
3✔
111
        ];
3✔
112

113
        $expectedYaml = self::toYaml($expected);
6✔
114
        $actualYaml   = self::toYaml($actual);
6✔
115

116
        if (!\str_contains($actualYaml, $expectedYaml)) {
6✔
117
            isSame(
×
UNCOV
118
                $expectedYaml,
UNCOV
119
                $actualYaml,
120
                \implode("\n", [
×
UNCOV
121
                    'Expected Yaml file:',
122
                    "See: {$mailYmlPath}",
×
UNCOV
123
                    '----------------------------------------',
UNCOV
124
                    $expectedYaml,
UNCOV
125
                    '----------------------------------------',
UNCOV
126
                ]),
UNCOV
127
            );
128
        }
129
    }
130

131
    protected function getScheduleMinute(): string
132
    {
133
        return self::stringToNumber($this->packageName, 59) . ' */8 * * *';
6✔
134
    }
135

136
    protected static function stepBeforeTests(): ?array
137
    {
138
        return null;
6✔
139
    }
140

141
    protected static function stepAfterTests(): ?array
142
    {
143
        return null;
6✔
144
    }
145

146
    /**
147
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
148
     * @phan-suppress PhanUnusedProtectedNoOverrideMethodParameter
149
     */
150
    protected static function checkoutStep(string $jobName): array
151
    {
152
        return [
3✔
153
            'name' => 'Checkout code',
3✔
154
            'uses' => 'actions/checkout@v3',
3✔
155
            'with' => ['fetch-depth' => 0],
3✔
156
        ];
3✔
157
    }
158

159
    protected static function phpVersions(): array
160
    {
161
        return [8.1, 8.2, 8.3];
6✔
162
    }
163

164
    /**
165
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
166
     * @phan-suppress PhanUnusedProtectedNoOverrideMethodParameter
167
     */
168
    protected static function buildStep(string $jobName): array
169
    {
170
        return [
3✔
171
            'name' => 'Build the Project',
3✔
172
            'run'  => 'make update --no-print-directory',
3✔
173
        ];
3✔
174
    }
175

176
    protected static function setupPhpStep(string $jobName): array
177
    {
178
        $coverage = 'none';
6✔
179
        if ($jobName === 'phpunit') {
6✔
180
            $coverage = '${{ matrix.coverage }}';
6✔
181
        } elseif ($jobName === 'report') {
6✔
182
            $coverage = 'xdebug';
6✔
183
        }
184

185
        return [
3✔
186
            'name' => 'Setup PHP',
3✔
187
            'uses' => 'shivammathur/setup-php@v2',
3✔
188
            'with' => [
3✔
189
                'php-version' => '${{ matrix.php-version }}',
3✔
190
                'coverage'    => $coverage,
3✔
191
                'tools'       => 'composer',
3✔
192
                'extensions'  => 'ast',
3✔
193
            ],
3✔
194
        ];
3✔
195
    }
196

197
    protected static function uploadArtifactsStep(string $stepName): array
198
    {
199
        return [
3✔
200
            'name'              => 'Upload Artifacts',
3✔
201
            'uses'              => 'actions/upload-artifact@v3',
3✔
202
            'continue-on-error' => true,
3✔
203
            'with'              => ['name' => $stepName, 'path' => 'build/'],
3✔
204
        ];
3✔
205
    }
206

207
    private static function normalizeData(array $data): array
208
    {
209
        foreach ($data['jobs'] as $jobName => $job) {
6✔
210
            foreach ($job['steps'] as $stepIndex => $step) {
6✔
211
                if ($step === null) {
6✔
212
                    unset($data['jobs'][$jobName]['steps'][$stepIndex]);
6✔
213
                }
214
            }
215

216
            $data['jobs'][$jobName]['steps'] = \array_values($data['jobs'][$jobName]['steps']);
6✔
217
        }
218

219
        return $data;
6✔
220
    }
221

222
    private static function toYaml(array $data): string
223
    {
224
        return (string)yml(self::normalizeData($data));
6✔
225
    }
226

227
    private static function stringToNumber(string $string, int $maxNumber): int
228
    {
229
        $hash   = \md5($string);
6✔
230
        $substr = \substr($hash, 0, 8);
6✔
231
        $number = \hexdec($substr);
6✔
232

233
        return $number % ($maxNumber + 1);
6✔
234
    }
235
}
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

© 2025 Coveralls, Inc