This repository was archived by the owner on Jan 31, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +82
-14
lines changed Expand file tree Collapse file tree 3 files changed +82
-14
lines changed Original file line number Diff line number Diff line change 34
34
abstract class Options implements ParameterObject
35
35
{
36
36
/**
37
- * @param array|Traversable|null $config
37
+ * @param array|Traversable|null $config
38
38
* @return Options
39
39
* @throws Exception\InvalidArgumentException
40
40
*/
41
41
public function __construct ($ config = null )
42
42
{
43
- if (!is_null ($ config )) {
44
- if (is_array ($ config ) || $ config instanceof Traversable) {
45
- $ this ->processArray ($ config );
46
- } else {
47
- throw new Exception \InvalidArgumentException (
48
- 'Parameter to \Zend\Stdlib\Options \'s '
49
- . 'constructor must be an array or implement the '
50
- . 'Traversable interface '
51
- );
52
- }
43
+ if (is_null ($ config )) {
44
+ return ;
53
45
}
46
+ $ this ->processArray ($ config );
54
47
}
55
48
56
49
/**
57
- * @param array $config
50
+ * @param array|Traversable $config
58
51
* @return void
59
52
*/
60
- protected function processArray (array $ config )
53
+ protected function processArray ($ config )
61
54
{
55
+ if (!is_array ($ config ) && !$ config instanceof Traversable) {
56
+ throw new Exception \InvalidArgumentException (sprintf (
57
+ 'Parameter provided to %s must be an array or Traversable ' ,
58
+ __METHOD__
59
+ ));
60
+ }
61
+
62
62
foreach ($ config as $ key => $ value ) {
63
63
$ setter = $ this ->assembleSetterNameFromConfigKey ($ key );
64
64
$ this ->{$ setter }($ value );
@@ -159,4 +159,4 @@ public function __unset($key)
159
159
);
160
160
}
161
161
}
162
- }
162
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ZendTest \Stdlib ;
4
+
5
+ use ArrayObject ,
6
+ ZendTest \Stdlib \TestAsset \TestOptions ,
7
+ ZendTest \Stdlib \TestAsset \TestTraversable ,
8
+ Zend \Stdlib \Exception \InvalidArgumentException ;
9
+
10
+ class OptionsTest extends \PHPUnit_Framework_TestCase
11
+ {
12
+ public function testConstructionWithArray ()
13
+ {
14
+ $ options = new TestOptions (array ('test_field ' => 1 ));
15
+
16
+ $ this ->assertEquals (1 , $ options ->test_field );
17
+ }
18
+
19
+ public function testConstructionWithTraversable ()
20
+ {
21
+ $ config = new ArrayObject (array ('test_field ' => 1 ));
22
+ $ options = new TestOptions ($ config );
23
+
24
+ $ this ->assertEquals (1 , $ options ->test_field );
25
+ }
26
+
27
+ public function testConstructionWithNull ()
28
+ {
29
+ try {
30
+ $ options = new TestOptions (null );
31
+ } catch (InvalidArgumentException $ e ) {
32
+ $ this ->fail ("Unexpected InvalidArgumentException raised " );
33
+ }
34
+ }
35
+
36
+ public function testUnsetting ()
37
+ {
38
+ $ options = new TestOptions (array ('test_field ' => 1 ));
39
+
40
+ $ this ->assertEquals (true , isset ($ options ->test_field ));
41
+ unset($ options ->testField );
42
+ $ this ->assertEquals (false , isset ($ options ->test_field ));
43
+
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ZendTest \Stdlib \TestAsset ;
4
+
5
+ use Zend \Stdlib \Options ;
6
+
7
+ /**
8
+ * Dummy TestOptions used to test Stdlib\Options
9
+ */
10
+ class TestOptions extends Options
11
+ {
12
+ protected $ testField ;
13
+
14
+ public function setTestField ($ value )
15
+ {
16
+ $ this ->testField = $ value ;
17
+ }
18
+
19
+ public function getTestField ()
20
+ {
21
+ return $ this ->testField ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments