Merge "objectcache: Never use CACHE_NONE for CACHE_ANYTHING"
[lhc/web/wiklou.git] / tests / parser / DbTestPreviewer.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 class DbTestPreviewer extends TestRecorder {
23 protected $filter; // /< Test name filter callback
24 protected $lb; // /< Database load balancer
25 protected $db; // /< Database connection to the main DB
26 protected $curRun; // /< run ID number for the current run
27 protected $prevRun; // /< run ID number for the previous run, if any
28 protected $results; // /< Result array
29
30 /**
31 * This should be called before the table prefix is changed
32 */
33 function __construct( $db, $filter = false ) {
34 $this->db = $db;
35 $this->filter = $filter;
36 }
37
38 /**
39 * Set up result recording; insert a record for the run with the date
40 * and all that fun stuff
41 */
42 function start() {
43 if ( !$this->db->tableExists( 'testrun', __METHOD__ )
44 || !$this->db->tableExists( 'testitem', __METHOD__ )
45 ) {
46 print "WARNING> `testrun` table not found in database.\n";
47 $this->prevRun = false;
48 } else {
49 // We'll make comparisons against the previous run later...
50 $this->prevRun = $this->db->selectField( 'testrun', 'MAX(tr_id)' );
51 }
52
53 $this->results = [];
54 }
55
56 function record( $test, ParserTestResult $result ) {
57 $this->results[$test['desc']] = $result->isSuccess() ? 1 : 0;
58 }
59
60 function report() {
61 if ( $this->prevRun ) {
62 // f = fail, p = pass, n = nonexistent
63 // codes show before then after
64 $table = [
65 'fp' => 'previously failing test(s) now PASSING! :)',
66 'pn' => 'previously PASSING test(s) removed o_O',
67 'np' => 'new PASSING test(s) :)',
68
69 'pf' => 'previously passing test(s) now FAILING! :(',
70 'fn' => 'previously FAILING test(s) removed O_o',
71 'nf' => 'new FAILING test(s) :(',
72 'ff' => 'still FAILING test(s) :(',
73 ];
74
75 $prevResults = [];
76
77 $res = $this->db->select( 'testitem', [ 'ti_name', 'ti_success' ],
78 [ 'ti_run' => $this->prevRun ], __METHOD__ );
79 $filter = $this->filter;
80
81 foreach ( $res as $row ) {
82 if ( !$filter || $filter( $row->ti_name ) ) {
83 $prevResults[$row->ti_name] = $row->ti_success;
84 }
85 }
86
87 $combined = array_keys( $this->results + $prevResults );
88
89 # Determine breakdown by change type
90 $breakdown = [];
91 foreach ( $combined as $test ) {
92 if ( !isset( $prevResults[$test] ) ) {
93 $before = 'n';
94 } elseif ( $prevResults[$test] == 1 ) {
95 $before = 'p';
96 } else /* if ( $prevResults[$test] == 0 ) */ {
97 $before = 'f';
98 }
99
100 if ( !isset( $this->results[$test] ) ) {
101 $after = 'n';
102 } elseif ( $this->results[$test] == 1 ) {
103 $after = 'p';
104 } else /* if ( $this->results[$test] == 0 ) */ {
105 $after = 'f';
106 }
107
108 $code = $before . $after;
109
110 if ( isset( $table[$code] ) ) {
111 $breakdown[$code][$test] = $this->getTestStatusInfo( $test, $after );
112 }
113 }
114
115 # Write out results
116 foreach ( $table as $code => $label ) {
117 if ( !empty( $breakdown[$code] ) ) {
118 $count = count( $breakdown[$code] );
119 printf( "\n%4d %s\n", $count, $label );
120
121 foreach ( $breakdown[$code] as $differing_test_name => $statusInfo ) {
122 print " * $differing_test_name [$statusInfo]\n";
123 }
124 }
125 }
126 } else {
127 print "No previous test runs to compare against.\n";
128 }
129
130 print "\n";
131 }
132
133 /**
134 * Returns a string giving information about when a test last had a status change.
135 * Could help to track down when regressions were introduced, as distinct from tests
136 * which have never passed (which are more change requests than regressions).
137 * @param string $testname
138 * @param string $after
139 * @return string
140 */
141 private function getTestStatusInfo( $testname, $after ) {
142 // If we're looking at a test that has just been removed, then say when it first appeared.
143 if ( $after == 'n' ) {
144 $changedRun = $this->db->selectField( 'testitem',
145 'MIN(ti_run)',
146 [ 'ti_name' => $testname ],
147 __METHOD__ );
148 $appear = $this->db->selectRow( 'testrun',
149 [ 'tr_date', 'tr_mw_version' ],
150 [ 'tr_id' => $changedRun ],
151 __METHOD__ );
152
153 return "First recorded appearance: "
154 . date( "d-M-Y H:i:s", strtotime( $appear->tr_date ) )
155 . ", " . $appear->tr_mw_version;
156 }
157
158 // Otherwise, this test has previous recorded results.
159 // See when this test last had a different result to what we're seeing now.
160 $conds = [
161 'ti_name' => $testname,
162 'ti_success' => ( $after == 'f' ? "1" : "0" ) ];
163
164 if ( $this->curRun ) {
165 $conds[] = "ti_run != " . $this->db->addQuotes( $this->curRun );
166 }
167
168 $changedRun = $this->db->selectField( 'testitem', 'MAX(ti_run)', $conds, __METHOD__ );
169
170 // If no record of ever having had a different result.
171 if ( is_null( $changedRun ) ) {
172 if ( $after == "f" ) {
173 return "Has never passed";
174 } else {
175 return "Has never failed";
176 }
177 }
178
179 // Otherwise, we're looking at a test whose status has changed.
180 // (i.e. it used to work, but now doesn't; or used to fail, but is now fixed.)
181 // In this situation, give as much info as we can as to when it changed status.
182 $pre = $this->db->selectRow( 'testrun',
183 [ 'tr_date', 'tr_mw_version' ],
184 [ 'tr_id' => $changedRun ],
185 __METHOD__ );
186 $post = $this->db->selectRow( 'testrun',
187 [ 'tr_date', 'tr_mw_version' ],
188 [ "tr_id > " . $this->db->addQuotes( $changedRun ) ],
189 __METHOD__,
190 [ "LIMIT" => 1, "ORDER BY" => 'tr_id' ]
191 );
192
193 if ( $post ) {
194 $postDate = date( "d-M-Y H:i:s", strtotime( $post->tr_date ) ) . ", {$post->tr_mw_version}";
195 } else {
196 $postDate = 'now';
197 }
198
199 return ( $after == "f" ? "Introduced" : "Fixed" ) . " between "
200 . date( "d-M-Y H:i:s", strtotime( $pre->tr_date ) ) . ", " . $pre->tr_mw_version
201 . " and $postDate";
202 }
203 }