Yaffs site version 1.1
[yaffs-website] / vendor / consolidation / output-formatters / tests / testFormatterOptions.php
1 <?php
2 namespace Consolidation\OutputFormatters;
3
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;
9
10 class FormatterOptionsTests extends \PHPUnit_Framework_TestCase
11 {
12     public function createStringInput($testCommandline)
13     {
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),
28         ];
29         $definition = new InputDefinition($optionDefinitions);
30         $input->bind($definition);
31         return $input;
32     }
33
34     protected function getFormat(FormatterOptions $options, $defaults = [])
35     {
36         return $options->get(FormatterOptions::FORMAT, [], $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, ''));
37     }
38
39     public function testFormatterOptions() {
40         $configurationData = [
41             FormatterOptions::DEFAULT_FORMAT => 'table',
42             'test' => 'one',
43             'try' => 'two',
44         ];
45         $userOptions = [
46             'try' => 'three',
47         ];
48         $defaults = [
49             FormatterOptions::DEFAULT_FORMAT => 'var_export',
50             'try' => 'four',
51             'default-only' => 'defaulty',
52         ];
53
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);
60
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'));
73
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'));
80
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));
85
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));
91
92         $options = new FormatterOptions($configurationData, $userOptions);
93         $options->setConfigurationDefault(FormatterOptions::DEFAULT_FORMAT, 'php');
94         $this->assertEquals('table', $this->getFormat($options));
95
96         $options = new FormatterOptions($configurationData, $userOptions);
97         $options->setConfigurationData([]);
98         $this->assertEquals('', $this->getFormat($options));
99
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'));
107
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'));
111     }
112 }