wiki.techinc.nl/tests/parser/DbTestRecorder.php
C. Scott Ananian a5a04f56bd Pass <Test,TestMode> as argument pair, deprecate old TestFileReader
We introduced a new type for "ParserTestMode" and then uniformly pass
this around alongside the ParserTest object itself.  (We started by
using a string for the $mode but the "explicit changetree" mode
requires more structured data.  We *could* encode this in JSON just to
keep the string type around, but it seems cleaner and more future
proof to wrap this in a proper class type.)

Removed the old TestFileReader wrapper class, which served only as
a thunk to convert the new Test class into the same sort of array
which the legacy parser test runner code expected.  There's still
some remaining ::testToArray() usage here for the TestRecorder
framework, but that will be cleaned up in a future patch.

Also updated the TestRecorder with stronger types: use the same
<Test,TestMode> pair when invoking the TestRecorder, instead of the
loosely typed array we'd used previously.

Change-Id: Iec4c0c7972a655b4faccacc67bdc3ca66d4c163d
2022-06-03 17:15:38 -04:00

86 lines
2.4 KiB
PHP

<?php
/**
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Testing
*/
use Wikimedia\Rdbms\IMaintainableDatabase;
class DbTestRecorder extends TestRecorder {
/** @var string */
public $version;
/** @var IMaintainableDatabase */
private $db;
/** @var int */
private $curRun;
public function __construct( $db ) {
$this->db = $db;
}
/**
* Set up result recording; insert a record for the run with the date
* and all that fun stuff
*/
public function start() {
$this->db->begin( __METHOD__ );
if ( !$this->db->tableExists( 'testrun' )
|| !$this->db->tableExists( 'testitem' )
) {
print "WARNING> `testrun` table not found in database. Trying to create table.\n";
$updater = DatabaseUpdater::newForDB( $this->db );
$this->db->sourceFile( $updater->patchPath( $this->db, 'patch-testrun.sql' ) );
echo "OK, resuming.\n";
}
$this->db->insert( 'testrun',
[
'tr_date' => $this->db->timestamp(),
'tr_mw_version' => $this->version,
'tr_php_version' => PHP_VERSION,
'tr_db_version' => $this->db->getServerVersion(),
'tr_uname' => php_uname()
],
__METHOD__ );
$this->curRun = $this->db->insertId();
}
/**
* Record an individual test item's success or failure to the db
*
* @param ParserTestResult $result
*/
public function record( ParserTestResult $result ) {
$desc = $result->getDescription();
$this->db->insert( 'testitem',
[
'ti_run' => $this->curRun,
'ti_name' => $desc,
'ti_success' => $result->isSuccess() ? 1 : 0,
],
__METHOD__ );
}
/**
* Commit transaction and clean up for result recording
*/
public function end() {
$this->db->commit( __METHOD__ );
}
}