r81596 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81595‎ | r81596 | r81597 >
Date:13:59, 6 February 2011
Author:hashar
Status:resolved (Comments)
Tags:
Comment:
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()
Modified paths:
  • /trunk/phase3/tests/phpunit/includes/GlobalTest.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/MediaWikiTest.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/XmlJsTest.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/XmlSelectTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/XmlJsTest.php
@@ -0,0 +1,9 @@
 2+<?php
 3+class XmlJs extends PHPUnit_Framework_TestCase {
 4+ public function testConstruction() {
 5+ $obj = new XmlJsCode( null );
 6+ $this->assertNull( $obj->value );
 7+ $obj = new XmlJsCode( '' );
 8+ $this->assertSame( $obj->value, '' );
 9+ }
 10+}
Property changes on: trunk/phase3/tests/phpunit/includes/XmlJsTest.php
___________________________________________________________________
Added: svn:eol-style
111 + native
Index: trunk/phase3/tests/phpunit/includes/XmlSelectTest.php
@@ -17,12 +17,48 @@
1818 $this->assertEquals( '<select></select>', $this->select->getHTML() );
1919 }
2020
 21+ /**
 22+ * Parameters are $name (false), $id (false), $default (false)
 23+ * @dataProvider provideConstructionParameters
 24+ */
 25+ public function testConstructParameters( $name, $id, $default, $expected ) {
 26+ $this->select = new XmlSelect( $name, $id, $default );
 27+ $this->assertEquals( $expected, $this->select->getHTML() );
 28+ }
 29+
 30+ /**
 31+ * Provide parameters for testConstructParameters() which use three
 32+ * parameters:
 33+ * - $name (default: false)
 34+ * - $id (default: false)
 35+ * - $default (default: false)
 36+ * Provides a fourth parameters representing the expected HTML output
 37+ *
 38+ */
 39+ public function provideConstructionParameters() {
 40+ return array(
 41+ /**
 42+ * Values are set following a 3-bit Gray code where two successive
 43+ * values differ by only one value.
 44+ * See http://en.wikipedia.org/wiki/Gray_code
 45+ */
 46+ # $name $id $default
 47+ array( false , false, false, '<select></select>' ),
 48+ array( false , false, 'foo', '<select></select>' ),
 49+ array( false , 'id' , 'foo', '<select id="id"></select>' ),
 50+ array( false , 'id' , false, '<select id="id"></select>' ),
 51+ array( 'name', 'id' , false, '<select name="name" id="id"></select>' ),
 52+ array( 'name', 'id' , 'foo', '<select name="name" id="id"></select>' ),
 53+ array( 'name', false, 'foo', '<select name="name"></select>' ),
 54+ array( 'name', false, false, '<select name="name"></select>' ),
 55+ );
 56+ }
 57+
