Przeglądaj źródła

[ZF-5948] Neverending loop in ->parse(). Fixed by introducing Exception on wrong parameter.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19764 44c647ce-9c0f-0410-b52a-842ac1e357ba
mluiten 16 lat temu
rodzic
commit
967a6bda37
2 zmienionych plików z 40 dodań i 0 usunięć
  1. 12 0
      library/Zend/Console/Getopt.php
  2. 28 0
      tests/Zend/Console/GetoptTest.php

+ 12 - 0
library/Zend/Console/Getopt.php

@@ -344,10 +344,16 @@ class Zend_Console_Getopt
      * These are appended to those defined when the constructor was called.
      *
      * @param  array $argv
+     * @throws Zend_Console_Getopt_Exception When not given an array as parameter
      * @return Zend_Console_Getopt Provides a fluent interface
      */
     public function addArguments($argv)
     {
+        if(!is_array($argv)) {
+            require_once 'Zend/Console/Getopt/Exception.php';
+            throw new Zend_Console_Getopt_Exception(
+                "Parameter #1 to addArguments should be an array");
+        }
         $this->_argv = array_merge($this->_argv, $argv);
         $this->_parsed = false;
         return $this;
@@ -358,10 +364,16 @@ class Zend_Console_Getopt
      * These replace any currently defined.
      *
      * @param  array $argv
+     * @throws Zend_Console_Getopt_Exception When not given an array as parameter
      * @return Zend_Console_Getopt Provides a fluent interface
      */
     public function setArguments($argv)
     {
+        if(!is_array($argv)) {
+            require_once 'Zend/Console/Getopt/Exception.php';
+            throw new Zend_Console_Getopt_Exception(
+                "Parameter #1 to setArguments should be an array");
+        }
         $this->_argv = $argv;
         $this->_parsed = false;
         return $this;

+ 28 - 0
tests/Zend/Console/GetoptTest.php

@@ -278,6 +278,34 @@ class Zend_Console_GetoptTest extends PHPUnit_Framework_TestCase
         unset($opts->a);
         $this->assertFalse(isset($opts->a));
     }
+    
+    /**
+     * @group ZF-5948
+     */
+    public function testGetoptAddSetNonArrayArguments()
+    {
+        $opts = new Zend_Console_GetOpt('abp:', array('-foo'));
+        try {
+            $opts->setArguments('-a');
+            $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
+        } catch(Zend_Exception $e) {
+            $this->assertType('Zend_Console_Getopt_Exception', $e,
+                'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
+            $this->assertEquals("Parameter #1 to setArguments should be an array",
+                $e->getMessage());
+        }
+        
+        try {
+            $opts->addArguments('-b');
+            $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
+        } catch(Zend_Exception $e) {
+            $this->assertType('Zend_Console_Getopt_Exception', $e,
+                'Expected Zend_Console_Getopt_Exception, got '. get_class($e));
+            $this->assertEquals("Parameter #1 to addArguments should be an array",
+                $e->getMessage());
+        }
+        
+    }
 
     public function testGetoptAddArguments()
     {