From 5a8609073c7f04d539d7550fe1ccca01df765572 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Fri, 22 Mar 2013 15:16:35 +0100 Subject: [PATCH] ContentHandlerTest: Use dataProvider Changing a global 3 times in a test is bad, using dataProvider for two tests and split another in 3, because there are different. Using mwglobals to set the globals, than restoring is done automatically. Change-Id: Ie0f9ce0be39e457d18752e905d6deeb1803cb4c3 --- .../includes/content/ContentHandlerTest.php | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index 19ceadd5cc..2b77978426 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -145,62 +145,72 @@ class ContentHandlerTest extends MediaWikiTestCase { $this->assertEquals( $expected->getCode(), $lang->getCode() ); } - public function testGetContentText_Null() { - global $wgContentHandlerTextFallback; + public static function dataGetContentText_Null() { + return array( + array( 'fail' ), + array( 'serialize' ), + array( 'ignore' ), + ); + } - $content = null; + /** + * @dataProvider dataGetContentText_Null + */ + public function testGetContentText_Null( $contentHandlerTextFallback ) { + $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback ); - $wgContentHandlerTextFallback = 'fail'; - $text = ContentHandler::getContentText( $content ); - $this->assertEquals( '', $text ); + $content = null; - $wgContentHandlerTextFallback = 'serialize'; $text = ContentHandler::getContentText( $content ); $this->assertEquals( '', $text ); + } - $wgContentHandlerTextFallback = 'ignore'; - $text = ContentHandler::getContentText( $content ); - $this->assertEquals( '', $text ); + public static function dataGetContentText_TextContent() { + return array( + array( 'fail' ), + array( 'serialize' ), + array( 'ignore' ), + ); } - public function testGetContentText_TextContent() { - global $wgContentHandlerTextFallback; + /** + * @dataProvider dataGetContentText_TextContent + */ + public function testGetContentText_TextContent( $contentHandlerTextFallback ) { + $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback ); $content = new WikitextContent( "hello world" ); - $wgContentHandlerTextFallback = 'fail'; - $text = ContentHandler::getContentText( $content ); - $this->assertEquals( $content->getNativeData(), $text ); - - $wgContentHandlerTextFallback = 'serialize'; - $text = ContentHandler::getContentText( $content ); - $this->assertEquals( $content->serialize(), $text ); - - $wgContentHandlerTextFallback = 'ignore'; $text = ContentHandler::getContentText( $content ); $this->assertEquals( $content->getNativeData(), $text ); } - public function testGetContentText_NonTextContent() { - global $wgContentHandlerTextFallback; + /** + * ContentHandler::getContentText should have thrown an exception for non-text Content object + * @expectedException MWException + */ + public function testGetContentText_NonTextContent_fail() { + $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' ); $content = new DummyContentForTesting( "hello world" ); - $wgContentHandlerTextFallback = 'fail'; + ContentHandler::getContentText( $content ); + } - try { - $text = ContentHandler::getContentText( $content ); + public function testGetContentText_NonTextContent_serialize() { + $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' ); - $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" ); - } catch ( MWException $ex ) { - // as expected - } + $content = new DummyContentForTesting( "hello world" ); - $wgContentHandlerTextFallback = 'serialize'; $text = ContentHandler::getContentText( $content ); $this->assertEquals( $content->serialize(), $text ); + } + + public function testGetContentText_NonTextContent_ignore() { + $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' ); + + $content = new DummyContentForTesting( "hello world" ); - $wgContentHandlerTextFallback = 'ignore'; $text = ContentHandler::getContentText( $content ); $this->assertNull( $text ); } -- 2.20.1