Skip to content

Commit

Permalink
rdbms: Document varargs for IDatabase::buildLike
Browse files Browse the repository at this point in the history
This is needed in order for Phan not to consider calls to
IDatabase::buildLike as invalid. Interestingly, it does not
consider calls to Database::buildLike invalid.

Bug: T191668
Change-Id: I0e027f5ec66d20b1d11e3441086001f6a751e1f5
  • Loading branch information
umherirrender authored and AaronSchulz committed Jun 18, 2019
1 parent a50fb36 commit 725a59f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 50 deletions.
4 changes: 2 additions & 2 deletions includes/libs/rdbms/database/DBConnRef.php
Expand Up @@ -35,7 +35,7 @@ class DBConnRef implements IDatabase {
public function __construct( ILoadBalancer $lb, $conn, $role ) {
$this->lb = $lb;
$this->role = $role;
if ( $conn instanceof Database ) {
if ( $conn instanceof IDatabase && !( $conn instanceof DBConnRef ) ) {
$this->conn = $conn; // live handle
} elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self::FLD_DOMAIN] !== false ) {
$this->params = $conn;
Expand Down Expand Up @@ -461,7 +461,7 @@ public function addIdentifierQuotes( $s ) {
return $this->__call( __FUNCTION__, func_get_args() );
}

public function buildLike() {
public function buildLike( $param ) {
return $this->__call( __FUNCTION__, func_get_args() );
}

Expand Down
10 changes: 5 additions & 5 deletions includes/libs/rdbms/database/Database.php
Expand Up @@ -2718,11 +2718,11 @@ protected function escapeLikeInternal( $s, $escapeChar = '`' ) {
$s );
}

public function buildLike() {
$params = func_get_args();

if ( count( $params ) > 0 && is_array( $params[0] ) ) {
$params = $params[0];
public function buildLike( $param, ...$params ) {
if ( is_array( $param ) ) {
$params = $param;
} else {
$params = func_get_args();
}

$s = '';
Expand Down
3 changes: 2 additions & 1 deletion includes/libs/rdbms/database/IDatabase.php
Expand Up @@ -1220,9 +1220,10 @@ public function addIdentifierQuotes( $s );
* $query .= $dbr->buildLike( $pattern );
*
* @since 1.16
* @param array[]|string|LikeMatch $param
* @return string Fully built LIKE statement
*/
public function buildLike();
public function buildLike( $param );

/**
* Returns a token for buildLike() that denotes a '_' to be used in a LIKE query
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/includes/Revision/RevisionStoreTest.php
Expand Up @@ -16,7 +16,7 @@
use MWException;
use Title;
use WANObjectCache;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
use Wikimedia\TestingAccessWrapper;
use WikitextContent;
Expand Down Expand Up @@ -70,10 +70,10 @@ private function getMockLoadBalancer() {
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|Database
* @return \PHPUnit_Framework_MockObject_MockObject|IDatabase
*/
private function getMockDatabase() {
return $this->getMockBuilder( Database::class )
return $this->getMockBuilder( IDatabase::class )
->disableOriginalConstructor()->getMock();
}

Expand Down
48 changes: 18 additions & 30 deletions tests/phpunit/includes/Storage/NameTableStoreTest.php
Expand Up @@ -10,7 +10,7 @@
use MediaWikiTestCase;
use Psr\Log\NullLogger;
use WANObjectCache;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
use Wikimedia\TestingAccessWrapper;

Expand Down Expand Up @@ -57,37 +57,25 @@ private function getMockLoadBalancer( $db ) {
}

private function getCallCheckingDb( $insertCalls, $selectCalls ) {
$mock = $this->getMockBuilder( Database::class )
$proxiedMethods = [
'select' => $selectCalls,
'insert' => $insertCalls,
'affectedRows' => null,
'insertId' => null,
'getSessionLagStatus' => null,
'writesPending' => null,
'onTransactionPreCommitOrIdle' => null
];
$mock = $this->getMockBuilder( IDatabase::class )
->disableOriginalConstructor()
->getMock();
$mock->expects( $this->exactly( $insertCalls ) )
->method( 'insert' )
->willReturnCallback( function ( ...$args ) {
return call_user_func_array( [ $this->db, 'insert' ], $args );
} );
$mock->expects( $this->exactly( $selectCalls ) )
->method( 'select' )
->willReturnCallback( function ( ...$args ) {
return call_user_func_array( [ $this->db, 'select' ], $args );
} );
$mock->expects( $this->exactly( $insertCalls ) )
->method( 'affectedRows' )
->willReturnCallback( function ( ...$args ) {
return call_user_func_array( [ $this->db, 'affectedRows' ], $args );
} );
$mock->expects( $this->any() )
->method( 'insertId' )
->willReturnCallback( function ( ...$args ) {
return call_user_func_array( [ $this->db, 'insertId' ], $args );
} );
$mock->expects( $this->any() )
->method( 'query' )
->willReturn( [] );
$mock->expects( $this->any() )
->method( 'isOpen' )
->willReturn( true );
$wrapper = TestingAccessWrapper::newFromObject( $mock );
$wrapper->queryLogger = new NullLogger();
foreach ( $proxiedMethods as $method => $count ) {
$mock->expects( is_int( $count ) ? $this->exactly( $count ) : $this->any() )
->method( $method )
->willReturnCallback( function ( ...$args ) use ( $method ) {
return call_user_func_array( [ $this->db, $method ], $args );
} );
}
return $mock;
}

Expand Down
11 changes: 2 additions & 9 deletions tests/phpunit/includes/libs/rdbms/database/DBConnRefTest.php
@@ -1,9 +1,8 @@
<?php

use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\DBConnRef;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\ResultWrapper;

Expand Down Expand Up @@ -40,7 +39,7 @@ function () use ( $lb ) {
* @return IDatabase
*/
private function getDatabaseMock() {
$db = $this->getMockBuilder( Database::class )
$db = $this->getMockBuilder( IDatabase::class )
->disableOriginalConstructor()
->getMock();

Expand All @@ -60,12 +59,6 @@ private function getDatabaseMock() {
$db->method( 'isOpen' )->willReturnCallback( function () use ( &$open ) {
return $open;
} );
$db->method( 'open' )->willReturnCallback( function () use ( &$open ) {
$open = true;

return $open;
} );
$db->method( '__toString' )->willReturn( 'MOCK_DB' );

return $db;
}
Expand Down

0 comments on commit 725a59f

Please sign in to comment.