2 namespace Consolidation\OutputFormatters;
4 use Consolidation\OutputFormatters\Options\FormatterOptions;
5 use Symfony\Component\Console\Input\StringInput;
6 use Symfony\Component\Console\Input\InputOption;
7 use Symfony\Component\Console\Input\InputArgument;
8 use Symfony\Component\Console\Input\InputDefinition;
10 class FormatterOptionsTests extends \PHPUnit_Framework_TestCase
12 public function createStringInput($testCommandline)
14 $input = new StringInput($testCommandline);
15 $optionDefinitions = [
16 new InputArgument('unused', InputArgument::REQUIRED),
17 new InputOption(FormatterOptions::FORMAT, null, InputOption::VALUE_REQUIRED),
18 new InputOption(FormatterOptions::TABLE_STYLE, null, InputOption::VALUE_REQUIRED),
19 new InputOption(FormatterOptions::FIELD, null, InputOption::VALUE_REQUIRED),
20 new InputOption(FormatterOptions::FIELDS, null, InputOption::VALUE_REQUIRED),
21 new InputOption(FormatterOptions::INCLUDE_FIELD_LABELS, null, InputOption::VALUE_NONE),
22 new InputOption(FormatterOptions::ROW_LABELS, null, InputOption::VALUE_REQUIRED),
23 new InputOption(FormatterOptions::FIELD_LABELS, null, InputOption::VALUE_REQUIRED),
24 // These probably don't make senes to alter via options
25 new InputOption(FormatterOptions::DEFAULT_FORMAT, null, InputOption::VALUE_REQUIRED),
26 new InputOption(FormatterOptions::DEFAULT_FIELDS, null, InputOption::VALUE_REQUIRED),
27 new InputOption(FormatterOptions::LIST_ORIENTATION, null, InputOption::VALUE_NONE),
29 $definition = new InputDefinition($optionDefinitions);
30 $input->bind($definition);
34 protected function getFormat(FormatterOptions $options, $defaults = [])
36 return $options->get(FormatterOptions::FORMAT, [], $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, ''));
39 public function testFormatterOptions() {
40 $configurationData = [
41 FormatterOptions::DEFAULT_FORMAT => 'table',
49 FormatterOptions::DEFAULT_FORMAT => 'var_export',
51 'default-only' => 'defaulty',
54 // Create a StringInput object and ensure that Symfony Console is working right.
55 $input = $this->createStringInput('test --format=yaml --include-field-labels');
56 $testValue = $input->getOption(FormatterOptions::INCLUDE_FIELD_LABELS);
57 $this->assertTrue($testValue);
58 $testValue = $input->getOption(FormatterOptions::FORMAT);
59 $this->assertEquals('yaml', $testValue);
61 // $options->get() only returns the default parameter is there is
62 // no matching key in configuration, userOptions or defaults.
63 $options = new FormatterOptions($configurationData, $userOptions);
64 $this->assertEquals('', $options->get('default-only'));
65 $this->assertEquals('defaulty', $options->get('default-only', $defaults));
66 $this->assertEquals('defaulty', $options->get('default-only', $defaults, 'irrelevant'));
67 $this->assertEquals('three', $options->get('try'));
68 $this->assertEquals('three', $options->get('try', $defaults));
69 $this->assertEquals('three', $options->get('try', $defaults, 'irrelevant'));
70 $this->assertFalse($options->get('no-such-key'));
71 $this->assertFalse($options->get('no-such-key', $defaults));
72 $this->assertEquals('last-chance', $options->get('no-such-key', $defaults, 'last-chance'));
74 // Change a user option
75 $options = new FormatterOptions($configurationData, $userOptions);
76 $options->setOption('try', 'changed');
77 $this->assertEquals('changed', $options->get('try'));
78 $this->assertEquals('changed', $options->get('try', $defaults));
79 $this->assertEquals('changed', $options->get('try', $defaults, 'irrelevant'));
81 // Configuration has higher priority than defaults
82 $options = new FormatterOptions($configurationData, $userOptions);
83 $this->assertEquals('table', $this->getFormat($options));
84 $this->assertEquals('table', $this->getFormat($options, $defaults));
86 // Override has higher priority than configuration and defaults
87 $options = new FormatterOptions($configurationData, $userOptions);
88 $newOptions = $options->override([FormatterOptions::DEFAULT_FORMAT => 'json']);
89 $this->assertEquals('json', $this->getFormat($newOptions));
90 $this->assertEquals('json', $this->getFormat($newOptions, $defaults));
92 $options = new FormatterOptions($configurationData, $userOptions);
93 $options->setConfigurationDefault(FormatterOptions::DEFAULT_FORMAT, 'php');
94 $this->assertEquals('table', $this->getFormat($options));
96 $options = new FormatterOptions($configurationData, $userOptions);
97 $options->setConfigurationData([]);
98 $this->assertEquals('', $this->getFormat($options));
100 // It is only possible to override options that appear in '$default'
101 // with $input; if there are no defaults, then the --format=yaml
102 // option will not be picked up.
103 $options = new FormatterOptions($configurationData, $userOptions);
104 $options->setInput($input);
105 $this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT));
106 $this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant'));
108 // We won't see the default value unless the configuration value is empty.
109 $options = new FormatterOptions([], $userOptions);
110 $this->assertEquals('var_export', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant'));