r82959 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82958‎ | r82959 | r82960 >
Date:20:58, 28 February 2011
Author:hashar
Status:ok (Comments)
Tags:
Comment:
FormOptions: adds doc & tests
Modified paths:
  • /trunk/phase3/includes/FormOptions.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/FormOptionsInitializationTest.php (added) (history)
  • /trunk/phase3/tests/phpunit/includes/FormOptionsTest.php (added) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/FormOptionsTest.php
@@ -0,0 +1,250 @@
 2+<?php
 3+/**
 4+ * This file host two test case classes for the MediaWiki FormOptions class:
 5+ * - FormOptionsInitializationTest : tests initialization of the class.
 6+ * - FormOptionsTest : tests methods an on instance
 7+ *
 8+ * The split let us take advantage of setting up a fixture for the methods
 9+ * tests.
 10+ */
 11+
 12+/**
 13+ * Test class for FormOptions methods.
 14+ * Generated by PHPUnit on 2011-02-28 at 20:46:27.
 15+ *
 16+ * Copyright © 2011, Ashar Voultoiz
 17+ *
 18+ * @author Ashar Voultoiz
 19+ */
 20+class FormOptionsTest extends MediaWikiTestCase {
 21+ /**
 22+ * @var FormOptions
 23+ */
 24+ protected $object;
 25+
 26+ /**
 27+ * Instanciates a FormOptions object to play with.
 28+ * FormOptions::add() is tested by the class FormOptionsInitializationTest
 29+ * so we assume the function is well tested already an use it to create
 30+ * the fixture.
 31+ */
 32+ protected function setUp() {
 33+ $this->object = new FormOptions;
 34+ $this->object->add( 'string1', 'string one' );
 35+ $this->object->add( 'string2', 'string two' );
 36+ $this->object->add( 'integer', 0 );
 37+ $this->object->add( 'intnull', 0, FormOptions::INTNULL );
 38+ }
 39+
 40+ /**
 41+ * @todo Implement testDelete().
 42+ */
 43+ public function testDelete() {
 44+ // Remove the following lines when you implement this test.
 45+ $this->markTestIncomplete(
 46+ 'This test has not been implemented yet.'
 47+ );
 48+ }
 49+
 50+ /** Helpers for testGuessType() */
 51+ /* @{ */
 52+ private function assertGuessBoolean( $data ) {
 53+ $this->guess( FormOptions::BOOL, $data );
 54+ }
 55+ private function assertGuessInt( $data ) {
 56+ $this->guess( FormOptions::INT, $data );
 57+ }
 58+ private function assertGuessString( $data ) {
 59+ $this->guess( FormOptions::STRING, $data );
 60+ }
 61+
 62+ /** Generic helper */
 63+ private function guess( $expected, $data ) {
 64+ $this->assertEquals(
 65+ $expected,
 66+ FormOptions::guessType( $data )
 67+ );
 68+ }
 69+ /* @} */
 70+
 71+ /**
 72+ * Reuse helpers above assertGuessBoolean assertGuessInt assertGuessString
 73+ */
 74+ public function testGuessTypeDetection() {
 75+ $this->assertGuessBoolean( true );
 76+ $this->assertGuessBoolean( false );
 77+
 78+ $this->assertGuessInt( 0 );
 79+ $this->assertGuessInt( -5 );
 80+ $this->assertGuessInt( 5 );
 81+ $this->assertGuessInt( 0x0F );
 82+
 83+ $this->assertGuessString( 'true' );
 84+ $this->assertGuessString( 'false' );
 85+ $this->assertGuessString( '5' );
 86+ $this->assertGuessString( '0' );
 87+ }
 88+
 89+ /**
 90+ * @expectedException MWException
 91+ */
 92+ public function testGuessTypeOnArrayThrowException() {
 93+ $this->object->guessType( array( 'foo' ) );
 94+ }
 95+ /**
 96+ * @expectedException MWException
 97+ */
 98+ public function testGuessTypeOnNullThrowException() {
 99+ $this->object->guessType( null );
 100+ }
 101+
 102+ /**
 103+ * @todo Implement testValidateName().
 104+ */
 105+ public function testValidateName() {
 106+ // Remove the following lines when you implement this test.
 107+ $this->markTestIncomplete(
 108+ 'This test has not been implemented yet.'
 109+ );
 110+ }
 111+
 112+ /**
 113+ * @todo Implement testSetValue().
 114+ */
 115+ public function testSetValue() {
 116+ // Remove the following lines when you implement this test.
 117+ $this->markTestIncomplete(
 118+ 'This test has not been implemented yet.'
 119+ );
 120+ }
 121+
 122+ /**
 123+ * @todo Implement testGetValue().
 124+ */
 125+ public function testGetValue() {
 126+ // Remove the following lines when you implement this test.
 127+ $this->markTestIncomplete(
 128+ 'This test has not been implemented yet.'
 129+ );
 130+ }
 131+
 132+ /**
 133+ * @todo Implement testReset().
 134+ */
 135+ public function testReset() {
 136+ // Remove the following lines when you implement this test.
 137+ $this->markTestIncomplete(
 138+ 'This test has not been implemented yet.'
 139+ );
 140+ }
 141+
 142+ /**
 143+ * @todo Implement testConsumeValue().
 144+ */
 145+ public function testConsumeValue() {
 146+ // Remove the following lines when you implement this test.
 147+ $this->markTestIncomplete(
 148+ 'This test has not been implemented yet.'
 149+ );
 150+ }
 151+
 152+ /**
 153+ * @todo Implement testConsumeValues().
 154+ */
 155+ public function testConsumeValues() {
 156+ // Remove the following lines when you implement this test.
 157+ $this->markTestIncomplete(
 158+ 'This test has not been implemented yet.'
 159+ );
 160+ }
 161+
 162+ /**
 163+ * @todo Implement testValidateIntBounds().
 164+ */
 165+ public function testValidateIntBounds() {
 166+ // Remove the following lines when you implement this test.
 167+ $this->markTestIncomplete(
 168+ 'This test has not been implemented yet.'
 169+ );
 170+ }
 171+
 172+ /**
 173+ * @todo Implement testGetUnconsumedValues().
 174+ */
 175+ public function testGetUnconsumedValues() {
 176+ // Remove the following lines when you implement this test.
 177+ $this->markTestIncomplete(
 178+ 'This test has not been implemented yet.'
 179+ );
 180+ }
 181+
 182+ /**
 183+ * @todo Implement testGetChangedValues().
 184+ */
 185+ public function testGetChangedValues() {
 186+ // Remove the following lines when you implement this test.
 187+ $this->markTestIncomplete(
 188+ 'This test has not been implemented yet.'
 189+ );
 190+ }
 191+
 192+ /**
 193+ * @todo Implement testGetAllValues().
 194+ */
 195+ public function testGetAllValues() {
 196+ // Remove the following lines when you implement this test.
 197+ $this->markTestIncomplete(
 198+ 'This test has not been implemented yet.'
 199+ );
 200+ }
 201+
 202+ /**
 203+ * @todo Implement testFetchValuesFromRequest().
 204+ */
 205+ public function testFetchValuesFromRequest() {
 206+ // Remove the following lines when you implement this test.
 207+ $this->markTestIncomplete(
 208+ 'This test has not been implemented yet.'
 209+ );
 210+ }
 211+
 212+ /**
 213+ * @todo Implement testOffsetExists().
 214+ */
 215+ public function testOffsetExists() {
 216+ // Remove the following lines when you implement this test.
 217+ $this->markTestIncomplete(
 218+ 'This test has not been implemented yet.'
 219+ );
 220+ }
 221+
 222+ /**
 223+ * @todo Implement testOffsetGet().
 224+ */
 225+ public function testOffsetGet() {
 226+ // Remove the following lines when you implement this test.
 227+ $this->markTestIncomplete(
 228+ 'This test has not been implemented yet.'
 229+ );
 230+ }
 231+
 232+ /**
 233+ * @todo Implement testOffsetSet().
 234+ */
 235+ public function testOffsetSet() {
 236+ // Remove the following lines when you implement this test.
 237+ $this->markTestIncomplete(
 238+ 'This test has not been implemented yet.'
 239+ );
 240+ }
 241+
 242+ /**
 243+ * @todo Implement testOffsetUnset().
 244+ */
 245+ public function testOffsetUnset() {
 246+ // Remove the following lines when you implement this test.
 247+ $this->markTestIncomplete(
 248+ 'This test has not been implemented yet.'
 249+ );
 250+ }
 251+}
