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 |
1 | 252 | + 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 |
1 | 85 | + native |
Index: trunk/phase3/includes/FormOptions.php |
— | — | @@ -1,19 +1,38 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | 4 | * Helper class to keep track of options when mixing links and form elements. |
| 5 | + * @todo This badly need some examples and tests :-) |
5 | 6 | * |
6 | | - * Copyright © 2008, Niklas Laxström |
| 7 | + * Copyright © 2008, Niklas Laxstiröm |
7 | 8 | * |
| 9 | + * Copyright © 2011, Ashar Voultoiz |
| 10 | + * |
8 | 11 | * @author Niklas Laxström |
| 12 | + * @author Ashar Voultoiz |
9 | 13 | */ |
10 | 14 | |
11 | 15 | 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() */ |
13 | 23 | const STRING = 0; |
| 24 | + /** Integer type, maps guessType() to WebRequest::getInt() */ |
14 | 25 | const INT = 1; |
| 26 | + /** Boolean type, maps guessType() to WebRequest::getBool() */ |
15 | 27 | 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 | + /* @} */ |
17 | 33 | |
| 34 | + /** |
| 35 | + * @todo Document! |
| 36 | + */ |
18 | 37 | protected $options = array(); |
19 | 38 | |
20 | 39 | # Setting up |
— | — | @@ -38,6 +57,16 @@ |
39 | 58 | unset( $this->options[$name] ); |
40 | 59 | } |
41 | 60 | |
| 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 | + */ |
42 | 71 | public static function guessType( $data ) { |
43 | 72 | if ( is_bool( $data ) ) { |
44 | 73 | return self::BOOL; |
— | — | @@ -52,6 +81,13 @@ |
53 | 82 | |
54 | 83 | # Handling values |
55 | 84 | |
| 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 | + */ |
56 | 92 | public function validateName( $name, $strict = false ) { |
57 | 93 | if ( !isset( $this->options[$name] ) ) { |
58 | 94 | if ( $strict ) { |
— | — | @@ -63,6 +99,14 @@ |
64 | 100 | return true; |
65 | 101 | } |
66 | 102 | |
| 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 | + */ |
67 | 111 | public function setValue( $name, $value, $force = false ) { |
68 | 112 | $this->validateName( $name, true ); |
69 | 113 | |
— | — | @@ -74,12 +118,24 @@ |
75 | 119 | } |
76 | 120 | } |
77 | 121 | |
| 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 | + */ |
78 | 129 | public function getValue( $name ) { |
79 | 130 | $this->validateName( $name, true ); |
80 | 131 | |
81 | 132 | return $this->getValueReal( $this->options[$name] ); |
82 | 133 | } |
83 | 134 | |
| 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 | + */ |
84 | 140 | protected function getValueReal( $option ) { |
85 | 141 | if ( $option['value'] !== null ) { |
86 | 142 | return $option['value']; |
— | — | @@ -88,11 +144,22 @@ |
89 | 145 | } |
90 | 146 | } |
91 | 147 | |
| 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 | + */ |
92 | 154 | public function reset( $name ) { |
93 | 155 | $this->validateName( $name, true ); |
94 | 156 | $this->options[$name]['value'] = null; |
95 | 157 | } |
96 | 158 | |
| 159 | + /** |
| 160 | + * @todo Document |
| 161 | + * @param $name String: option name |
| 162 | + * @return null |
| 163 | + */ |
97 | 164 | public function consumeValue( $name ) { |
98 | 165 | $this->validateName( $name, true ); |
99 | 166 | $this->options[$name]['consumed'] = true; |
— | — | @@ -100,6 +167,11 @@ |
101 | 168 | return $this->getValueReal( $this->options[$name] ); |
102 | 169 | } |
103 | 170 | |
| 171 | + /** |
| 172 | + * @todo Document |
| 173 | + * @param $names Array: array of option names |
| 174 | + * @return null |
| 175 | + */ |
104 | 176 | public function consumeValues( /*Array*/ $names ) { |
105 | 177 | $out = array(); |
106 | 178 | |
— | — | @@ -112,8 +184,16 @@ |
113 | 185 | return $out; |
114 | 186 | } |
115 | 187 | |
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 | + */ |
118 | 198 | public function validateIntBounds( $name, $min, $max ) { |
119 | 199 | $this->validateName( $name, true ); |
120 | 200 | |
— | — | @@ -127,8 +207,11 @@ |
128 | 208 | $this->setValue( $name, $value ); |
129 | 209 | } |
130 | 210 | |
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 | + */ |
133 | 216 | public function getUnconsumedValues( $all = false ) { |
134 | 217 | $values = array(); |
135 | 218 | |
— | — | @@ -143,6 +226,10 @@ |
144 | 227 | return $values; |
145 | 228 | } |
146 | 229 | |
| 230 | + /** |
| 231 | + * Return options modified as an array ( name => value ) |
| 232 | + * @return Array |
| 233 | + */ |
147 | 234 | public function getChangedValues() { |
148 | 235 | $values = array(); |
149 | 236 | |
— | — | @@ -155,6 +242,10 @@ |
156 | 243 | return $values; |
157 | 244 | } |
158 | 245 | |
| 246 | + /** |
| 247 | + * Format options to an array ( name => value) |
| 248 | + * @return Array |
| 249 | + */ |
159 | 250 | public function getAllValues() { |
160 | 251 | $values = array(); |
161 | 252 | |
— | — | @@ -195,20 +286,26 @@ |
196 | 287 | } |
197 | 288 | } |
198 | 289 | |
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*/ |
200 | 296 | public function offsetExists( $name ) { |
201 | 297 | return isset( $this->options[$name] ); |
202 | 298 | } |
203 | | - |
| 299 | + /** Retrieve an option value */ |
204 | 300 | public function offsetGet( $name ) { |
205 | 301 | return $this->getValue( $name ); |
206 | 302 | } |
207 | | - |
| 303 | + /** Set an option to given value */ |
208 | 304 | public function offsetSet( $name, $value ) { |
209 | 305 | $this->setValue( $name, $value ); |
210 | 306 | } |
211 | | - |
| 307 | + /** Delete the option */ |
212 | 308 | public function offsetUnset( $name ) { |
213 | 309 | $this->delete( $name ); |
214 | 310 | } |
| 311 | + /* @} */ |
215 | 312 | } |