diff --git a/src/PHPCensor/Plugin/Util/Executor.php b/src/PHPCensor/Plugin/Util/Executor.php index a6448783..18bd2ebb 100644 --- a/src/PHPCensor/Plugin/Util/Executor.php +++ b/src/PHPCensor/Plugin/Util/Executor.php @@ -49,7 +49,7 @@ class Executor * * @return bool */ - public function executePlugins(&$config, $stage) + public function executePlugins($config, $stage) { $success = true; $pluginsToExecute = []; @@ -70,27 +70,55 @@ class Executor return $success; } + /** + * @param array $config + * @param string $branch + * + * @return bool|array + */ + public function getBranchSpecificConfig($config, $branch) + { + $configSections = array_keys($config); + + foreach ($configSections as $configSection) { + if (0 === strpos($configSection, 'branch-')) { + if ($configSection === ('branch-' . $branch)) { + return $config[$configSection]; + } + + if (0 === strpos($configSection, 'branch-regex:')) { + $pattern = '#' . substr($configSection, 13) . '#u'; + preg_match($pattern, $branch, $matches); + if (!empty($matches[0])) { + return $config[$configSection]; + } + } + } + } + + return []; + } + /** * Check the config for any plugins specific to the branch we're currently building. - * @param $config - * @param $stage - * @param $pluginsToExecute + * + * @param array $config + * @param string $stage + * @param array $pluginsToExecute + * * @return array */ - protected function getBranchSpecificPlugins(&$config, $stage, $pluginsToExecute) + protected function getBranchSpecificPlugins($config, $stage, $pluginsToExecute) { /** @var \PHPCensor\Model\Build $build */ - $build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build'); - $branch = $build->getBranch(); - - // If we don't have any branch-specific plugins: - if (!isset($config['branch-' . $branch][$stage]) || !is_array($config['branch-' . $branch][$stage])) { + $build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build'); + $branch = $build->getBranch(); + $branchConfig = $this->getBranchSpecificConfig($config, $branch); + if (!$branchConfig) { return $pluginsToExecute; } - // If we have branch-specific plugins to execute, add them to the list to be executed: - $branchConfig = $config['branch-' . $branch]; - $plugins = $branchConfig[$stage]; + $plugins = !empty($branchConfig[$stage]) ? $branchConfig[$stage] : []; $runOption = 'after'; if (!empty($branchConfig['run-option'])) { diff --git a/tests/PHPCensor/Plugin/Util/ExecutorTest.php b/tests/PHPCensor/Plugin/Util/ExecutorTest.php index c22c0f91..21abb900 100644 --- a/tests/PHPCensor/Plugin/Util/ExecutorTest.php +++ b/tests/PHPCensor/Plugin/Util/ExecutorTest.php @@ -31,6 +31,13 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase ); } + protected function getFakePluginClassName($pluginName) + { + $pluginNamespace = '\\Tests\\PHPCensor\\Plugin\\Util\\Fake\\'; + + return $pluginNamespace . $pluginName; + } + public function testExecutePlugin_AssumesNamespaceIfNoneGiven() { $options = []; @@ -147,11 +154,76 @@ class ExecutorTest extends \PHPUnit_Framework_TestCase $this->testedExecutor->executePlugins($config, 'stageOne'); } - protected function getFakePluginClassName($pluginName) + public function testGetBranchSpecificConfig() { - $pluginNamespace = '\\Tests\\PHPCensor\\Plugin\\Util\\Fake\\'; + $config = [ + 'setup' => [ + 'composer' => 'install', + ] + ]; - return $pluginNamespace . $pluginName; + $this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); + + $config = [ + 'setup' => [ + 'composer' => 'install', + ], + 'branch-branch-1' => [ + 'phpunit' => [], + ], + ]; + + $this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); + + $config = [ + 'setup' => [ + 'composer' => 'install', + ], + 'branch-branch-2' => [ + 'phpunit' => [], + ], + ]; + + $this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); + + $config = [ + 'setup' => [ + 'composer' => [ + 'install', + ], + ], + 'branch-regex:.+' => [ + 'phpunit' => [], + ], + ]; + + $this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); + + $config = [ + 'setup' => [ + 'composer' => [ + 'install', + ], + ], + 'branch-regex:^branch\-\d$' => [ + 'phpunit' => [], + ], + ]; + + $this->assertEquals(['phpunit' => []], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); + + $config = [ + 'setup' => [ + 'composer' => [ + 'install', + ], + ], + 'branch-regex:^branch\-\w{2,}$' => [ + 'phpunit' => [], + ], + ]; + + $this->assertEquals([], $this->testedExecutor->getBranchSpecificConfig($config, 'branch-1')); } }