Property changes on: trunk/phase3/tests/phpunit/includes/FormOptionsTest.php
___________________________________________________________________
Added: svn:eol-style
1252 + native
Index: trunk/phase3/tests/phpunit/includes/FormOptionsInitializationTest.php
@@ -0,0 +1,83 @@
 2+<?php
 3+/**
 4+ * This file host two test case classes for the MediaWiki FormOptions class:
 5+ * - FormOptionsInitializationTest : tests initialization of the class.
 6+ * - FormOptionsTest : tests methods an on instance
 7+ *
 8+ * The split let us take advantage of setting up a fixture for the methods
 9+ * tests.
 10+ */
 11+
 12+/**
 13+ * Dummy class to makes FormOptions::$options public.
 14+ * Used by FormOptionsInitializationTest which need to verify the $options
 15+ * array is correctly set through the FormOptions::add() function.
 16+ */
 17+class FormOptionsExposed extends FormOptions {
 18+ public $options;
 19+}
 20+
 21+/**
 22+ * Test class for FormOptions initialization
 23+ * Ensure the FormOptions::add() does what we want it to do.
 24+ *
 25+ * Generated by PHPUnit on 2011-02-28 at 20:46:27.
 26+ *
 27+ * Copyright © 2011, Ashar Voultoiz
 28+ *
 29+ * @author Ashar Voultoiz
 30+ */
 31+class FormOptionsInitializationTest extends MediaWikiTestCase {
 32+ /**
 33+ * @var FormOptions
 34+ */
 35+ protected $object;
 36+
 37+
 38+ /**
 39+ * A new fresh and empty FormOptions object to test initialization
 40+ * with.
 41+ */
 42+ protected function setUp() {
 43+ $this->object = new FormOptionsExposed();
 44+
 45+ }
 46+
 47+ public function testAddStringOption() {
 48+ $this->object->add( 'foo', 'string value' );
 49+ $this->assertEquals(
 50+ array(
 51+ 'foo' => array(
 52+ 'default' => 'string value',
 53+ 'consumed' => false,
 54+ 'type' => FormOptions::STRING,
 55+ 'value' => null,
 56+ )
 57+ ),
 58+ $this->object->options
 59+ );
 60+ }
 61+
 62+ public function testAddIntegers() {
 63+ $this->object->add( 'one', 1 );
 64+ $this->object->add( 'negone', -1 );
 65+ $this->assertEquals(
 66+ array(
 67+ 'negone' => array(
 68+ 'default' => -1,
 69+ 'value' => null,
 70+ 'consumed' => false,
 71+ 'type' => FormOptions::INT,
 72+ ),
 73+ 'one' => array(
 74+ 'default' => 1,
 75+ 'value' => null,
 76+ 'consumed' => false,
 77+ 'type' => FormOptions::INT,
 78+ )
 79+ ),
 80+ $this->object->options
 81+ );
 82+ }
 83+
 84+}
