/**
  * Test that it throws an exception as it fails to copy the config template file to its destination.
  *
  * @param \Envariable\Util\Filesystem $filesystem
  */
 function it_throws_exception_could_not_copy_config_file_to_destination(Filesystem $filesystem)
 {
     $applicationConfigDirectoryPath = 'path/to/application/config';
     $envariableConfigDirectoryPath = $applicationConfigDirectoryPath . '/Envariable';
     $configTemplatePath = 'Config/ConfigTemplate.php';
     $envariableConfigFilePath = $envariableConfigDirectoryPath . '/config.php';
     $filesystem->createDirectory($envariableConfigDirectoryPath)->willReturn(true);
     $filesystem->copyFile($configTemplatePath, $envariableConfigFilePath)->willReturn(false);
     $this->setFilesystem($filesystem);
     $this->shouldThrow(new \RuntimeException('Could not copy config file to destination.'))->duringCreateConfigFile($applicationConfigDirectoryPath);
 }
 /**
  * Copy the Envariable config template to the application config.
  *
  * @param string $applicationConfigDirectoryPath
  */
 public function createConfigFile($applicationConfigDirectoryPath)
 {
     $envariableConfigDirectoryPath = sprintf('%s%sEnvariable', $applicationConfigDirectoryPath, DIRECTORY_SEPARATOR);
     if (!$this->filesystem->createDirectory($envariableConfigDirectoryPath)) {
         throw new \RuntimeException('Could not create Envariable config directory.');
     }
     $configTemplatePath = sprintf('Config%sConfigTemplate.php', DIRECTORY_SEPARATOR);
     $envariableConfigFilePath = sprintf('%s%sconfig.php', $envariableConfigDirectoryPath, DIRECTORY_SEPARATOR);
     if (!$this->filesystem->copyFile($configTemplatePath, $envariableConfigFilePath)) {
         throw new \RuntimeException('Could not copy config file to destination.');
     }
 }
 /**
  * Execute the process of iterating over the custom config and
  * storing the data within environment variables.
  *
  * @throws \RuntimeException
  */
 public function execute()
 {
     $applicationRootPath = $this->filesystem->getApplicationRootPath();
     $customEnvironmentConfigFilePath = sprintf('%s/.env.%s.php', $applicationRootPath, $this->environment);
     if ($this->config['customEnvironmentConfigPath'] !== null) {
         $customEnvironmentConfigPath = rtrim($this->config['customEnvironmentConfigPath'], '/') . '/';
         $customEnvironmentConfigFilePath = sprintf('%s/%s.env.%s.php', $applicationRootPath, $customEnvironmentConfigPath, $this->environment);
     }
     if (!file_exists($customEnvironmentConfigFilePath)) {
         throw new \RuntimeException("Could not find configuration file: [{$customEnvironmentConfigFilePath}]");
     }
     $this->process(require $customEnvironmentConfigFilePath);
 }
 /**
  * Iterate over framework detection commands to identify an appropriate
  * Envariable config loader and then load the config.
  *
  * @return array
  *
  * @throws \RuntimeException
  */
 public function loadConfigFile()
 {
     try {
         array_walk($this->frameworkConfigPathLocatorCommandList, array($this, 'frameworkConfigPathLocatorCallback'));
     } catch (\RuntimeException $runtimeException) {
         // Do nothing. Exiting array_walk as soon as a config has been loaded.
     }
     if (!$this->filesystem->fileExists($this->configPath)) {
         throw new \RuntimeException('Could not load Envariable config.');
     }
     $envariableConfigFilePath = sprintf('%s%sEnvariable%sconfig.php', $this->configPath, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
     if (!$this->filesystem->fileExists($envariableConfigFilePath)) {
         $this->configCreator->createConfigFile($envariableConfigFilePath);
     }
     return $this->filesystem->loadConfigFile($envariableConfigFilePath);
 }
 /**
  * Test that createConfigFile is called as envarible config file path does not exist.
  *
  * @param \Envariable\Util\Filesystem $filesystem
  * @param \Envariable\ConfigCreator   $configCreator
  */
 function it_will_call_createConfigFile_as_envariable_config_file_does_not_exist(Filesystem $filesystem, ConfigCreator $configCreator)
 {
     for ($i = 0; $i < count(self::$commandList); $i++) {
         $this->addCommand(self::$commandList[$i]);
     }
     $configDirectoryPath = 'path/to/config/directory';
     $envariableConfigFilePath = $configDirectoryPath . '/Envariable/config.php';
     $expectedResult = array('some-config-key' => 'some-config-value');
     $filesystem->fileExists($configDirectoryPath)->willReturn(true);
     $filesystem->fileExists($envariableConfigFilePath)->willReturn(false);
     $configCreator->createConfigFile($envariableConfigFilePath)->shouldBeCalled();
     $filesystem->loadConfigFile($envariableConfigFilePath)->willReturn($expectedResult);
     $this->setFilesystem($filesystem);
     $this->setConfigCreator($configCreator);
     $this->loadConfigFile()->shouldreturn($expectedResult);
 }
    /**
     * Test that an exception is thrown in the event that the application path is not found.
     *
     * @param \Envariable\Util\Filesystem $filesystem
     */
    function it_throws_an_exception_as_application_path_not_found(Filesystem $filesystem)
    {
        $frontControllerPath = 'path/to/index.php';
        $frontControllerContent = <<<FC
<?php

\$system_path = 'system';
\$renamed_application_folder = 'alternate';

require_once BASEPATH.'core/CodeIgniter.php';
FC;
        $filesystem->getApplicationRootPath()->willReturn('path/to');
        $filesystem->fileGetContents($frontControllerPath)->willReturn($frontControllerContent);
        $this->setFilesystem($filesystem);
        $this->shouldThrow(new \RuntimeException('Application path could not be found.'))->duringLocate($frontControllerPath);
    }
 /**
  * Test that an exception is throw when name already exists within the environment store.
  *
  * @param \Envariable\Util\Filesystem $filesystem
  */
 function it_will_throw_exception_when_name_already_exists_within_getenv_environment_store(Filesystem $filesystem)
 {
     putenv('SOMEGETENVDUPEDB_HOST=Too late! I got here first.');
     $environment = 'getenvdupe';
     $filesystem->getApplicationRootPath()->willReturn(__DIR__ . '/Config');
     $this->setConfiguration(array('customEnvironmentConfigPath' => null));
     $this->setEnvironment($environment);
     $this->setFilesystem($filesystem);
     $this->shouldThrow(new \RuntimeException('An environment variable with the name "SOMEGETENVDUPEDB_HOST" already exists. Aborting.'))->duringExecute();
 }