Procházet zdrojové kódy

CS fixes in Zend_Console_Getopt

Frank Brückner před 11 roky
rodič
revize
2b8d4cb63c
2 změnil soubory, kde provedl 70 přidání a 36 odebrání
  1. 16 12
      library/Zend/Console/Getopt.php
  2. 54 24
      tests/Zend/Console/GetoptTest.php

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

@@ -730,23 +730,27 @@ class Zend_Console_Getopt
         $this->_parsed = true;
         return $this;
     }
-    
+
+    /**
+     * @throws Zend_Console_Getopt_Exception
+     */
     public function checkRequiredArguments()
-    {    
-        foreach ($this->_rules as $name=>$rule){           
-            if ($rule['param'] === 'required'){                
-                $defined = false;                
-                foreach ($rule['alias'] as $alias){                    
-                    $defined = $defined === true ? true : array_key_exists($alias, $this->_options);                    
+    {
+        foreach ($this->_rules as $name => $rule) {
+            if ($rule['param'] === 'required') {
+                $defined = false;
+                foreach ($rule['alias'] as $alias) {
+                    $defined = $defined === true ? true : array_key_exists($alias, $this->_options);
                 }
-                if ($defined === false){                    
+                if ($defined === false) {
                     require_once 'Zend/Console/Getopt/Exception.php';
-                    throw new Zend_Console_Getopt_Exception("Option \"$alias\" requires a parameter.", $this->getUsageMessage());
-                    
+                    throw new Zend_Console_Getopt_Exception(
+                        'Option "$alias" requires a parameter.',
+                        $this->getUsageMessage()
+                    );
                 }
-            }            
+            }
         }
-        
     }
 
     /**

+ 54 - 24
tests/Zend/Console/GetoptTest.php

@@ -268,42 +268,72 @@ class Zend_Console_GetoptTest extends PHPUnit_Framework_TestCase
         unset($opts->a);
         $this->assertFalse(isset($opts->a));
     }
-    
+
+    /**
+     * @group GH-377
+     */
     public function testVerifyRequiredArgument()
     {
-        $opts = new Zend_Console_Getopt(array('apple|a=s' => "First required argument"));
-        try {   
+        $opts = new Zend_Console_Getopt(
+            array('apple|a=s' => "First required argument")
+        );
+
+        try {
             $opts->parse();
             $opts->checkRequiredArguments();
             $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
+        } catch (Zend_Exception $e) {
+            $this->assertTrue(
+                $e instanceof Zend_Console_Getopt_Exception,
+                'Expected Zend_Console_Getopt_Exception, got ' . get_class($e)
+            );
+            $this->assertEquals(
+                'Option "a" requires a parameter.', $e->getMessage()
+            );
         }
-        catch (Zend_Exception $e){
-            $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
-                'Expected Zend_Console_Getopt_Exception, got '. get_class($e));            
-            $this->assertEquals('Option "a" requires a parameter.' , $e->getMessage());
-        }        
-        
-        $opts->addArguments(array( "-a", "apple") );
+
+        $opts->addArguments(
+            array(
+                "-a",
+                "apple"
+            )
+        );
         $opts->parse();
         $opts->checkRequiredArguments();//-> no Exception here
     }
-    
+
+    /**
+     * @group GH-377
+     */
     public function testEmptyRequiredOption()
-    {       
-        $opts = new Zend_Console_Getopt(array(
-            'apple|a=s' =>"First required argument",
-            'banana|b=i'  =>"Second required argument"
-        ));        
-        $opts->addArguments(array("-a","-b","123"));
-        try {   
+    {
+        $opts = new Zend_Console_Getopt(
+            array(
+                'apple|a=s' => "First required argument",
+                'banana|b=i' => "Second required argument"
+            )
+        );
+        $opts->addArguments(
+            array(
+                "-a",
+                "-b",
+                "123"
+            )
+        );
+
+        try {
             $opts->parse();
             $opts->checkRequiredArguments();
-            $this->fail('Expected to catch a Zend_Console_Getopt_Exception');             
-         } catch (Zend_Exception $e) {                          
-            $this->assertTrue($e instanceof Zend_Console_Getopt_Exception,
-                'Expected Zend_Console_Getopt_Exception, got '. get_class($e));            
-            $this->assertEquals('Option "a" requires a parameter.' , $e->getMessage());
-         }
+            $this->fail('Expected to catch a Zend_Console_Getopt_Exception');
+        } catch (Zend_Exception $e) {
+            $this->assertTrue(
+                $e instanceof Zend_Console_Getopt_Exception,
+                'Expected Zend_Console_Getopt_Exception, got ' . get_class($e)
+            );
+            $this->assertEquals(
+                'Option "a" requires a parameter.', $e->getMessage()
+            );
+        }
     }
 
     /**