/** * Prepares test environment. */ public function setUp() { $finder = new Finder(); $finder->files()->in(__DIR__ . '/config/'); $finder->name('*.yml'); $this->dummyParser = $this->getMock('Symfony\\Component\\Yaml\\Parser'); $this->dummyDumper = $this->getMock('Symfony\\Component\\Yaml\\Dumper'); $this->dummyFilesystem = $this->getMock('Symfony\\Component\\Filesystem\\Filesystem'); $dummyDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface'); $dummyDispatcher->expects($this->once())->method('dispatch')->willReturnCallback(function ($name, $event) { /** @var GroupOptionTypeEvent $event */ $optionTypes = ['acp' => ['twomartens.core' => ['access' => new BooleanOptionType()]]]; $event->addOptions($optionTypes); }); $this->yamlData = []; $this->optionData = []; foreach ($finder as $file) { /** @var SplFileInfo $file */ $basename = $file->getBasename('.yml'); $this->yamlData[$basename] = Yaml::parse($file->getContents()); $this->optionData[$basename] = ConfigUtil::convertToOptions($this->yamlData[$basename]); } $this->dummyParser->expects($this->any())->method('parse')->willReturnCallback(function ($content) { return Yaml::parse($content); }); /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dummyDispatcher */ $this->groupService = new GroupService($finder, $this->dummyParser, $this->dummyDumper, $this->dummyFilesystem, $dummyDispatcher); }
/** * Sets the test environment up. */ public function setUp() { $finder = new Finder(); $finder->files()->in(__DIR__ . '/config/'); $finder->name('config.yml'); $this->dummyParser = $this->getMock('Symfony\\Component\\Yaml\\Parser'); $this->dummyDumper = $this->getMock('Symfony\\Component\\Yaml\\Dumper'); $this->dummyFilesystem = $this->getMock('Symfony\\Component\\Filesystem\\Filesystem'); foreach ($finder as $file) { /** @var SplFileInfo $file */ $this->yamlData = Yaml::parse($file->getContents()); $this->optionData = ConfigUtil::convertToOptions($this->yamlData); $this->dummyParser->expects($this->once())->method('parse')->willReturn($this->yamlData); $this->dummyDumper->expects($this->any())->method('dump')->willReturn($file->getContents()); $this->dummyFilesystem->expects($this->any())->method('dumpFile')->with($file->getRealPath(), $file->getContents()); } $this->optionService = new OptionService($finder, $this->dummyParser, $this->dummyDumper, $this->dummyFilesystem); }
/** * Tests the convertToArray method. */ public function testConvertToArray() { // simple case $optionCategory = new OptionCategory(); $mainCategory = new OptionCategory('twomartens.core'); $options = [new Option(0, 'one', 'string', 'hello'), new Option(0, 'two', 'boolean', false), new Option(0, 'three', 'array', [1, 2, 3])]; $mainCategory->setOptions($options); $optionCategory->setCategories([$mainCategory]); $yamlData = ConfigUtil::convertToArray($optionCategory); $expectedYamlData = ['twomartens.core' => ['one' => 'hello', 'two' => false, 'three' => [1, 2, 3]]]; $this->assertEquals($expectedYamlData, $yamlData); // complex case $optionCategory = new OptionCategory(); $acpCategory = new OptionCategory('acp'); $ourCategory = new OptionCategory('twomartens.core'); $acpCategory->setCategories([$ourCategory]); $optionCategory->setCategories([$acpCategory]); $options = [new Option(0, 'one', 'boolean', true), new Option(0, 'two', 'string', 'foo'), new Option(0, 'three', 'array', [4, 5, 6])]; $ourCategory->setOptions($options); $yamlData = ConfigUtil::convertToArray($optionCategory); $expectedYamlData = ['acp' => ['twomartens.core' => ['one' => true, 'two' => 'foo', 'three' => [4, 5, 6]]]]; $this->assertEquals($expectedYamlData, $yamlData); }
/** * Writes the config files. * * ATTENTION: This method DOES NOT perform any sanitize actions. It * overwrites the config files with the options entirely. */ private function writeConfig() { // updates existing files and adds new files if necessary $path = null; $roleNames = array_keys($this->optionData); $baseNames = []; foreach ($this->finder as $file) { /** @var SplFileInfo $file */ if ($path === null) { $path = $file->getPath(); break; } } foreach ($roleNames as $roleName) { $baseNames[] = strtolower($roleName); } foreach ($baseNames as $basename) { $category = $this->optionData[strtoupper($basename)]; $yaml = ConfigUtil::convertToArray($category); $dumpReady = $this->dumper->dump($yaml, 3); $writePath = $path . '/' . $basename . '.yml'; $this->filesystem->dumpFile($writePath, $dumpReady); } foreach ($this->toBeRemoved as $toRemove) { $basename = strtolower($toRemove) . '.yml'; $deletePath = $path . '/' . $basename; $this->filesystem->remove($deletePath); } $this->toBeRemoved = []; }
/** * {@inheritdoc} */ public function get($optionCategory, $optionName) { $optionValue = $this->optionsYAML[$optionCategory][$optionName]; return ConfigUtil::convertToOption($optionName, $optionValue); }