Property changes on: trunk/phase3/tests/phpunit/includes/FormOptionsInitializationTest.php
___________________________________________________________________
Added: svn:eol-style
185 + native
Index: trunk/phase3/includes/FormOptions.php
@@ -1,19 +1,38 @@
22 <?php
33 /**
44 * Helper class to keep track of options when mixing links and form elements.
 5+ * @todo This badly need some examples and tests :-)
56 *
6 - * Copyright © 2008, Niklas Laxström
 7+ * Copyright © 2008, Niklas Laxstiröm
78 *
 9+ * Copyright © 2011, Ashar Voultoiz
 10+ *
811 * @author Niklas Laxström
 12+ * @author Ashar Voultoiz
913 */
1014
1115 class FormOptions implements ArrayAccess {
12 - const AUTO = -1; // ! Automatically detects simple data types
 16+ /** @name Type constants
 17+ * Used internally to map an option value to a WebRequest accessor
 18+ */
 19+ /* @{ */
 20+ /** Mark value for automatic detection (for simple data types only) */
 21+ const AUTO = -1;
 22+ /** String type, maps guessType() to WebRequest::getText() */
1323 const STRING = 0;
 24+ /** Integer type, maps guessType() to WebRequest::getInt() */
1425 const INT = 1;
 26+ /** Boolean type, maps guessType() to WebRequest::getBool() */
1527 const BOOL = 2;
16 - const INTNULL = 3; // ! Useful for namespace selector
 28+ /** Integer type or null, maps to WebRequest::getIntOrNull()
 29+ * This is useful for the namespace selector.
 30+ */
 31+ const INTNULL = 3;
 32+ /* @} */
1733
 34+ /**
 35+ * @todo Document!
 36+ */
1837 protected $options = array();
1938
2039 # Setting up
@@ -38,6 +57,16 @@
3958 unset( $this->options[$name] );
4059 }
4160
 61+ /**
 62+ * Used to find out which type the data is.
 63+ * All types are defined in the 'Type constants' section of this class
 64+ * Please note we do not support detection of INTNULL MediaWiki type
 65+ * which will be assumed as INT if the data is an integer.
 66+ *
 67+ * @param $data Mixed: value to guess type for
 68+ * @exception MWException Unsupported datatype
 69+ * @return Type constant
 70+ */
