Browse Source

Merge remote branch 'danielholmes/phpunit_xml_dir'

* danielholmes/phpunit_xml_dir:
  [FrameworkBundle] removed function for checking for phpunit.xml in cwd
  [FrameworkBundle] fixed error with arg reversing from previous changes
  [FrameworkBundle] refactored getPhpUnitXmlDir in to separate parts and added support for --confguration=... style phpunit flag
  [FrameworkBundle] fixed getPhpUnitXmlDir to work with directory paths with spaces in them
Fabien Potencier 14 years ago
parent
commit
9ff8080d9f
1 changed files with 35 additions and 9 deletions
  1. 35 9
      src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

+ 35 - 9
src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

@@ -55,20 +55,20 @@ abstract class WebTestCase extends BaseWebTestCase
      */
     protected function getPhpUnitXmlDir()
     {
-        $dir = getcwd();
+        $dir = null;
         if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
             throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
         }
 
-        // find the --configuration flag from PHPUnit
-        $cli = implode(' ', $_SERVER['argv']);
-        if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
-            $dir = realpath($matches[1]);
-        } elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
-            $dir = realpath($matches[1]);
-        } elseif (file_exists(getcwd().'/phpunit.xml') || file_exists(getcwd().'/phpunit.xml.dist')) {
+        $dir = $this->getPhpUnitCliConfigArgument();
+        if ($dir === null && 
+            (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml') ||
+            file_exists(getcwd() . DIRECTORY_SEPARATOR . 'phpunit.xml.dist'))) {
             $dir = getcwd();
-        } else {
+        }
+
+        // Can't continue
+        if ($dir === null) {
             throw new \RuntimeException('Unable to guess the Kernel directory.');
         }
 
@@ -79,6 +79,32 @@ abstract class WebTestCase extends BaseWebTestCase
         return $dir;
     }
 
+    /**
+     * Finds the value of configuration flag from cli
+     *
+     * PHPUnit will use the last configuration argument on the command line, so this only returns
+     * the last configuration argument
+     *
+     * @return string The value of the phpunit cli configuration option
+     */
+    private function getPhpUnitCliConfigArgument()
+    {
+        $dir = null;
+        $reversedArgs = array_reverse($_SERVER['argv']);
+        foreach ($reversedArgs as $argIndex=>$testArg) {
+            if ($testArg === '-c' || $testArg === '--configuration') {
+                $dir = realpath($reversedArgs[$argIndex - 1]);
+                break;
+            } else if (strpos($testArg, '--configuration=') === 0) {
+                $argPath = substr($testArg, strlen('--configuration='));
+                $dir = realpath($argPath);
+                break;
+            }
+        }
+
+        return $dir;
+    }
+
     /**
      * Attempts to guess the kernel location.
      *