Browse Source

ConfigurationResolver - look for .php_cs file in cwd as well

Dariusz Ruminski 9 years ago
parent
commit
200b828606

+ 8 - 1
src/Console/ConfigurationResolver.php

@@ -341,10 +341,17 @@ final class ConfigurationResolver
             $configDir = $path;
         }
 
-        return array(
+        $candidates = array(
             $configDir.DIRECTORY_SEPARATOR.'.php_cs',
             $configDir.DIRECTORY_SEPARATOR.'.php_cs.dist',
         );
+
+        if ($configDir !== $this->cwd) {
+            $candidates[] = $this->cwd.DIRECTORY_SEPARATOR.'.php_cs';
+            $candidates[] = $this->cwd.DIRECTORY_SEPARATOR.'.php_cs.dist';
+        }
+
+        return $candidates;
     }
 
     /**

+ 21 - 3
tests/Console/ConfigurationResolverTest.php

@@ -178,11 +178,17 @@ final class ConfigurationResolverTest extends \PHPUnit_Framework_TestCase
     /**
      * @dataProvider provideResolveConfigFileDefaultCases
      */
-    public function testResolveConfigFileChooseFile($expectedFile, $expectedClass, $path)
+    public function testResolveConfigFileChooseFile($expectedFile, $expectedClass, $path, $cwdPath = null)
     {
-        $this->resolver
+        $resolver = $this->resolver
             ->setOption('path', $path)
-            ->resolve();
+        ;
+
+        if (null !== $cwdPath) {
+            $resolver->setCwd($cwdPath);
+        }
+
+        $resolver->resolve();
 
         $this->assertSame($expectedFile, $this->resolver->getConfigFile());
         $this->assertInstanceOf($expectedClass, $this->resolver->getConfig());
@@ -208,6 +214,18 @@ final class ConfigurationResolverTest extends \PHPUnit_Framework_TestCase
                 'Test3Config',
                 $dirBase.'case_3',
             ),
+            array(
+                $dirBase.'case_6'.DIRECTORY_SEPARATOR.'.php_cs.dist',
+                'Test6Config',
+                $dirBase.'case_6'.DIRECTORY_SEPARATOR.'subdir',
+                $dirBase.'case_6',
+            ),
+            array(
+                $dirBase.'case_6'.DIRECTORY_SEPARATOR.'.php_cs.dist',
+                'Test6Config',
+                $dirBase.'case_6'.DIRECTORY_SEPARATOR.'subdir/empty_file.php',
+                $dirBase.'case_6',
+            ),
         );
     }
 

+ 7 - 0
tests/Fixtures/ConfigurationResolverConfigFile/case_6/.php_cs.dist

@@ -0,0 +1,7 @@
+<?php
+
+if (!class_exists('Test6Config')) {
+    class Test6Config extends PhpCsFixer\Config {}
+}
+
+return Test6Config::create();

+ 1 - 0
tests/Fixtures/ConfigurationResolverConfigFile/case_6/subdir/empty_file.php

@@ -0,0 +1 @@
+<?php