From: Antoine Musso Date: Sun, 6 Feb 2011 13:59:06 +0000 (+0000) Subject: Improve PHPUnit code coverage X-Git-Tag: 1.31.0-rc.0~32164 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=da4f37facd1ba06a7329aeae683ab555dcb067d6;p=lhc%2Fweb%2Fwiklou.git Improve PHPUnit code coverage XmlJs: - simple class == simple test GlobalFunctions: - wfArrayDiff2: simple test, please add more XmlSelectTest: - test for constructor - tests for setDefault. Found a potential bug when setting a default after options have been added. The default option will not get marked as selected! We should change our code to generates HTML for options at rendering time or throw an exception that default can not be used if an option is present. MediaWiki: - basic placeholder generated by PHPUnit - tests for setVal() / getVal() --- diff --git a/tests/phpunit/includes/GlobalTest.php b/tests/phpunit/includes/GlobalTest.php index 26a5165e74..1a4918e760 100644 --- a/tests/phpunit/includes/GlobalTest.php +++ b/tests/phpunit/includes/GlobalTest.php @@ -17,6 +17,30 @@ class GlobalTest extends MediaWikiTestCase { $wgReadOnlyFile = $this->originals['wgReadOnlyFile']; } + /** @dataProvider provideForWfArrayDiff2 */ + public function testWfArrayDiff2( $a, $b, $expected ) { + $this->assertEquals( + wfArrayDiff2( $a, $b), $expected + ); + } + + // @todo Provide more tests + public function provideForWfArrayDiff2() { + // $a $b $expected + return array( + array( + array( 'a', 'b'), + array( 'a', 'b'), + array(), + ), + array( + array( array( 'a'), array( 'a', 'b', 'c' )), + array( array( 'a'), array( 'a', 'b' )), + array( 1 => array( 'a', 'b', 'c' ) ), + ), + ); + } + function testRandom() { # This could hypothetically fail, but it shouldn't ;) $this->assertFalse( diff --git a/tests/phpunit/includes/MediaWikiTest.php b/tests/phpunit/includes/MediaWikiTest.php new file mode 100644 index 0000000000..af582655d6 --- /dev/null +++ b/tests/phpunit/includes/MediaWikiTest.php @@ -0,0 +1,182 @@ +object = new MediaWiki; + } + + protected function tearDown() { + $this->object = NULL; + } + + /** + * Test case insentiveness for get / set + */ + public function testSetGetValKeyInsentiveness() { + + // set with lower case key + $value = 'SomeValue'; + $this->object->setVal( 'foobar', $value ); + + $this->assertEquals( + $this->object->getVal( 'foobar' ), 'SomeValue', + 'lower case key set, getting lower case key' + ); + $this->assertEquals( + $this->object->getVal( 'FOOBAR' ), 'SomeValue', + 'lower case key set, getting upper case key' + ); + + // set with Mixed case key + $value = 'SomeValue2'; + $this->object->setVal( 'FooBar', $value ); + + $this->assertEquals( + $this->object->getVal( 'foobar' ), 'SomeValue2', + 'mixed case key set, getting lower case key' + ); + $this->assertEquals( + $this->object->getVal( 'FOOBAR' ), 'SomeValue2', + 'mixed case key set, getting upper case key' + ); + } + + public function testGetValWithDefault() { + $this->assertEmpty( + $this->object->getVal( 'NonExistent' ), + 'Non existent key return empty string' + ); + $this->assertEquals( + $this->object->getVal( 'NonExistent2', 'Default Value' ), 'Default Value', + 'Non existent key with default given, should give default' + ); + } + + /** + * @todo Implement testPerformRequestForTitle(). + */ + public function testPerformRequestForTitle() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testCheckMaxLag(). + */ + public function testCheckMaxLag() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testCheckInitialQueries(). + */ + public function testCheckInitialQueries() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testPreliminaryChecks(). + */ + public function testPreliminaryChecks() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testHandleSpecialCases(). + */ + public function testHandleSpecialCases() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testArticleFromTitle(). + */ + public function testArticleFromTitle() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testGetAction(). + */ + public function testGetAction() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testInitializeArticle(). + */ + public function testInitializeArticle() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testFinalCleanup(). + */ + public function testFinalCleanup() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testDoJobs(). + */ + public function testDoJobs() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testRestInPeace(). + */ + public function testRestInPeace() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @todo Implement testPerformAction(). + */ + public function testPerformAction() { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } +} diff --git a/tests/phpunit/includes/XmlJsTest.php b/tests/phpunit/includes/XmlJsTest.php new file mode 100644 index 0000000000..9f759f30b1 --- /dev/null +++ b/tests/phpunit/includes/XmlJsTest.php @@ -0,0 +1,9 @@ +assertNull( $obj->value ); + $obj = new XmlJsCode( '' ); + $this->assertSame( $obj->value, '' ); + } +} diff --git a/tests/phpunit/includes/XmlSelectTest.php b/tests/phpunit/includes/XmlSelectTest.php index a4b0f8f1ce..c17cba224c 100644 --- a/tests/phpunit/includes/XmlSelectTest.php +++ b/tests/phpunit/includes/XmlSelectTest.php @@ -17,12 +17,48 @@ class XmlSelectTest extends MediaWikiTestCase { $this->assertEquals( '', $this->select->getHTML() ); } + /** + * Parameters are $name (false), $id (false), $default (false) + * @dataProvider provideConstructionParameters + */ + public function testConstructParameters( $name, $id, $default, $expected ) { + $this->select = new XmlSelect( $name, $id, $default ); + $this->assertEquals( $expected, $this->select->getHTML() ); + } + + /** + * Provide parameters for testConstructParameters() which use three + * parameters: + * - $name (default: false) + * - $id (default: false) + * - $default (default: false) + * Provides a fourth parameters representing the expected HTML output + * + */ + public function provideConstructionParameters() { + return array( + /** + * Values are set following a 3-bit Gray code where two successive + * values differ by only one value. + * See http://en.wikipedia.org/wiki/Gray_code + */ + # $name $id $default + array( false , false, false, '' ), + array( false , false, 'foo', '' ), + array( false , 'id' , 'foo', '' ), + array( false , 'id' , false, '' ), + array( 'name', 'id' , false, '' ), + array( 'name', 'id' , 'foo', '' ), + array( 'name', false, 'foo', '' ), + array( 'name', false, false, '' ), + ); + } + # Begin XmlSelect::addOption() similar to Xml::option public function testAddOption() { $this->select->addOption( 'foo' ); $this->assertEquals( '', $this->select->getHTML() ); } - public function testAddOptionWithDefault() { $this->select->addOption( 'foo', true ); $this->assertEquals( '', $this->select->getHTML() ); @@ -37,4 +73,33 @@ class XmlSelectTest extends MediaWikiTestCase { } # End XmlSelect::addOption() similar to Xml::option + public function testSetDefault() { + $this->select->setDefault( 'bar1' ); + $this->select->addOption( 'foo1' ); + $this->select->addOption( 'bar1' ); + $this->select->addOption( 'foo2' ); + $this->assertEquals( +'', $this->select->getHTML() ); + } + + /** + * Adding default later on should set the correct selection or + * raise an exception. + * To handle this, we need to render the options in getHtml() + */ + public function testSetDefaultAfterAddingOptions() { + $this->markTestSkipped( 'XmlSelect::setDefault() need to apply to previously added options'); + + $this->select->addOption( 'foo1' ); + $this->select->addOption( 'bar1' ); + $this->select->addOption( 'foo2' ); + $this->select->setDefault( 'bar1' ); # setting default after adding options + $this->assertEquals( +'', $this->select->getHTML() ); + } + }