tests: Prefer PHPUnit's assertSame() when comparing empty strings

assertSame() is guaranteed to never do any magic type conversion.
This can be critical when accidentially comparing empty strings (a
value PHP considers to be "falsy") to false, 0, 0.0, null, and such.

Change-Id: I2e2685c5992cae252f629a68ffe1a049f2e5ed1b
This commit is contained in:
Thiemo Kreuz 2019-09-17 16:28:35 +02:00 committed by Krinkle
parent a99171228d
commit e06ce9f467
18 changed files with 26 additions and 27 deletions

View file

@ -45,6 +45,6 @@ trait MediaWikiCoversValidator {
}
}
$this->assertEquals( '', $bad );
$this->assertSame( '', $bad );
}
}

View file

@ -50,7 +50,7 @@ class FauxRequestTest extends PHPUnit\Framework\TestCase {
public function testGetText() {
$req = new FauxRequest( [ 'x' => 'Value' ] );
$this->assertEquals( 'Value', $req->getText( 'x' ) );
$this->assertEquals( '', $req->getText( 'z' ) );
$this->assertSame( '', $req->getText( 'z' ) );
}
/**
@ -287,8 +287,8 @@ class FauxRequestTest extends PHPUnit\Framework\TestCase {
*/
public function testDummies() {
$req = new FauxRequest();
$this->assertEquals( '', $req->getRawQueryString() );
$this->assertEquals( '', $req->getRawPostString() );
$this->assertEquals( '', $req->getRawInput() );
$this->assertSame( '', $req->getRawQueryString() );
$this->assertSame( '', $req->getRawPostString() );
$this->assertSame( '', $req->getRawInput() );
}
}

View file

