/** * Detect the environment and define the ENVIRONMENT constant. * * @throws \RuntimeException */ public function detect() { if (empty($this->configMap['cliDefaultEnvironment'])) { throw new \RuntimeException('cliDefaultEnvironment must contain a value within Envariable config.'); } if (empty($this->configMap['environmentToDetectionMethodMap'])) { throw new \RuntimeException('You have not defined any hostnames within the "environmentToDetectionMethodMap" array within Envariable config.'); } if ($this->server->getInterfaceType() === 'cli') { $this->environment = $this->configMap['cliDefaultEnvironment']; return; } $result = array_filter($this->configMap['environmentToDetectionMethodMap'], array($this, 'isValidEnvironment')); if (empty($result) || count($result) > 1) { throw new \RuntimeException('Could not detect the environment.'); } $this->environment = key($result); }
/** * Test that exception is thrown as hostname and servername configuration are invalid. * * @param \Envariable\Util\Server $server */ function it_throws_exception_as_hostname_is_invalid(Server $server) { $server->getInterfaceType()->willReturn('apache2handler'); $this->setConfiguration(array('environmentToDetectionMethodMap' => array('production' => array('misspelled-hostname-servername-key' => 'production.machine-name.without-servername-matching')), 'cliDefaultEnvironment' => 'production')); $this->setServer($server); $this->setEnvironmentValidationStrategyMap(self::$environmentValidationStrategyMap); $this->shouldThrow(new \RuntimeException('Invalid hostname or servername configuration.'))->duringDetect(); $this->getEnvironment()->shouldReturn(null); }