/** * handle. * * @param Args $args * @param IO $io * * @return int */ public function handle(Args $args, IO $io) { $configFileExist = true; $overwrite = is_string($args->getOption('force')); try { $this->configurationLoader->setRootDirectory($args->getOption('config')); $configuration = $this->configurationLoader->loadConfiguration(); } catch (ConfigurationLoadingException $e) { $configFileExist = false; } if (!$configFileExist || $overwrite) { $configuration = ['urls' => ['google' => ['url' => 'https://www.google.fr', 'method' => 'GET', 'headers' => [], 'timeout' => 1, 'validator' => [], 'status_code' => 200, 'metric_uuid' => null, 'service_uuid' => null]], 'hogosha_portal' => ['username' => '', 'password' => '', 'base_uri' => 'http://localhost:8000/api/', 'metric_update' => false, 'incident_update' => false, 'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%', 'default_resolved_incident_message' => 'The service %service_name% is back to normal']]; // Dump configuration $content = $this->configurationDumper->dumpConfiguration($configuration); $this->filesystem->dumpFile($this->configurationLoader->getConfigurationFilepath(), $content); $io->writeLine('<info>Creating monitor file</info>'); } else { $io->writeLine(sprintf('<info>You already have a configuration file in</info> "%s"', $this->configurationLoader->getConfigurationFilepath())); } }
/** * testDefaultConfigurationFile. */ public function testDefaultConfigurationFile() { $testFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . Monitor::CONFIG_FILENAME; $configurationLoader = new ConfigurationLoader(); $configurationDumper = new ConfigurationDumper(); $filesystem = new Filesystem(); $argsMock = $this->prophesize(Args::class); $argsMock->getOption(Argument::type('string'))->willReturn(sys_get_temp_dir()); $ioMock = $this->prophesize(IO::class); $ioMock->writeLine(Argument::type('string'))->shouldBeCalled(); $commandMock = $this->prophesize(Command::class); $initHandler = new InitHandler($configurationLoader, $configurationDumper, $filesystem); $initHandler->handle($argsMock->reveal(), $ioMock->reveal(), $commandMock->reveal()); // Test the configuration of the enduser file $this->assertEquals($testFile, $configurationLoader->getConfigurationFilepath()); $this->assertEquals(['urls' => ['google' => ['url' => 'https://www.google.fr', 'method' => 'GET', 'headers' => [], 'timeout' => 1, 'validator' => [], 'status_code' => 200, 'metric_uuid' => null, 'service_uuid' => null]], 'hogosha_portal' => ['username' => '', 'password' => '', 'base_uri' => 'http://localhost:8000/api/', 'metric_update' => false, 'incident_update' => false, 'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%', 'default_resolved_incident_message' => 'The service %service_name% is back to normal']], Yaml::parse(file_get_contents($testFile))); unlink($testFile); }