From e06ce9f4676699f4b9a11126d35a421f87b7d006 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Tue, 17 Sep 2019 16:28:35 +0200 Subject: [PATCH] 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 --- tests/phpunit/MediaWikiCoversValidator.php | 2 +- tests/phpunit/includes/FauxRequestTest.php | 8 ++++---- tests/phpunit/includes/HtmlTest.php | 4 ++-- tests/phpunit/includes/XmlTest.php | 2 +- .../phpunit/includes/changes/EnhancedChangesListTest.php | 2 +- tests/phpunit/includes/content/ContentHandlerTest.php | 2 +- .../includes/content/UnknownContentHandlerTest.php | 2 +- tests/phpunit/includes/content/UnknownContentTest.php | 2 +- .../includes/content/WikitextContentHandlerTest.php | 2 +- tests/phpunit/includes/db/LBFactoryTest.php | 9 ++++----- tests/phpunit/includes/filerepo/file/LocalFileTest.php | 2 +- tests/phpunit/includes/http/HttpTest.php | 2 +- tests/phpunit/includes/linker/LinkRendererTest.php | 4 ++-- .../includes/resourceloader/ResourceLoaderTest.php | 2 +- tests/phpunit/includes/site/SiteTest.php | 2 +- tests/phpunit/languages/LanguageTest.php | 2 +- tests/phpunit/maintenance/DumpTestCase.php | 2 +- .../unit/includes/password/PasswordFactoryTest.php | 2 +- 18 files changed, 26 insertions(+), 27 deletions(-) diff --git a/tests/phpunit/MediaWikiCoversValidator.php b/tests/phpunit/MediaWikiCoversValidator.php index ce3f2e281d..ca400bf8aa 100644 --- a/tests/phpunit/MediaWikiCoversValidator.php +++ b/tests/phpunit/MediaWikiCoversValidator.php @@ -45,6 +45,6 @@ trait MediaWikiCoversValidator { } } - $this->assertEquals( '', $bad ); + $this->assertSame( '', $bad ); } } diff --git a/tests/phpunit/includes/FauxRequestTest.php b/tests/phpunit/includes/FauxRequestTest.php index c054caa06f..7b7f6b9d43 100644 --- a/tests/phpunit/includes/FauxRequestTest.php +++ b/tests/phpunit/includes/FauxRequestTest.php @@ -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() ); } } diff --git a/tests/phpunit/includes/HtmlTest.php b/tests/phpunit/includes/HtmlTest.php index 5d83b7eb5b..4401410ef3 100644 --- a/tests/phpunit/includes/HtmlTest.php +++ b/tests/phpunit/includes/HtmlTest.php @@ -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' diff --git a/tests/phpunit/includes/XmlTest.php b/tests/phpunit/includes/XmlTest.php index ab9abbb429..f95505bcbc 100644 --- a/tests/phpunit/includes/XmlTest.php +++ b/tests/phpunit/includes/XmlTest.php @@ -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' ); } diff --git a/tests/phpunit/includes/changes/EnhancedChangesListTest.php b/tests/phpunit/includes/changes/EnhancedChangesListTest.php index 1511d46c5c..895a3cbdaa 100644 --- a/tests/phpunit/includes/changes/EnhancedChangesListTest.php +++ b/tests/phpunit/includes/changes/EnhancedChangesListTest.php @@ -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() { diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index e73b554561..39b538025d 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -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() { diff --git a/tests/phpunit/includes/content/UnknownContentHandlerTest.php b/tests/phpunit/includes/content/UnknownContentHandlerTest.php index bc1d3c6405..057228975d 100644 --- a/tests/phpunit/includes/content/UnknownContentHandlerTest.php +++ b/tests/phpunit/includes/content/UnknownContentHandlerTest.php @@ -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() { diff --git a/tests/phpunit/includes/content/UnknownContentTest.php b/tests/phpunit/includes/content/UnknownContentTest.php index fd8e3ba585..2b6f3f1ff2 100644 --- a/tests/phpunit/includes/content/UnknownContentTest.php +++ b/tests/phpunit/includes/content/UnknownContentTest.php @@ -139,7 +139,7 @@ class UnknownContentTest extends MediaWikiLangTestCase { public function testGetWikitextForTransclusion() { $content = $this->newContent( 'hello world.' ); - $this->assertEquals( '', $content->getWikitextForTransclusion() ); + $this->assertFalse( $content->getWikitextForTransclusion() ); } /** diff --git a/tests/phpunit/includes/content/WikitextContentHandlerTest.php b/tests/phpunit/includes/content/WikitextContentHandlerTest.php index b372e37c9f..e18cecb402 100644 --- a/tests/phpunit/includes/content/WikitextContentHandlerTest.php +++ b/tests/phpunit/includes/content/WikitextContentHandlerTest.php @@ -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() { diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php index c789e83082..f4ddca2482 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -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' ), diff --git a/tests/phpunit/includes/filerepo/file/LocalFileTest.php b/tests/phpunit/includes/filerepo/file/LocalFileTest.php index 8f378052a3..fe5dba1beb 100644 --- a/tests/phpunit/includes/filerepo/file/LocalFileTest.php +++ b/tests/phpunit/includes/filerepo/file/LocalFileTest.php @@ -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() ); } diff --git a/tests/phpunit/includes/http/HttpTest.php b/tests/phpunit/includes/http/HttpTest.php index ef499a1ee7..94b8c91b4e 100644 --- a/tests/phpunit/includes/http/HttpTest.php +++ b/tests/phpunit/includes/http/HttpTest.php @@ -14,7 +14,7 @@ class HttpTest extends MediaWikiTestCase { $this->hideDeprecated( 'Http::getProxy' ); $this->setMwGlobals( 'wgHTTPProxy', false ); - $this->assertEquals( + $this->assertSame( '', Http::getProxy(), 'default setting' diff --git a/tests/phpunit/includes/linker/LinkRendererTest.php b/tests/phpunit/includes/linker/LinkRendererTest.php index b26a247c80..d176e39145 100644 --- a/tests/phpunit/includes/linker/LinkRendererTest.php +++ b/tests/phpunit/includes/linker/LinkRendererTest.php @@ -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 ) ); diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php index 94e346139a..be11d538a5 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderTest.php @@ -718,7 +718,7 @@ END ] ); $context = $this->getResourceLoaderContext( [], $rl ); - $this->assertEquals( + $this->assertSame( '', $rl->getCombinedVersion( $context, [] ), 'empty list' diff --git a/tests/phpunit/includes/site/SiteTest.php b/tests/phpunit/includes/site/SiteTest.php index 9ad61b2359..2119d31064 100644 --- a/tests/phpunit/includes/site/SiteTest.php +++ b/tests/phpunit/includes/site/SiteTest.php @@ -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() { diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 2f6fa39b36..628d248938 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -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' ] ) ); diff --git a/tests/phpunit/maintenance/DumpTestCase.php b/tests/phpunit/maintenance/DumpTestCase.php index 7647915ab9..03875351fb 100644 --- a/tests/phpunit/maintenance/DumpTestCase.php +++ b/tests/phpunit/maintenance/DumpTestCase.php @@ -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( diff --git a/tests/phpunit/unit/includes/password/PasswordFactoryTest.php b/tests/phpunit/unit/includes/password/PasswordFactoryTest.php index cbfddd4d72..2c852e0c75 100644 --- a/tests/phpunit/unit/includes/password/PasswordFactoryTest.php +++ b/tests/phpunit/unit/includes/password/PasswordFactoryTest.php @@ -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' ], -- 2.20.1