2158 # Begin XmlSelect::addOption() similar to Xml::option
2259 public function testAddOption() {
2360 $this->select->addOption( 'foo' );
2461 $this->assertEquals( '<select><option value="foo">foo</option></select>', $this->select->getHTML() );
2562 }
26 -
2763 public function testAddOptionWithDefault() {
2864 $this->select->addOption( 'foo', true );
2965 $this->assertEquals( '<select><option value="1">foo</option></select>', $this->select->getHTML() );
@@ -37,4 +73,33 @@
3874 }
3975 # End XmlSelect::addOption() similar to Xml::option
4076
 77+ public function testSetDefault() {
 78+ $this->select->setDefault( 'bar1' );
 79+ $this->select->addOption( 'foo1' );
 80+ $this->select->addOption( 'bar1' );
 81+ $this->select->addOption( 'foo2' );
 82+ $this->assertEquals(
 83+'<select><option value="foo1">foo1</option>
 84+<option value="bar1" selected="selected">bar1</option>
 85+<option value="foo2">foo2</option></select>', $this->select->getHTML() );
 86+ }
 87+
 88+ /**
 89+ * Adding default later on should set the correct selection or
 90+ * raise an exception.
 91+ * To handle this, we need to render the options in getHtml()
 92+ */
 93+ public function testSetDefaultAfterAddingOptions() {
 94+ $this->markTestSkipped( 'XmlSelect::setDefault() need to apply to previously added options');
 95+
 96+ $this->select->addOption( 'foo1' );
 97+ $this->select->addOption( 'bar1' );
 98+ $this->select->addOption( 'foo2' );
 99+ $this->select->setDefault( 'bar1' ); # setting default after adding options
 100+ $this->assertEquals(
 101+'<select><option value="foo1">foo1</option>
 102+<option value="bar1" selected="selected">bar1</option>
 103+<option value="foo2">foo2</option></select>', $this->select->getHTML() );
 104+ }
 105+
41106 }
Index: trunk/phase3/tests/phpunit/includes/GlobalTest.php
@@ -17,6 +17,30 @@
1818 $wgReadOnlyFile = $this->originals['wgReadOnlyFile'];
1919 }
2020
 21+ /** @dataProvider provideForWfArrayDiff2 */
 22+ public function testWfArrayDiff2( $a, $b, $expected ) {
 23+ $this->assertEquals(
 24+ wfArrayDiff2( $a, $b), $expected
 25+ );
 26+ }
 27+
 28+ // @todo Provide more tests
 29+ public function provideForWfArrayDiff2() {
 30+ // $a $b $expected
 31+ return array(
 32+ array(
 33+ array( 'a', 'b'),
 34+ array( 'a', 'b'),
 35+ array(),
 36+ ),
 37+ array(
 38+ array( array( 'a'), array( 'a', 'b', 'c' )),
 39+ array( array( 'a'), array( 'a', 'b' )),
 40+ array( 1 => array( 'a', 'b', 'c' ) ),
 41+ ),
 42+ );
 43+ }
 44+
