sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.7k stars 2.2k forks source link

--list-groups option listing authors too #378

Closed hithwen closed 13 years ago

hithwen commented 13 years ago

Running phpunit 3.5.9 phpunit --list-groups is showing authors as well as groups

sebastianbergmann commented 13 years ago

That is because @author is an alias for @group.

hithwen commented 13 years ago

Hi, I think that It can be very useful in some cases to have separate options to list authors and groups, the idea behind is to later include an option to list tests that do not belong to any group (but they might have authors).

I've created a patch for 3.6 that keeps current alias functionality and provides two extra options: --list-authors and --list-only-groups.

I hope this can be useful for other users too, so if you agree I could fork the project to include this changes. I'm working on list-ungrouped-tests feature right now and I can submit that patch too.

Regards, Julia

diff -Naur PHPUnit/Extensions/SeleniumTestCase.php PHPUnit/Extensions/SeleniumTestCase.php
--- PHPUnit/Extensions/SeleniumTestCase.php 2011-10-24 19:02:30.000000000 +0200
+++ PHPUnit/Extensions/SeleniumTestCase.php 2011-10-24 18:58:44.000000000 +0200
@@ -357,6 +357,7 @@

         $class            = new ReflectionClass($className);
         $classGroups      = PHPUnit_Util_Test::getGroups($className);
+        $classAuthors     = PHPUnit_Util_Test::getAuthors($className);
         $staticProperties = $class->getStaticProperties();

         // Create tests from Selenese/HTML files.
@@ -376,7 +377,7 @@
                     foreach ($files as $file) {
                         $browserSuite->addTest(
                           new $className($file, array(), '', $browser),
-                          $classGroups
+                          $classGroups, $classAuthors
                         );
                     }

@@ -387,7 +388,7 @@
             // Create tests from Selenese/HTML files for single browser.
             else {
                 foreach ($files as $file) {
-                    $suite->addTest(new $className($file), $classGroups);
+                    $suite->addTest(new $className($file), $classGroups, $classAuthors);
                 }
             }
         }
@@ -403,6 +404,7 @@
                         $name   = $method->getName();
                         $data   = PHPUnit_Util_Test::getProvidedData($className, $name);
                         $groups = PHPUnit_Util_Test::getGroups($className, $name);
+                        $authors = PHPUnit_Util_Test::getAuthors($className, $name);

                         // Test method with @dataProvider.
                         if (is_array($data) || $data instanceof Iterator) {
@@ -413,7 +415,7 @@
                             foreach ($data as $_dataName => $_data) {
                                 $dataSuite->addTest(
                                   new $className($name, $_data, $_dataName, $browser),
-                                  $groups
+                                  $groups, $authors
                                 );
                             }

@@ -436,7 +438,7 @@
                         // Test method without @dataProvider.
                         else {
                             $browserSuite->addTest(
-                              new $className($name, array(), '', $browser), $groups
+                              new $className($name, array(), '', $browser), $groups, $authors
                             );
                         }
                     }
@@ -453,6 +455,7 @@
                     $name   = $method->getName();
                     $data   = PHPUnit_Util_Test::getProvidedData($className, $name);
                     $groups = PHPUnit_Util_Test::getGroups($className, $name);
+                    $authors = PHPUnit_Util_Test::getAuthors($className, $name);

                     // Test method with @dataProvider.
                     if (is_array($data) || $data instanceof Iterator) {
@@ -463,7 +466,7 @@
                         foreach ($data as $_dataName => $_data) {
                             $dataSuite->addTest(
                               new $className($name, $_data, $_dataName),
-                              $groups
+                              $groups, $authors
                             );
                         }

@@ -486,7 +489,7 @@
                     // Test method without @dataProvider.
                     else {
                         $suite->addTest(
-                          new $className($name), $groups
+                          new $className($name), $groups, $authors
                         );
                     }
                 }
diff -Naur PHPUnit/Framework/TestSuite.php PHPUnit/Framework/TestSuite.php
--- PHPUnit/Framework/TestSuite.php 2011-10-24 19:02:30.000000000 +0200
+++ PHPUnit/Framework/TestSuite.php 2011-10-24 18:58:44.000000000 +0200
@@ -109,6 +109,13 @@
      * @var    array
      */
     protected $groups = array();
