DtaResult.php 4.2 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    public $name;

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

    public $failureType;
    public $failureReason;
    public $stacktrace;

    public $columnNumber;
    public $lineNumber;
    public $position;

    /**
     * @return name of state like defined
     */
    public static function getStateName(int $state): string {
        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
    public $globalStacktrace;
63
64
    public $successfulTestCompetencyProfile;
    public $overallTestCompetencyProfile;
65
66
67
68
69
70
    public $results;

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

        $summary = new DtaResultSummary();
        $summary->timestamp = $response->timestamp;
76
77
78
79
        $summary->globalStacktrace = $response->globalStacktrace;

        $summary->successfulTestCompetencyProfile = $response->successfulTestCompetencyProfile;
        $summary->overallTestCompetencyProfile = $response->overallTestCompetencyProfile;
80
81
82
83
84
85

        $summary->results = self::decodeJsonResultArray($response->results);

        return $summary;
    }

86
    private static function decodeJsonCompetencyArray($jsonarray): array {
87
        $ret = array();
88
89
90
91
        foreach ($jsonarray as $entry) {
            $ret[] = $entry;
        }
        return $ret;
92
93
94
95
96
97
	}

    /**
     * @param array $jsonArray decoded json array of results array
     * @return array of DtaResult
     */
98
    private static function decodeJsonResultArray($jsonarray): array {
99
        $ret = array();
100
        foreach ($jsonarray as $entry) {
101
102
            $value = new DtaResult();
            $value->packageName = $entry->packageName;
103
104
105
            $value->className = $entry->className;
            $value->name = $entry->name;

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            $value->state = $entry->state;

            $value->failureType = $entry->failureType;
            $value->failureReason = $entry->failureReason;
            $value->stacktrace = $entry->stacktrace;

            $value->columnNumber = $entry->columnNumber;
            $value->lineNumber = $entry->lineNumber;
            $value->position = $entry->position;

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

    /**
     * @param int $state state ordinal number
     * @return int count of occurences provided state has
     */
    public function stateOccurenceCount(int $state): int {
        $num = 0;
127
        foreach ($this->results as $r) {
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            if ($r->state == $state) {
                $num++;
            }
        }

        return $num;
    }

    public function compilationErrorCount(): int {
        return $this->stateOccurenceCount(3);
    }

    public function failedCount(): int {
        return $this->stateOccurenceCount(2);
    }

    public function resultCount(): int {
        return count($this->results);
    }

    public function successfulCount(): int {
        return $this->stateOccurenceCount(1);
    }

    public function unknownCount(): int {
        return $this->stateOccurenceCount(0);
    }

}