First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,17 @@
<?php
namespace Consolidation\TestUtils;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Consolidation\OutputFormatters\StructuredData\RenderCellInterface;
class PropertyListWithCsvCells extends PropertyList implements RenderCellInterface
{
public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
{
if (is_array($cellData)) {
return implode(',', $cellData);
}
return $cellData;
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Consolidation\TestUtils;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\RenderCellInterface;
class RowsOfFieldsWithAlternatives extends RowsOfFields implements RenderCellInterface
{
public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
{
if (is_array($cellData)) {
return implode('|', $cellData);
}
return $cellData;
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Consolidation\OutputFormatters;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
class APIDocsTests extends \PHPUnit_Framework_TestCase
{
function testAPIDocs()
{
if (getenv('CI')) {
$this->markTestIncomplete(
'API generation has slight variations when run on CI server. This test is therefore skipped on CI until we can make the test results consistent.'
);
}
$testDocs = tempnam(sys_get_temp_dir(), 'TestAPIDocs.md');
$currentDocs = getcwd() . '/docs/api.md';
passthru("vendor/bin/phpdoc-md generate src > $testDocs");
$testDocsContent = file_get_contents($testDocs);
$currentDocsContent = file_get_contents($currentDocs);
$testDocsContent = str_replace (array("\r\n", "\r"), "\n", $testDocsContent);
$currentDocsContent = str_replace (array("\r\n", "\r"), "\n", $currentDocsContent);
$this->assertEquals($testDocsContent, $currentDocsContent, "API docuementation out of date. Run 'composer api' to update.");
}
}

View file

@ -0,0 +1,112 @@
<?php
namespace Consolidation\OutputFormatters;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
class FormatterOptionsTests extends \PHPUnit_Framework_TestCase
{
public function createStringInput($testCommandline)
{
$input = new StringInput($testCommandline);
$optionDefinitions = [
new InputArgument('unused', InputArgument::REQUIRED),
new InputOption(FormatterOptions::FORMAT, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::TABLE_STYLE, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::FIELD, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::FIELDS, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::INCLUDE_FIELD_LABELS, null, InputOption::VALUE_NONE),
new InputOption(FormatterOptions::ROW_LABELS, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::FIELD_LABELS, null, InputOption::VALUE_REQUIRED),
// These probably don't make senes to alter via options
new InputOption(FormatterOptions::DEFAULT_FORMAT, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::DEFAULT_FIELDS, null, InputOption::VALUE_REQUIRED),
new InputOption(FormatterOptions::LIST_ORIENTATION, null, InputOption::VALUE_NONE),
];
$definition = new InputDefinition($optionDefinitions);
$input->bind($definition);
return $input;
}
protected function getFormat(FormatterOptions $options, $defaults = [])
{
return $options->get(FormatterOptions::FORMAT, [], $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, ''));
}
public function testFormatterOptions() {
$configurationData = [
FormatterOptions::DEFAULT_FORMAT => 'table',
'test' => 'one',
'try' => 'two',
];
$userOptions = [
'try' => 'three',
];
$defaults = [
FormatterOptions::DEFAULT_FORMAT => 'var_export',
'try' => 'four',
'default-only' => 'defaulty',
];
// Create a StringInput object and ensure that Symfony Console is working right.
$input = $this->createStringInput('test --format=yaml --include-field-labels');
$testValue = $input->getOption(FormatterOptions::INCLUDE_FIELD_LABELS);
$this->assertTrue($testValue);
$testValue = $input->getOption(FormatterOptions::FORMAT);
$this->assertEquals('yaml', $testValue);
// $options->get() only returns the default parameter is there is
// no matching key in configuration, userOptions or defaults.
$options = new FormatterOptions($configurationData, $userOptions);
$this->assertEquals('', $options->get('default-only'));
$this->assertEquals('defaulty', $options->get('default-only', $defaults));
$this->assertEquals('defaulty', $options->get('default-only', $defaults, 'irrelevant'));
$this->assertEquals('three', $options->get('try'));
$this->assertEquals('three', $options->get('try', $defaults));
$this->assertEquals('three', $options->get('try', $defaults, 'irrelevant'));
$this->assertFalse($options->get('no-such-key'));
$this->assertFalse($options->get('no-such-key', $defaults));
$this->assertEquals('last-chance', $options->get('no-such-key', $defaults, 'last-chance'));
// Change a user option
$options = new FormatterOptions($configurationData, $userOptions);
$options->setOption('try', 'changed');
$this->assertEquals('changed', $options->get('try'));
$this->assertEquals('changed', $options->get('try', $defaults));
$this->assertEquals('changed', $options->get('try', $defaults, 'irrelevant'));
// Configuration has higher priority than defaults
$options = new FormatterOptions($configurationData, $userOptions);
$this->assertEquals('table', $this->getFormat($options));
$this->assertEquals('table', $this->getFormat($options, $defaults));
// Override has higher priority than configuration and defaults
$options = new FormatterOptions($configurationData, $userOptions);
$newOptions = $options->override([FormatterOptions::DEFAULT_FORMAT => 'json']);
$this->assertEquals('json', $this->getFormat($newOptions));
$this->assertEquals('json', $this->getFormat($newOptions, $defaults));
$options = new FormatterOptions($configurationData, $userOptions);
$options->setConfigurationDefault(FormatterOptions::DEFAULT_FORMAT, 'php');
$this->assertEquals('table', $this->getFormat($options));
$options = new FormatterOptions($configurationData, $userOptions);
$options->setConfigurationData([]);
$this->assertEquals('', $this->getFormat($options));
// It is only possible to override options that appear in '$default'
// with $input; if there are no defaults, then the --format=yaml
// option will not be picked up.
$options = new FormatterOptions($configurationData, $userOptions);
$options->setInput($input);
$this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT));
$this->assertEquals('table', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant'));
// We won't see the default value unless the configuration value is empty.
$options = new FormatterOptions([], $userOptions);
$this->assertEquals('var_export', $options->get(FormatterOptions::DEFAULT_FORMAT, $defaults, 'irrelevant'));
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,64 @@
<?php
namespace Consolidation\OutputFormatters;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
class IncompatibleDataTests extends \PHPUnit_Framework_TestCase
{
protected $formatterManager;
function setup() {
$this->formatterManager = new FormatterManager();
}
protected function assertIncompatibleDataMessage($expected, $formatter, $data)
{
$e = new IncompatibleDataException($formatter, $data, $formatter->validDataTypes());
$this->assertEquals($expected, $e->getMessage());
}
public function testIncompatibleData()
{
$tableFormatter = $this->formatterManager->getFormatter('table');
$this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, a string was provided.', $tableFormatter, 'a string');
$this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an instance of Consolidation\OutputFormatters\FormatterManager was provided.', $tableFormatter, $this->formatterManager);
$this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an array was provided.', $tableFormatter, []);
$this->assertIncompatibleDataMessage('Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, an instance of Consolidation\OutputFormatters\StructuredData\PropertyList was provided.', $tableFormatter, new PropertyList([]));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Undescribable data error: NULL
*/
public function testUndescribableData()
{
$tableFormatter = $this->formatterManager->getFormatter('table');
$data = $tableFormatter->validate(null);
$this->assertEquals('Will throw before comparing.', $data);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\TableFormatter must be either an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields or an instance of Consolidation\OutputFormatters\StructuredData\PropertyList. Instead, a string was provided.
*/
public function testInvalidTableData()
{
$tableFormatter = $this->formatterManager->getFormatter('table');
$data = $tableFormatter->validate('bad data type');
$this->assertEquals('Will throw before comparing.', $data);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\SectionsFormatter must be an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields. Instead, a string was provided.
*/
public function testInvalidSectionsData()
{
$tableFormatter = $this->formatterManager->getFormatter('sections');
$data = $tableFormatter->validate('bad data type');
$this->assertEquals('Will throw before comparing.', $data);
}
}

View file

@ -0,0 +1,88 @@
<?php
namespace Consolidation\OutputFormatters;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
class ValidFormatsTests extends \PHPUnit_Framework_TestCase
{
protected $formatterManager;
function setup() {
$this->formatterManager = new FormatterManager();
$this->formatterManager->addDefaultFormatters();
$this->formatterManager->addDefaultSimplifiers();
}
function testValidFormats()
{
$arrayObjectRef = new \ReflectionClass('\ArrayObject');
$associativeListRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\PropertyList');
$rowsOfFieldsRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields');
$notADataType = new \ReflectionClass('\Consolidation\OutputFormatters\FormatterManager');
$jsonFormatter = $this->formatterManager->getFormatter('json');
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, $notADataType);
$this->assertFalse($isValid);
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, new \ArrayObject());
$this->assertTrue($isValid);
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, $arrayObjectRef);
$this->assertTrue($isValid);
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, []);
$this->assertTrue($isValid);
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, $associativeListRef);
$this->assertTrue($isValid);
$isValid = $this->formatterManager->isValidFormat($jsonFormatter, $rowsOfFieldsRef);
$this->assertTrue($isValid);
$sectionsFormatter = $this->formatterManager->getFormatter('sections');
$isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $notADataType);
$this->assertFalse($isValid);
$isValid = $this->formatterManager->isValidFormat($sectionsFormatter, []);
$this->assertFalse($isValid);
$isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $arrayObjectRef);
$this->assertFalse($isValid);
$isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $rowsOfFieldsRef);
$this->assertTrue($isValid);
$isValid = $this->formatterManager->isValidFormat($sectionsFormatter, $associativeListRef);
$this->assertFalse($isValid);
// Check to see which formats can handle a simple array
$validFormats = $this->formatterManager->validFormats([]);
$this->assertEquals('csv,json,list,php,print-r,string,tsv,var_export,xml,yaml', implode(',', $validFormats));
// Check to see which formats can handle an PropertyList
$validFormats = $this->formatterManager->validFormats($associativeListRef);
$this->assertEquals('csv,json,list,php,print-r,string,table,tsv,var_export,xml,yaml', implode(',', $validFormats));
// Check to see which formats can handle an RowsOfFields
$validFormats = $this->formatterManager->validFormats($rowsOfFieldsRef);
$this->assertEquals('csv,json,list,php,print-r,sections,string,table,tsv,var_export,xml,yaml', implode(',', $validFormats));
// TODO: it woud be better if this returned an empty set instead of 'string'.
$validFormats = $this->formatterManager->validFormats($notADataType);
$this->assertEquals('string', implode(',', $validFormats));
}
function testAutomaticOptions()
{
$rowsOfFieldsRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields');
$formatterOptions = new FormatterOptions(
[
FormatterOptions::FIELD_LABELS => "name: Name\nphone_number: Phone Number",
]
);
$inputOptions = $this->formatterManager->automaticOptions($formatterOptions, $rowsOfFieldsRef);
$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);
}
function assertInputOptionDescriptionsEquals($expected, $inputOptions)
{
$descriptions = [];
foreach ($inputOptions as $inputOption) {
$descriptions[] = $inputOption->getDescription() . " [Default: '" . $inputOption->getDefault() . "']";
}
$this->assertEquals($expected, implode("\n", $descriptions));
}
}