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

JBZoo / CI-Report-Converter / 7686009151

28 Jan 2024 01:12PM UTC coverage: 94.125% (-0.3%) from 94.375%
7686009151

push

github

web-flow
Update PHP version and package dependencies in GitHub actions (#47)

1426 of 1515 relevant lines covered (94.13%)

45.89 hits per line

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

93.81
/src/Commands/Convert.php
1
<?php
2

3
/**
4
 * JBZoo Toolbox - CI-Report-Converter.
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/CI-Report-Converter
13
 */
14

15
declare(strict_types=1);
16

17
namespace JBZoo\CIReportConverter\Commands;
18

19
use JBZoo\CIReportConverter\Converters\CheckStyleConverter;
20
use JBZoo\CIReportConverter\Converters\Map;
21
use JBZoo\CIReportConverter\Converters\TeamCityTestsConverter;
22
use JBZoo\Cli\Codes;
23
use JBZoo\Cli\OutLvl;
24
use Symfony\Component\Console\Input\InputOption;
25

26
/**
27
 * @psalm-suppress PropertyNotSetInConstructor
28
 */
29
final class Convert extends AbstractCommand
30
{
31
    protected function configure(): void
32
    {
33
        $this
54✔
34
            ->setName('convert')
54✔
35
            ->setDescription('Convert one report format to another one')
54✔
36
            ->addOption(
54✔
37
                'input-format',
54✔
38
                'S',
54✔
39
                InputOption::VALUE_REQUIRED,
54✔
40
                'Source format. Available options: <info>' . \implode(
54✔
41
                    ', ',
54✔
42
                    Map::getAvailableFormats(Map::INPUT),
54✔
43
                ) . '</info>',
54✔
44
                CheckStyleConverter::TYPE,
54✔
45
            )
54✔
46
            ->addOption(
54✔
47
                'input-file',
54✔
48
                'I',
54✔
49
                InputOption::VALUE_OPTIONAL,
54✔
50
                'File path with the original report format. If not set or empty, then the STDIN is used.',
54✔
51
            )
54✔
52
            ->addOption(
54✔
53
                'output-format',
54✔
54
                'T',
54✔
55
                InputOption::VALUE_REQUIRED,
54✔
56
                'Target format. Available options: <info>' . \implode(
54✔
57
                    ', ',
54✔
58
                    Map::getAvailableFormats(Map::OUTPUT),
54✔
59
                ) . '</info>',
54✔
60
                TeamCityTestsConverter::TYPE,
54✔
61
            )
54✔
62
            ->addOption(
54✔
63
                'output-file',
54✔
64
                'O',
54✔
65
                InputOption::VALUE_OPTIONAL,
54✔
66
                'File path with the result report format. If not set or empty, then the STDOUT is used.',
54✔
67
            )
54✔
68
            ->addOption(
54✔
69
                'root-path',
54✔
70
                'R',
54✔
71
                InputOption::VALUE_OPTIONAL,
54✔
72
                'If option is set, all absolute file paths will be converted to relative once.',
54✔
73
                '.',
54✔
74
            )
54✔
75
            ->addOption(
54✔
76
                'suite-name',
54✔
77
                'N',
54✔
78
                InputOption::VALUE_REQUIRED,
54✔
79
                "Set custom name of root group/suite (if it's possible).",
54✔
80
            )
54✔
81
            ->addOption(
54✔
82
                'tc-flow-id',
54✔
83
                'F',
54✔
84
                InputOption::VALUE_OPTIONAL,
54✔
85
                'Custom flowId in TeamCity output. Default value is PID of the tool.',
54✔
86
            )
54✔
87
            ->addOption(
54✔
88
                'non-zero-code',
54✔
89
                'Q',
54✔
90
                InputOption::VALUE_OPTIONAL,
54✔
91
                'Will exit with the code=1, if any violations are found.',
54✔
92
                'no',
54✔
93
            );
54✔
94

95
        parent::configure();
54✔
96
    }
97

98
    protected function executeAction(): int
99
    {
100
        $sourceReport = $this->getSourceCode();
36✔
101
        $rootPath     = $this->getOptString('root-path');
36✔
102
        $suiteName    = $this->getOptString('suite-name');
36✔
103
        $nonZeroCode  = $this->getOptBool('non-zero-code');
36✔
104

105
        $casesAreFound = false;
36✔
106

107
        if ($sourceReport !== null) {
36✔
108
            $internalReport = Map::getConverter($this->getFormat('input-format'), Map::INPUT)
30✔
109
                ->setRootPath($rootPath)
30✔
110
                ->setRootSuiteName($suiteName)
30✔
111
                ->toInternal($sourceReport);
30✔
112

113
            $errorsCount  = $internalReport->getErrorsCount();
30✔
114
            $warningCount = $internalReport->getWarningCount();
30✔
115
            $failureCount = $internalReport->getFailureCount();
30✔
116

117
            $casesAreFound = $errorsCount > 0 || $warningCount > 0 || $failureCount > 0;
30✔
118

119
            $targetReport = Map::getConverter($this->getFormat('output-format'), Map::OUTPUT)
30✔
120
                ->setRootPath($rootPath)
30✔
121
                ->setRootSuiteName($suiteName)
30✔
122
                ->setFlowId($this->getOptInt('tc-flow-id'))
30✔
123
                ->fromInternal($internalReport);
30✔
124

125
            if ($this->saveResult($targetReport)) {
30✔
126
                if ($errorsCount > 0) {
6✔
127
                    $this->_("Found errors: {$errorsCount}", OutLvl::E);
×
128
                }
129

130
                if ($warningCount > 0) {
6✔
131
                    $this->_("Found warnings: {$warningCount}", OutLvl::E);
×
132
                }
133

134
                if ($failureCount > 0) {
6✔
135
                    $this->_("Found failures: {$failureCount}", OutLvl::E);
6✔
136
                }
137
            }
138
        }
139

140
        return $nonZeroCode && $casesAreFound ? Codes::GENERAL_ERROR : Codes::OK;
36✔
141
    }
142

143
    private function getFormat(string $optionName): string
144
    {
145
        $format = \strtolower($this->getOptString($optionName));
30✔
146

147
        $validFormats = Map::getAvailableFormats();
30✔
148

149
        if (!\in_array($format, $validFormats, true)) {
30✔
150
            throw new Exception(
×
151
                "Format \"{$format}\" not found. See the option \"--{$optionName}\".\n" .
×
152
                'Available options: ' . \implode(',', $validFormats),
×
153
            );
×
154
        }
155

156
        return $format;
30✔
157
    }
158
}
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