+     
+    /**
+    * The test authors of the test suite.
+    *
+    * @var    array
+    */
+    protected $authors = array();

     /**
      * The tests in the test suite.
@@ -245,8 +252,9 @@
      *
      * @param  PHPUnit_Framework_Test $test
      * @param  array                  $groups
+     * @param  array                  $authors
      */
-    public function addTest(PHPUnit_Framework_Test $test, $groups = array())
+    public function addTest(PHPUnit_Framework_Test $test, $groups = array(), $authors = array())
     {
         $class = new ReflectionClass($test);

@@ -254,15 +262,20 @@
             $this->tests[]  = $test;
             $this->numTests = -1;

-            if ($test instanceof PHPUnit_Framework_TestSuite &&
-                empty($groups)) {
-                $groups = $test->getGroups();
+            if ($test instanceof PHPUnit_Framework_TestSuite) {
+               if (empty($groups)) {
+                   $groups = $test->getGroups();
+               }
+               
+               if (empty($authors)) {
+                   $authors = $test->getAuthors();
+               } 
             }

             if (empty($groups)) {
                 $groups = array('__nogroup__');
             }
-
+            
             foreach ($groups as $group) {
                 if (!isset($this->groups[$group])) {
                     $this->groups[$group] = array($test);
@@ -270,6 +283,14 @@
                     $this->groups[$group][] = $test;
                 }
             }
+            
+            foreach ($authors as $author) {
+               if (!isset($this->authors[$author])) {
+                   $this->authors[$author] = array($test);
+               } else {
+                   $this->authors[$author][] = $test;
+               }
+            }
         }
     }

@@ -520,7 +541,8 @@
                     }

                     else {
-                        $groups = PHPUnit_Util_Test::getGroups($className, $name);
+                        $groups  = PHPUnit_Util_Test::getGroups($className, $name);
+                        $authors = PHPUnit_Util_Test::getAuthors($className, $name);

                         foreach ($data as $_dataName => $_data) {
                             $_test = new $className($name, $_data, $_dataName);
@@ -545,7 +567,7 @@
                                 );
                             }

-                            $test->addTest($_test, $groups);
+                            $test->addTest($_test, $groups, $authors);
                         }
                     }
                 }
@@ -615,6 +637,17 @@
     {
         return array_keys($this->groups);
     }
+    
+    /**
+    * Returns the test groups of the suite.
+    *
+    * @return array
+    * @since  Method available since Release 3.2.0
+    */
+    public function getAuthors()
+    {
+       return array_keys($this->authors);
+    }

     /**
      * Runs the tests and collects their result in a TestResult.
@@ -825,7 +858,7 @@
      */
     protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method)
     {
-        $name = $method->getName();
+        $name = $method->getName();

         if ($this->isPublicTestMethod($method)) {
             $test = self::createTest($class, $name);
@@ -837,9 +870,9 @@
                 );
             }

-            $this->addTest($test, PHPUnit_Util_Test::getGroups(
-              $class->getName(), $name)
-            );
+            $groups = PHPUnit_Util_Test::getGroups($class->getName(), $name);
+            $authors = PHPUnit_Util_Test::getAuthors($class->getName(), $name);
+            $this->addTest($test, $groups, $authors);
         }

         else if ($this->isTestMethod($method)) {
diff -Naur PHPUnit/TextUI/Command.php PHPUnit/TextUI/Command.php
--- PHPUnit/TextUI/Command.php  2011-10-24 19:02:30.000000000 +0200
+++ PHPUnit/TextUI/Command.php  2011-10-24 19:00:08.000000000 +0200
@@ -63,6 +63,8 @@
      */
     protected $arguments = array(
       'listGroups'              => FALSE,
+      'listOnlyGroups'          => FALSE,
+      'listAuthors'             => FALSE,
       'loader'                  => NULL,
       'useDefaultConfiguration' => TRUE
     );
@@ -90,6 +92,8 @@
       'help' => NULL,
       'include-path=' => NULL,
       'list-groups' => NULL,
+      'list-only-groups' => NULL,
+      'list-authors' => NULL,
       'loader=' => NULL,
       'log-json=' => NULL,
       'log-junit=' => NULL,
@@ -167,7 +171,7 @@

             print "Available test group(s):\n";

-            $groups = $suite->getGroups();
+            $groups = array_merge($suite->getGroups(), $suite->getAuthors());
             sort($groups);

             foreach ($groups as $group) {
@@ -180,6 +184,42 @@
                 return PHPUnit_TextUI_TestRunner::SUCCESS_EXIT;
             }
         }