4271 public static function guessType( $data ) {
4372 if ( is_bool( $data ) ) {
4473 return self::BOOL;
@@ -52,6 +81,13 @@
5382
5483 # Handling values
5584
 85+ /**
 86+ * Verify the given option name exist.
 87+ *
 88+ * @param $name String: option name
 89+ * @param $strict Boolean: throw an exception when the option does not exist (default false)
 90+ * @return Boolean: true if option exist, false otherwise
 91+ */
5692 public function validateName( $name, $strict = false ) {
5793 if ( !isset( $this->options[$name] ) ) {
5894 if ( $strict ) {
@@ -63,6 +99,14 @@
64100 return true;
65101 }
66102
 103+ /**
 104+ * Use to set the value of an option.
 105+ *
 106+ * @param $name String: option name
 107+ * @param $value Mixed: value for the option
 108+ * @param $force Boolean: whether to set the value when it is equivalent to the default value for this option (default false).
 109+ * @return null
 110+ */
67111 public function setValue( $name, $value, $force = false ) {
68112 $this->validateName( $name, true );
69113
@@ -74,12 +118,24 @@
75119 }
76120 }
77121
 122+ /**
 123+ * Get the value for the given option name.
 124+ * Internally use getValueReal()
 125+ *
 126+ * @param $name String: option name
 127+ * @return Mixed
 128+ */
78129 public function getValue( $name ) {
79130 $this->validateName( $name, true );
80131
81132 return $this->getValueReal( $this->options[$name] );
82133 }
83134
 135+ /**
 136+ * @todo Document
 137+ * @param $option Array: array structure describing the option
 138+ * @return Mixed. Value or the default value if it is null
 139+ */
84140 protected function getValueReal( $option ) {
85141 if ( $option['value'] !== null ) {
86142 return $option['value'];
@@ -88,11 +144,22 @@
89145 }
90146 }
91147
 148+ /**
 149+ * Delete the option value.
 150+ * This will make future calls to getValue() return the default value.
 151+ * @param $name String: option name
 152+ * @return null
 153+ */
92154 public function reset( $name ) {
93155 $this->validateName( $name, true );
94156 $this->options[$name]['value'] = null;
95157 }
96158
 159+ /**
 160+ * @todo Document
 161+ * @param $name String: option name
 162+ * @return null
 163+ */
97164 public function consumeValue( $name ) {
98165 $this->validateName( $name, true );
99166 $this->options[$name]['consumed'] = true;
@@ -100,6 +167,11 @@
101168 return $this->getValueReal( $this->options[$name] );
102169 }
103170
 171+ /**
 172+ * @todo Document
 173+ * @param $names Array: array of option names
 174+ * @return null
 175+ */
104176 public function consumeValues( /*Array*/ $names ) {
105177 $out = array();
106178
@@ -112,8 +184,16 @@
113185 return $out;
114186 }
115187
116 - # Validating values
117 -
 188+ /**
 189+ * Validate and set an option integer value
 190+ * The value will be altered to fit in the range.
 191+ *
 192+ * @param $name String: option name
 193+ * @param $min Int: minimum value
 194+ * @param $max Int: maximum value
 195+ * @exception MWException Option is not of type int
 196+ * @return null
 197+ */
118198 public function validateIntBounds( $name, $min, $max ) {
119199 $this->validateName( $name, true );
120200
@@ -127,8 +207,11 @@
128208 $this->setValue( $name, $value );
129209 }
130210
131 - # Getting the data out for use
132 -
 211+ /**
 212+ * Getting the data out for use
 213+ * @param $all Boolean: whether to include unchanged options (default: false)
 214+ * @return Array
 215+ */
133216 public function getUnconsumedValues( $all = false ) {
134217 $values = array();
135218
@@ -143,6 +226,10 @@
144227 return $values;
145228 }
146229
 230+ /**
 231+ * Return options modified as an array ( name => value )
 232+ * @return Array
 233+ */
147234 public function getChangedValues() {
148235 $values = array();
149236
@@ -155,6 +242,10 @@
156243 return $values;
157244 }
158245
 246+ /**
 247+ * Format options to an array ( name => value)
 248+ * @return Array
 249+ */
159250 public function getAllValues() {
160251 $values = array();
161252
@@ -195,20 +286,26 @@
196287 }
197288 }
198289
199 - /* ArrayAccess methods */
 290+ /** @name ArrayAccess functions
 291+ * Those function implements PHP ArrayAccess interface
 292+ * @see http://php.net/manual/en/class.arrayaccess.php
 293+ */
 294+ /* @{ */
 295+ /** Whether option exist*/
200296 public function offsetExists( $name ) {
201297 return isset( $this->options[$name] );
202298 }
203 -
 299+ /** Retrieve an option value */
204300 public function offsetGet( $name ) {
205301 return $this->getValue( $name );
206302 }
207 -
 303+ /** Set an option to given value */
208304 public function offsetSet( $name, $value ) {
209305 $this->setValue( $name, $value );
210306 }
211 -
 307+ /** Delete the option */
212308 public function offsetUnset( $name ) {
213309 $this->delete( $name );
214310 }
 311+ /* @} */
215312 }

Comments

#Comment by Aaron Schulz (talk | contribs)   21:20, 15 June 2011

Some people seem to not like incomplete tests though...

#Comment by 😂 (talk | contribs)   21:24, 15 June 2011

I don't, they clutter my output and increase runtime for no reason :)

I'm all for more tests...just implement them if you're going to bother at all.

Status & tagging log