X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fdb%2FDatabaseTestHelper.php;h=e9fc34fa167fb7b6c3ad2c0c64d3bb78a5195acd;hb=48f931b9cb0f4b53e84cfc090e4313f71a130941;hp=fa9898d1cd31395d7bff60fd9e787eb353952763;hpb=9ba178b8aa3aa93a5bba1d09cfa85527e8edfa8f;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/db/DatabaseTestHelper.php b/tests/phpunit/includes/db/DatabaseTestHelper.php index fa9898d1cd..e9fc34fa16 100644 --- a/tests/phpunit/includes/db/DatabaseTestHelper.php +++ b/tests/phpunit/includes/db/DatabaseTestHelper.php @@ -2,6 +2,7 @@ use Wikimedia\Rdbms\TransactionProfiler; use Wikimedia\Rdbms\DatabaseDomain; +use Wikimedia\Rdbms\Database; /** * Helper for testing the methods from the Database class @@ -25,6 +26,11 @@ class DatabaseTestHelper extends Database { /** @var array List of row arrays */ protected $nextResult = []; + /** @var array|null */ + protected $nextError = null; + /** @var array|null */ + protected $lastError = null; + /** * Array of tables to be considered as existing by tableExist() * Use setExistingTables() to alter. @@ -47,7 +53,11 @@ class DatabaseTestHelper extends Database { $this->errorLogger = function ( Exception $e ) { wfWarn( get_class( $e ) . ": {$e->getMessage()}" ); }; + $this->deprecationLogger = function ( $msg ) { + wfWarn( $msg ); + }; $this->currentDomain = DatabaseDomain::newUnspecified(); + $this->open( 'localhost', 'testuser', 'password', 'testdb' ); } /** @@ -73,6 +83,16 @@ class DatabaseTestHelper extends Database { $this->nextResult = $res; } + /** + * @param int $errno Error number + * @param string $error Error text + * @param array $options + * - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError() + */ + public function forceNextQueryError( $errno, $error, $options = [] ) { + $this->nextError = [ 'errno' => $errno, 'error' => $error ] + $options; + } + protected function addSql( $sql ) { // clean up spaces before and after some words and the whole string $this->lastSqls[] = trim( preg_replace( @@ -82,7 +102,17 @@ class DatabaseTestHelper extends Database { } protected function checkFunctionName( $fname ) { - if ( substr( $fname, 0, strlen( $this->testName ) ) !== $this->testName ) { + if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) { + return; // no $fname parameter + } + + // Handle some internal calls from the Database class + $check = $fname; + if ( preg_match( '/^Wikimedia\\\\Rdbms\\\\Database::query \((.+)\)$/', $fname, $m ) ) { + $check = $m[1]; + } + + if ( substr( $check, 0, strlen( $this->testName ) ) !== $this->testName ) { throw new MWException( 'function name does not start with test class. ' . $fname . ' vs. ' . $this->testName . '. ' . 'Please provide __METHOD__ to database methods.' ); @@ -101,14 +131,13 @@ class DatabaseTestHelper extends Database { public function query( $sql, $fname = '', $tempIgnore = false ) { $this->checkFunctionName( $fname ); - $this->addSql( $sql ); return parent::query( $sql, $fname, $tempIgnore ); } public function tableExists( $table, $fname = __METHOD__ ) { $tableRaw = $this->tableName( $table, 'raw' ); - if ( isset( $this->mSessionTempTables[$tableRaw] ) ) { + if ( isset( $this->sessionTempTables[$tableRaw] ) ) { return true; // already known to exist } @@ -127,7 +156,9 @@ class DatabaseTestHelper extends Database { } function open( $server, $user, $password, $dbName ) { - return false; + $this->conn = (object)[ 'test' ]; + + return true; } function fetchObject( $res ) { @@ -159,11 +190,17 @@ class DatabaseTestHelper extends Database { } function lastErrno() { - return -1; + return $this->lastError ? $this->lastError['errno'] : -1; } function lastError() { - return 'test'; + return $this->lastError ? $this->lastError['error'] : 'test'; + } + + protected function wasKnownStatementRollbackError() { + return isset( $this->lastError['wasKnownStatementRollbackError'] ) + ? $this->lastError['wasKnownStatementRollbackError'] + : false; } function fieldInfo( $table, $field ) { @@ -191,7 +228,7 @@ class DatabaseTestHelper extends Database { } function isOpen() { - return true; + return $this->conn ? true : false; } function ping( &$rtt = null ) { @@ -200,12 +237,22 @@ class DatabaseTestHelper extends Database { } protected function closeConnection() { - return false; + return true; } protected function doQuery( $sql ) { + $sql = preg_replace( '< /\* .+? \*/>', '', $sql ); + $this->addSql( $sql ); + + if ( $this->nextError ) { + $this->lastError = $this->nextError; + $this->nextError = null; + return false; + } + $res = $this->nextResult; $this->nextResult = []; + $this->lastError = null; return new FakeResultWrapper( $res ); }