+        
+        /**
+        * @author Julia S.Simon <julia@tuenti.com>
+        */
+        if ($this->arguments['listAuthors']) {
+           PHPUnit_TextUI_TestRunner::printVersionString();
+        
+           print "Test authors:\n";
+        
+           $tests = $suite->getAuthors();
+           sort($tests);
+        
+           foreach ($tests as $test) {
+               print "   $test\n";
+           }
+        
+           exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
+        }
+        
+        /**
+        * @author Julia S.Simon <julia@tuenti.com>
+        */
+        if ($this->arguments['listOnlyGroups']) {
+           PHPUnit_TextUI_TestRunner::printVersionString();
+        
+           print "Test groups:\n";
+        
+           $tests = $suite->getGroups();
+           sort($tests);
+        
+           foreach ($tests as $test) {
+               print "   $test\n";
+           }
+                
+           exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT);
+        }

         unset($this->arguments['test']);
         unset($this->arguments['testFile']);
@@ -385,7 +425,17 @@
                     $this->arguments['printer'] = $option[1];
                 }
                 break;
-
+                
+                case '--list-only-groups': {
+                   $this->arguments['listOnlyGroups'] = TRUE;
+                }
+                break;
+                
+                case '--list-authors': {
+                   $this->arguments['listAuthors'] = TRUE;
+                }
+                break;
+                
                 case '--loader': {
                     $this->arguments['loader'] = $option[1];
                 }
@@ -902,7 +952,9 @@
   --filter <pattern>        Filter which tests to run.
   --group ...               Only runs tests from the specified group(s).
   --exclude-group ...       Exclude tests from the specified group(s).
-  --list-groups             List available test groups.
+  --list-groups             List available test groups (author is an alias to group).
+  --list-only-groups        List only groups from available test groups.
+  --list-authors            List available test authors.

   --loader <loader>         TestSuiteLoader implementation to use.
   --printer <printer>       TestSuiteListener implementation to use.
diff -Naur PHPUnit/Util/Test.php PHPUnit/Util/Test.php
--- PHPUnit/Util/Test.php   2011-10-24 19:02:30.000000000 +0200
+++ PHPUnit/Util/Test.php   2011-10-24 18:58:44.000000000 +0200
@@ -362,15 +362,7 @@
         );

         $groups = array();
-
-        if (isset($annotations['method']['author'])) {
-            $groups = $annotations['method']['author'];
-        }
-
-        else if (isset($annotations['class']['author'])) {
-            $groups = $annotations['class']['author'];
-        }
-
+         
         if (isset($annotations['class']['group'])) {
             $groups = array_merge($groups, $annotations['class']['group']);
         }
@@ -399,6 +391,31 @@

         return array_unique($groups);
     }
+    
+    /**
+    * Returns the authors for a test class or method.
+    *
+    * @param  string $className
+    * @param  string $methodName
+    * @return array
+    * @author Julia S.Simon <julia@tuenti.com>
+    */
+    public static function getAuthors($className, $methodName = '')
+    {
+       $annotations = self::parseTestMethodAnnotations($className, $methodName);
+    
+       $authors = array();
+    
+       if (isset($annotations['method']['author'])) {
+           $authors = $annotations['method']['author'];
+       }
+    
+       else if (isset($annotations['class']['author'])) {
+           $authors = $annotations['class']['author'];
+       }
+        
+       return array_unique($authors);
+    }

     /**
      * Returns the size of the test.