From: Kunal Mehta Date: Wed, 30 Jan 2019 05:47:05 +0000 (-0800) Subject: Require ClassMatchesFilename sniff to pass for most of tests/ X-Git-Tag: 1.34.0-rc.0~2990 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=b6b10bb58fdc396f3c659c26ed1d8588daeb66b2;p=lhc%2Fweb%2Fwiklou.git Require ClassMatchesFilename sniff to pass for most of tests/ phpunit-patch-coverage assumes that the filename matches the classname as a performance optimization. And for most test cases, this is true. We should enforce this with PHPCS, mostly to help developers not make mistakes. Test cases that have mock classes will need to ensure that the test case class that matches the filename comes first, since that's the only class the sniff will look at. Tests in GlobalFunctions/ and maintenance/ are still exempted for now, since they don't match yet. Change-Id: Iede341504290f5ba2da1c81908069ba9d465600f --- diff --git a/.phpcs.xml b/.phpcs.xml index 88a6f8cd70..30f920144a 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -175,8 +175,12 @@ */profileinfo\.php */languages/*\.php - - */tests/*\.php + + */tests/parser/*\.php + */tests/phan/*\.php + */tests/phpunit/maintenance/*\.php + */tests/phpunit/bootstrap\.php + */tests/phpunit/phpunit\.php - */tests/*\.php + + */tests/phpunit/includes/GlobalFunctions/*\.php + */tests/phpunit/maintenance/*\.php diff --git a/tests/phpunit/includes/db/DatabaseSqliteTest.php b/tests/phpunit/includes/db/DatabaseSqliteTest.php index 78af11d25d..eebb045948 100644 --- a/tests/phpunit/includes/db/DatabaseSqliteTest.php +++ b/tests/phpunit/includes/db/DatabaseSqliteTest.php @@ -5,26 +5,6 @@ use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\DatabaseSqlite; use Wikimedia\Rdbms\ResultWrapper; -class DatabaseSqliteMock extends DatabaseSqlite { - public static function newInstance( array $p = [] ) { - $p['dbFilePath'] = ':memory:'; - $p['schema'] = false; - - return Database::factory( 'SqliteMock', $p ); - } - - function query( $sql, $fname = '', $tempIgnore = false ) { - return true; - } - - /** - * Override parent visibility to public - */ - public function replaceVars( $s ) { - return parent::replaceVars( $s ); - } -} - /** * @group sqlite * @group Database @@ -539,3 +519,23 @@ class DatabaseSqliteTest extends MediaWikiTestCase { $this->assertTrue( $attributes[Database::ATTR_DB_LEVEL_LOCKING] ); } } + +class DatabaseSqliteMock extends DatabaseSqlite { + public static function newInstance( array $p = [] ) { + $p['dbFilePath'] = ':memory:'; + $p['schema'] = false; + + return Database::factory( 'SqliteMock', $p ); + } + + function query( $sql, $fname = '', $tempIgnore = false ) { + return true; + } + + /** + * Override parent visibility to public + */ + public function replaceVars( $s ) { + return parent::replaceVars( $s ); + } +} diff --git a/tests/phpunit/includes/deferred/SearchUpdateTest.php b/tests/phpunit/includes/deferred/SearchUpdateTest.php index 9e4dbea221..74a5e3c470 100644 --- a/tests/phpunit/includes/deferred/SearchUpdateTest.php +++ b/tests/phpunit/includes/deferred/SearchUpdateTest.php @@ -1,20 +1,5 @@ cache ) ) { - $success = true; - return $this->cache[$key]; - } - $success = false; - return false; - } - - protected function storeResult( $key, $result ) { - $this->cache[$key] = $result; - } -} - -/** - * PHP Unit tests for MemoizedCallable class. + * PHPUnit tests for MemoizedCallable class. * @covers MemoizedCallable */ class MemoizedCallableTest extends PHPUnit\Framework\TestCase { @@ -140,3 +119,24 @@ class MemoizedCallableTest extends PHPUnit\Framework\TestCase { $memoized = new MemoizedCallable( 14 ); } } + +/** + * A MemoizedCallable subclass that stores function return values + * in an instance property rather than APC or APCu. + */ +class ArrayBackedMemoizedCallable extends MemoizedCallable { + private $cache = []; + + protected function fetchResult( $key, &$success ) { + if ( array_key_exists( $key, $this->cache ) ) { + $success = true; + return $this->cache[$key]; + } + $success = false; + return false; + } + + protected function storeResult( $key, $result ) { + $this->cache[$key] = $result; + } +} diff --git a/tests/phpunit/includes/poolcounter/PoolCounterTest.php b/tests/phpunit/includes/poolcounter/PoolCounterTest.php index f7f2013cb4..19baf5a82f 100644 --- a/tests/phpunit/includes/poolcounter/PoolCounterTest.php +++ b/tests/phpunit/includes/poolcounter/PoolCounterTest.php @@ -1,14 +1,5 @@