DtaResult.php 3.98 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16

17
18
defined('MOODLE_INTERNAL') || die();

19
20
class DtaResult {

21
22
    public $packagename;
    public $classname;
23
24
25
26
27
28
29
30
31
32
33
34
    public $name;

    /**
     * State is defined like below
     *
     *  0 UNKNOWN
     *  1 SUCCESS
     *  2 FAILURE
     *  3 COMPILATIONERROR
     */
    public $state;

35
36
    public $failuretype;
    public $failurereason;
37
38
    public $stacktrace;

39
40
    public $columnnumber;
    public $linenumber;
41
42
43
44
45
    public $position;

    /**
     * @return name of state like defined
     */
46
    public static function getstatename(int $state): string {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        if ($state == 1) {
            return "success";
        } else if ($state == 2) {
            return "failed";
        } else if ($state == 3) {
            return "compilation error";
        } else {
            return "unknown";
        }
    }
}

class DtaResultSummary {

    public $timestamp;
62
63
64
    public $globalstacktrace;
    public $successfultestcompetencies;
    public $overalltestcompetencies;
65
66
67
68
69
70
    public $results;

    /**
     * @param string $jsonString jsonString containing DtaResultSummary
     * @return DtaResultSummary
     */
71
    public static function decodejson($jsonstring): DtaResultSummary {
72
        $response = json_decode($jsonstring);
73
74
75

        $summary = new DtaResultSummary();
        $summary->timestamp = $response->timestamp;
76
        $summary->globalstacktrace = $response->globalstacktrace;
77

78
79
        $summary->successfultestcompetencies = $response->successfulTestCompetencyProfile;
        $summary->overalltestcompetencies = $response->overallTestCompetencyProfile;
80

81
        $summary->results = self::decodejsonresultarray($response->results);
82
83
84
85
86
87
88
89

        return $summary;
    }

    /**
     * @param array $jsonArray decoded json array of results array
     * @return array of DtaResult
     */
90
    private static function decodejsonresultarray($jsonarray): array {
91
        $ret = [];
92
        foreach ($jsonarray as $entry) {
93
            $value = new DtaResult();
94
95
            $value->packagename = $entry->packageName;
            $value->classname = $entry->className;
96
97
            $value->name = $entry->name;

98
99
            $value->state = $entry->state;

100
101
            $value->failuretype = $entry->failureType;
            $value->failurereason = $entry->failureReason;
102
103
            $value->stacktrace = $entry->stacktrace;

104
105
            $value->columnnumber = $entry->columnNumber;
            $value->linenumber = $entry->lineNumber;
106
107
108
109
110
111
112
            $value->position = $entry->position;

            $ret[] = $value;
        }
        return $ret;
    }

113
114
115
116
    public function resultcount(): int {
        return count($this->results);
    }

117
118
119
120
    /**
     * @param int $state state ordinal number
     * @return int count of occurences provided state has
     */
121
    public function stateoccurencecount(int $state): int {
122
        $num = 0;
123
        foreach ($this->results as $r) {
124
125
126
127
128
129
130
            if ($r->state == $state) {
                $num++;
            }
        }
        return $num;
    }

131
132
    public function compilationerrorcount(): int {
        return $this->stateoccurencecount(3);
133
134
    }

135
136
    public function failedcount(): int {
        return $this->stateoccurencecount(2);
137
138
    }

139
140
    public function successfulcount(): int {
        return $this->stateoccurencecount(1);
141
142
    }

143
144
    public function unknowncount(): int {
        return $this->stateoccurencecount(0);
145
146
147
    }

}