Drupal investigation

testFormatterOptions.php 5.8KB

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