tests: Prefer PHPUnit's assertSame() when comparing empty strings
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Tue, 17 Sep 2019 14:28:35 +0000 (16:28 +0200)
committerKrinkle <krinklemail@gmail.com>
Fri, 20 Sep 2019 15:27:58 +0000 (15:27 +0000)
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

18 files changed:
tests/phpunit/MediaWikiCoversValidator.php
tests/phpunit/includes/FauxRequestTest.php
tests/phpunit/includes/HtmlTest.php
tests/phpunit/includes/XmlTest.php
tests/phpunit/includes/changes/EnhancedChangesListTest.php
tests/phpunit/includes/content/ContentHandlerTest.php
tests/phpunit/includes/content/UnknownContentHandlerTest.php
tests/phpunit/includes/content/UnknownContentTest.php
tests/phpunit/includes/content/WikitextContentHandlerTest.php
tests/phpunit/includes/db/LBFactoryTest.php
tests/phpunit/includes/filerepo/file/LocalFileTest.php
tests/phpunit/includes/http/HttpTest.php
tests/phpunit/includes/linker/LinkRendererTest.php
tests/phpunit/includes/resourceloader/ResourceLoaderTest.php
tests/phpunit/includes/site/SiteTest.php
tests/phpunit/languages/LanguageTest.php
tests/phpunit/maintenance/DumpTestCase.php
tests/phpunit/unit/includes/password/PasswordFactoryTest.php

index ce3f2e2..ca400bf 100644 (file)
@@ -45,6 +45,6 @@ trait MediaWikiCoversValidator {
                        }
                }
 
-               $this->assertEquals( '', $bad );
+               $this->assertSame( '', $bad );
        }
 }
index c054caa..7b7f6b9 100644 (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() );
        }
 }
index 5d83b7e..4401410 100644 (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'
index ab9abbb..f95505b 100644 (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'
                );
        }
index 1511d46..895a3cb 100644 (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() {
index e73b554..39b5380 100644 (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() {
index bc1d3c6..0572289 100644 (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() {
index fd8e3ba..2b6f3f1 100644 (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() );
        }
 
        /**
index b372e37..e18cecb 100644 (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() {
index c789e83..f4ddca2 100644 (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' ),
index 8f37805..fe5dba1 100644 (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() );
        }
index ef499a1..94b8c91 100644 (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'
index b26a247..d176e39 100644 (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 )
                );
index 94e3461..be11d53 100644 (file)
@@ -718,7 +718,7 @@ END
                ] );
                $context = $this->getResourceLoaderContext( [], $rl );
 
-               $this->assertEquals(
+               $this->assertSame(
                        '',
                        $rl->getCombinedVersion( $context, [] ),
                        'empty list'
index 9ad61b2..2119d31 100644 (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() {
index 2f6fa39..628d248 100644 (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' ] ) );
index 7647915..0387535 100644 (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(
index cbfddd4..2c852e0 100644 (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' ],