Drupal investigation

testValidFormats.php 4.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Consolidation\OutputFormatters;
  3. use Consolidation\OutputFormatters\Options\FormatterOptions;
  4. use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
  5. use Consolidation\OutputFormatters\StructuredData\PropertyList;
  6. class ValidFormatsTests extends \PHPUnit_Framework_TestCase
  7. {
  8. protected $formatterManager;
  9. function setup() {
  10. $this->formatterManager = new FormatterManager();
  11. $this->formatterManager->addDefaultFormatters();
  12. $this->formatterManager->addDefaultSimplifiers();
  13. }
  14. function testValidFormats()
  15. {
  16. $arrayObjectRef = new \ReflectionClass('\ArrayObject');
  17. $associativeListRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\PropertyList');
  18. $rowsOfFieldsRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields');
  19. $notADataType = new \ReflectionClass('\Consolidation\OutputFormatters\FormatterManager');
  20. $jsonFormatter = $this->formatterManager->getFormatter('json');
  21. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, $notADataType);
  22. $this->assertFalse($isValid);
  23. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, new \ArrayObject());
  24. $this->assertTrue($isValid);
  25. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, $arrayObjectRef);
  26. $this->assertTrue($isValid);
  27. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, []);
  28. $this->assertTrue($isValid);
  29. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, $associativeListRef);
  30. $this->assertTrue($isValid);
  31. $isValid = $this->formatterManager->isValidFormat($jsonFormatter, $rowsOfFieldsRef);
  32. $this->assertTrue($isValid);
  33. $sectionsFormatter = $this->formatterManager->getFormatter('sections');
  34. $isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $notADataType);
  35. $this->assertFalse($isValid);
  36. $isValid = $this->formatterManager->isValidFormat($sectionsFormatter, []);
  37. $this->assertFalse($isValid);
  38. $isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $arrayObjectRef);
  39. $this->assertFalse($isValid);
  40. $isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $rowsOfFieldsRef);
  41. $this->assertTrue($isValid);
  42. $isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $associativeListRef);
  43. $this->assertFalse($isValid);
  44. // Check to see which formats can handle a simple array
  45. $validFormats = $this->formatterManager->validFormats([]);
  46. $this->assertEquals('csv,json,list,php,print-r,string,tsv,var_export,xml,yaml', implode(',', $validFormats));
  47. // Check to see which formats can handle an PropertyList
  48. $validFormats = $this->formatterManager->validFormats($associativeListRef);
  49. $this->assertEquals('csv,json,list,php,print-r,string,table,tsv,var_export,xml,yaml', implode(',', $validFormats));
  50. // Check to see which formats can handle an RowsOfFields
  51. $validFormats = $this->formatterManager->validFormats($rowsOfFieldsRef);
  52. $this->assertEquals('csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml', implode(',', $validFormats));
  53. // TODO: it woud be better if this returned an empty set instead of 'string'.
  54. $validFormats = $this->formatterManager->validFormats($notADataType);
  55. $this->assertEquals('string', implode(',', $validFormats));
  56. }
  57. function testAutomaticOptions()
  58. {
  59. $rowsOfFieldsRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields');
  60. $formatterOptions = new FormatterOptions(
  61. [
  62. FormatterOptions::FIELD_LABELS => "name: Name\nphone_number: Phone Number",
  63. ]
  64. );
  65. $inputOptions = $this->formatterManager->automaticOptions($formatterOptions, $rowsOfFieldsRef);
  66. $this->assertInputOptionDescriptionsEquals("Format the result data. Available formats: csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml [Default: 'table']\nAvailable fields: Name (name), Phone Number (phone_number) [Default: '']\nSelect just one field, and force format to 'string'. [Default: '']", $inputOptions);
  67. }
  68. function assertInputOptionDescriptionsEquals($expected, $inputOptions)
  69. {
  70. $descriptions = [];
  71. foreach ($inputOptions as $inputOption) {
  72. $descriptions[] = $inputOption->getDescription() . " [Default: '" . $inputOption->getDefault() . "']";
  73. }
  74. $this->assertEquals($expected, implode("\n", $descriptions));
  75. }
  76. }