@ -161,12 +161,12 @@ class HtmlTest extends MediaWikiTestCase {
* @covers Html::expandAttributes
*/
public function testExpandAttributesForBooleans() {
$this->assertEquals(
$this->assertSame(
'',
Html::expandAttributes( [ 'selected' => false ] ),
'Boolean attributes do not generates output when value is false'
);
$this->assertEquals(
$this->assertSame(
'',
Html::expandAttributes( [ 'selected' => null ] ),
'Boolean attributes do not generates output when value is null'

View file

@ -46,7 +46,7 @@ class XmlTest extends MediaWikiTestCase {
$this->assertNull( Xml::expandAttributes( null ),
'Converting a null list of attributes'
);
$this->assertEquals( '', Xml::expandAttributes( [] ),
$this->assertSame( '', Xml::expandAttributes( [] ),
'Converting an empty list of attributes'
);
}

View file

@ -76,7 +76,7 @@ class EnhancedChangesListTest extends MediaWikiLangTestCase {
$recentChange2 = $this->getEditChange( '20131103092253' );
$html = $enhancedChangesList->recentChangesLine( $recentChange2, false );
$this->assertEquals( '', $html );
$this->assertSame( '', $html );
}
public function testRecentChangesPrefix() {

View file

@ -181,7 +181,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
$content = null;
$text = ContentHandler::getContentText( $content );
$this->assertEquals( '', $text );
$this->assertSame( '', $text );
}
public static function dataGetContentText_TextContent() {

View file

@ -49,7 +49,7 @@ class UnknownContentHandlerTest extends MediaWikiLangTestCase {
$content = $handler->makeEmptyContent();
$this->assertTrue( $content->isEmpty() );
$this->assertEquals( '', $content->getData() );
$this->assertSame( '', $content->getData() );
}
public static function dataIsSupportedFormat() {

View file

@ -139,7 +139,7 @@ class UnknownContentTest extends MediaWikiLangTestCase {
public function testGetWikitextForTransclusion() {
$content = $this->newContent( 'hello world.' );
$this->assertEquals( '', $content->getWikitextForTransclusion() );
$this->assertFalse( $content->getWikitextForTransclusion() );
}
/**

View file

@ -64,7 +64,7 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase {
$content = $this->handler->makeEmptyContent();
$this->assertTrue( $content->isEmpty() );
$this->assertEquals( '', $content->getText() );
$this->assertSame( '', $content->getText() );
}
public static function dataIsSupportedFormat() {

View file

@ -475,17 +475,16 @@ class LBFactoryTest extends MediaWikiTestCase {
/** @var IMaintainableDatabase $db */
$db = $lb->getConnection( DB_MASTER, [], '' );
$this->assertEquals(
$this->assertSame(
'',
$db->getDomainId(),
'Null domain ID handle used'
);
$this->assertEquals(
'',
$this->assertNull(
$db->getDBname(),
'Null domain ID handle used'
);
$this->assertEquals(
$this->assertSame(
'',
$db->tablePrefix(),
'Main domain ID handle used; prefix is empty though'
@ -555,7 +554,7 @@ class LBFactoryTest extends MediaWikiTestCase {
/** @var IMaintainableDatabase $db */
$db = $lb->getConnection( DB_MASTER, [], '' );
$this->assertEquals( '', $db->getDomainID(), "Null domain used" );
$this->assertSame( '', $db->getDomainID(), "Null domain used" );
$this->assertEquals(
$this->quoteTable( $db, 'page' ),

View file

@ -52,7 +52,7 @@ class LocalFileTest extends MediaWikiTestCase {
* @covers File::getHashPath
*/
public function testGetHashPath() {
$this->assertEquals( '', $this->file_hl0->getHashPath() );
$this->assertSame( '', $this->file_hl0->getHashPath() );
$this->assertEquals( 'a/a2/', $this->file_hl2->getHashPath() );
$this->assertEquals( 'c/c4/', $this->file_lc->getHashPath() );
}

View file

@ -14,7 +14,7 @@ class HttpTest extends MediaWikiTestCase {
$this->hideDeprecated( 'Http::getProxy' );
$this->setMwGlobals( 'wgHTTPProxy', false );
$this->assertEquals(
$this->assertSame(
'',
Http::getProxy(),
'default setting'

View file

@ -168,7 +168,7 @@ class LinkRendererTest extends MediaWikiLangTestCase {
$linkRenderer = new LinkRenderer( $titleFormatter, $linkCache, $nsInfo );
$linkRenderer->setStubThreshold( 0 );
$this->assertEquals(
$this->assertSame(
'',
$linkRenderer->getLinkClasses( $foobarTitle )
);
@ -186,7 +186,7 @@ class LinkRendererTest extends MediaWikiLangTestCase {
);
$linkRenderer->setStubThreshold( 20 );
$this->assertEquals(
$this->assertSame(
'',
$linkRenderer->getLinkClasses( $userTitle )
);

View file

@ -718,7 +718,7 @@ END
] );
$context = $this->getResourceLoaderContext( [], $rl );
$this->assertEquals(
$this->assertSame(
'',
$rl->getCombinedVersion( $context, [] ),
'empty list'

View file

@ -222,7 +222,7 @@ class SiteTest extends MediaWikiTestCase {
$path = '//acme.com/'; // protocol-relative URL
$site->setPath( $type, $path );
$this->assertEquals( '', $site->getProtocol() );
$this->assertSame( '', $site->getProtocol() );
}
public static function provideGetPageUrl() {

View file

@ -1799,7 +1799,7 @@ class LanguageTest extends LanguageClassesTestCase {
$s = $lang->getMessageFromDB( 'word-separator' );
$c = $lang->getMessageFromDB( 'comma-separator' );
$this->assertEquals( '', $lang->listToText( [] ) );
$this->assertSame( '', $lang->listToText( [] ) );
$this->assertEquals( 'a', $lang->listToText( [ 'a' ] ) );
$this->assertEquals( "a{$and}{$s}b", $lang->listToText( [ 'a', 'b' ] ) );
$this->assertEquals( "a{$c}b{$and}{$s}c", $lang->listToText( [ 'a', 'b', 'c' ] ) );

View file

@ -185,7 +185,7 @@ abstract class DumpTestCase extends MediaWikiLangTestCase {
// 2. Do the real output checking on our own.
$lines = explode( "\n", $this->getActualOutput() );
$this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
$this->assertEquals( '', array_pop( $lines ), "Output ends in LF" );
$this->assertSame( '', array_pop( $lines ), "Output ends in LF" );
$timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
foreach ( $lines as $line ) {
$this->assertRegExp(

View file

@ -7,7 +7,7 @@ class PasswordFactoryTest extends MediaWikiUnitTestCase {
public function testConstruct() {
$pf = new PasswordFactory();
$this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) );
$this->assertEquals( '', $pf->getDefaultType() );
$this->assertSame( '', $pf->getDefaultType() );
$pf = new PasswordFactory( [
'foo' => [ 'class' => 'FooPassword' ],