DbTestRecorder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. use Wikimedia\Rdbms\IMaintainableDatabase;
  22. class DbTestRecorder extends TestRecorder {
  23. public $version;
  24. /** @var Database */
  25. private $db;
  26. public function __construct( IMaintainableDatabase $db ) {
  27. $this->db = $db;
  28. }
  29. /**
  30. * Set up result recording; insert a record for the run with the date
  31. * and all that fun stuff
  32. */
  33. function start() {
  34. $this->db->begin( __METHOD__ );
  35. if ( !$this->db->tableExists( 'testrun' )
  36. || !$this->db->tableExists( 'testitem' )
  37. ) {
  38. print "WARNING> `testrun` table not found in database. Trying to create table.\n";
  39. $updater = DatabaseUpdater::newForDB( $this->db );
  40. $this->db->sourceFile( $updater->patchPath( $this->db, 'patch-testrun.sql' ) );
  41. echo "OK, resuming.\n";
  42. }
  43. $this->db->insert( 'testrun',
  44. [
  45. 'tr_date' => $this->db->timestamp(),
  46. 'tr_mw_version' => $this->version,
  47. 'tr_php_version' => PHP_VERSION,
  48. 'tr_db_version' => $this->db->getServerVersion(),
  49. 'tr_uname' => php_uname()
  50. ],
  51. __METHOD__ );
  52. if ( $this->db->getType() === 'postgres' ) {
  53. $this->curRun = $this->db->currentSequenceValue( 'testrun_id_seq' );
  54. } else {
  55. $this->curRun = $this->db->insertId();
  56. }
  57. }
  58. /**
  59. * Record an individual test item's success or failure to the db
  60. *
  61. * @param array $test
  62. * @param ParserTestResult $result
  63. */
  64. function record( $test, ParserTestResult $result ) {
  65. $this->db->insert( 'testitem',
  66. [
  67. 'ti_run' => $this->curRun,
  68. 'ti_name' => $test['desc'],
  69. 'ti_success' => $result->isSuccess() ? 1 : 0,
  70. ],
  71. __METHOD__ );
  72. }
  73. /**
  74. * Commit transaction and clean up for result recording
  75. */
  76. function end() {
  77. $this->db->commit( __METHOD__ );
  78. }
  79. }