/** * Resolves the environment configuration and maps the given servers to the given tasks, * according to the defined environment. * * @param array $environmentOptions Environment options * @param Environment $environment Environment * @param Project $project Project * * @return Mapping */ public function resolve(array $environmentOptions, Environment $environment, Project $project) { if (!array_key_exists('servers', $environmentOptions) || empty($environmentOptions['servers'])) { throw new \DomainException(sprintf("No servers for environment '%s' given", $environment->getName())); } if (!array_key_exists('stages', $environmentOptions) || empty($environmentOptions['stages'])) { throw new \DomainException(sprintf("No stages for environment '%s' given", $environment->getName())); } $this->logger->debug(sprintf("Resolving mapping for environment '%s'.", $environment->getName())); $mapping = new Mapping(); foreach ($environmentOptions['stages'] as $stageName => $stageOptions) { foreach ($stageOptions as $taskName => $taskOptions) { $stage = $environment->getStage($stageName); $task = $stage->getTask($taskName); if ($task instanceof LocalTask) { $this->logger->debug(sprintf("Local task '%s' in stage '%s' given. No servers will be added.", $taskName, $stageName)); $mapping->add($stage, $task); continue; } if (array_key_exists('servers', $taskOptions)) { foreach ($taskOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } else { foreach ($environmentOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } } } return $mapping; }
public function testConverting() { $stage1 = new Stage('before'); $stage2 = new Stage('release'); $task1 = $this->getMockForAbstractClass('AndreasWeber\\Kanoa\\Task\\Task\\Base\\RemoteTask', array('checkoutRepository')); $task2 = $this->getMockForAbstractClass('AndreasWeber\\Kanoa\\Task\\Task\\Base\\RemoteTask', array('doSomethingCool')); $task3 = $this->getMockForAbstractClass('AndreasWeber\\Kanoa\\Task\\Task\\Base\\LocalTask', array('wellSomeLocalStuff')); $server1 = new Server(array('name' => 'app1', 'host' => 'localhost', 'port' => 22, 'username' => 'root', 'password' => '12341234', 'tags' => array('app'))); $server2 = new Server(array('name' => 'app2', 'host' => 'localhost', 'port' => 22, 'username' => 'root', 'password' => '43214321', 'tags' => array('app'))); $mapping = new Mapping(); $mapping->add($stage1, $task3); $mapping->add($stage2, $task1, $server1); $mapping->add($stage2, $task1, $server2); $mapping->add($stage2, $task2, $server1); $runner = $this->converter->convert($mapping); $this->assertCount(4, $runner->getTaskCollection()->getTasks()); }