2145 function testRandom() {
2246 # This could hypothetically fail, but it shouldn't ;)
2347 $this->assertFalse(
Index: trunk/phase3/tests/phpunit/includes/MediaWikiTest.php
@@ -0,0 +1,182 @@
 2+<?php
 3+/**
 4+ * Test class for MediaWiki.
 5+ * Generated by PHPUnit on 2011-02-06 at 11:41:23.
 6+ */
 7+class MediaWikiTest extends PHPUnit_Framework_TestCase {
 8+ /**
 9+ * @var MediaWiki
 10+ */
 11+ protected $object;
 12+
 13+ protected function setUp() {
 14+ $this->object = new MediaWiki;
 15+ }
 16+
 17+ protected function tearDown() {
 18+ $this->object = NULL;
 19+ }
 20+
 21+ /**
 22+ * Test case insentiveness for get / set
 23+ */
 24+ public function testSetGetValKeyInsentiveness() {
 25+
 26+ // set with lower case key
 27+ $value = 'SomeValue';
 28+ $this->object->setVal( 'foobar', $value );
 29+
 30+ $this->assertEquals(
 31+ $this->object->getVal( 'foobar' ), 'SomeValue',
 32+ 'lower case key set, getting lower case key'
 33+ );
 34+ $this->assertEquals(
 35+ $this->object->getVal( 'FOOBAR' ), 'SomeValue',
 36+ 'lower case key set, getting upper case key'
 37+ );
 38+
 39+ // set with Mixed case key
 40+ $value = 'SomeValue2';
 41+ $this->object->setVal( 'FooBar', $value );
 42+
 43+ $this->assertEquals(
 44+ $this->object->getVal( 'foobar' ), 'SomeValue2',
 45+ 'mixed case key set, getting lower case key'
 46+ );
 47+ $this->assertEquals(
 48+ $this->object->getVal( 'FOOBAR' ), 'SomeValue2',
 49+ 'mixed case key set, getting upper case key'
 50+ );
 51+ }
 52+
 53+ public function testGetValWithDefault() {
 54+ $this->assertEmpty(
 55+ $this->object->getVal( 'NonExistent' ),
 56+ 'Non existent key return empty string'
 57+ );
 58+ $this->assertEquals(
 59+ $this->object->getVal( 'NonExistent2', 'Default Value' ), 'Default Value',
 60+ 'Non existent key with default given, should give default'
 61+ );
 62+ }
 63+
 64+ /**
 65+ * @todo Implement testPerformRequestForTitle().
 66+ */
 67+ public function testPerformRequestForTitle() {
 68+ // Remove the following lines when you implement this test.
 69+ $this->markTestIncomplete(
 70+ 'This test has not been implemented yet.'
 71+ );
 72+ }
 73+
 74+ /**
 75+ * @todo Implement testCheckMaxLag().
 76+ */
 77+ public function testCheckMaxLag() {
 78+ // Remove the following lines when you implement this test.
 79+ $this->markTestIncomplete(
 80+ 'This test has not been implemented yet.'
 81+ );
 82+ }
 83+
 84+ /**
 85+ * @todo Implement testCheckInitialQueries().
 86+ */
 87+ public function testCheckInitialQueries() {
 88+ // Remove the following lines when you implement this test.
 89+ $this->markTestIncomplete(
 90+ 'This test has not been implemented yet.'
 91+ );
 92+ }
 93+
 94+ /**
 95+ * @todo Implement testPreliminaryChecks().
 96+ */
 97+ public function testPreliminaryChecks() {
 98+ // Remove the following lines when you implement this test.
 99+ $this->markTestIncomplete(
 100+ 'This test has not been implemented yet.'
 101+ );
 102+ }
 103+
 104+ /**
 105+ * @todo Implement testHandleSpecialCases().
 106+ */
 107+ public function testHandleSpecialCases() {
 108+ // Remove the following lines when you implement this test.
 109+ $this->markTestIncomplete(
 110+ 'This test has not been implemented yet.'
 111+ );
 112+ }
 113+
 114+ /**
 115+ * @todo Implement testArticleFromTitle().
 116+ */
 117+ public function testArticleFromTitle() {
 118+ // Remove the following lines when you implement this test.
 119+ $this->markTestIncomplete(
 120+ 'This test has not been implemented yet.'
 121+ );
 122+ }
 123+
 124+ /**
 125+ * @todo Implement testGetAction().
 126+ */
 127+ public function testGetAction() {
 128+ // Remove the following lines when you implement this test.
 129+ $this->markTestIncomplete(
 130+ 'This test has not been implemented yet.'
 131+ );
 132+ }
 133+
 134+ /**
 135+ * @todo Implement testInitializeArticle().
 136+ */
 137+ public function testInitializeArticle() {
 138+ // Remove the following lines when you implement this test.
 139+ $this->markTestIncomplete(
 140+ 'This test has not been implemented yet.'
 141+ );
 142+ }
 143+
 144+ /**
 145+ * @todo Implement testFinalCleanup().
 146+ */
 147+ public function testFinalCleanup() {
 148+ // Remove the following lines when you implement this test.
 149+ $this->markTestIncomplete(
 150+ 'This test has not been implemented yet.'
 151+ );
 152+ }
 153+
 154+ /**
 155+ * @todo Implement testDoJobs().
 156+ */
 157+ public function testDoJobs() {
 158+ // Remove the following lines when you implement this test.
 159+ $this->markTestIncomplete(
 160+ 'This test has not been implemented yet.'
 161+ );
 162+ }
 163+
 164+ /**
 165+ * @todo Implement testRestInPeace().
 166+ */
 167+ public function testRestInPeace() {
 168+ // Remove the following lines when you implement this test.
 169+ $this->markTestIncomplete(
 170+ 'This test has not been implemented yet.'
 171+ );
 172+ }
 173+
 174+ /**
 175+ * @todo Implement testPerformAction().
 176+ */
 177+ public function testPerformAction() {
 178+ // Remove the following lines when you implement this test.
 179+ $this->markTestIncomplete(
 180+ 'This test has not been implemented yet.'
 181+ );
 182+ }
 183+}
Property changes on: trunk/phase3/tests/phpunit/includes/MediaWikiTest.php
___________________________________________________________________
Added: svn:eol-style
1184 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r81880Add support of assertEmpty (used since r81596) for phpunit < 3.5.0platonides09:51, 10 February 2011

Comments

#Comment by Platonides (talk | contribs)   21:39, 6 February 2011

Call to undefined method MediaWikiTest::assertEmpty()

Which phpunit version adds it?

#Comment by Hashar (talk | contribs)   07:15, 7 February 2011

assertEmpty was introduced with version 3.5.0

At this point, I wanted to ensure we received empty string. Could probably be replaced by:

  assertTrue( $this->object->getVal( 'NonExistent' ) ===  )

Status & tagging log