PK ?VjӢ phpunit.xml.distnu W+A
tests
src
FormatterInterface.php.php
OverrideRestructureInterface.php.php
RestructureInterface.php.php
ValidationInterface.php
Formatters/RenderDataInterface.php
StructuredData/ListDataInterface.php.php
StructuredData/RenderCellInterface.php.php
StructuredData/TableDataInterface.php.php
PK ?Vƻ
.editorconfignu W+A # This file is for unifying the coding style for different editors and IDEs
# editorconfig.org
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[**.php]
indent_style = space
indent_size = 4
PK ?V1 1 LICENSEnu W+A Copyright (c) 2016 Consolidation Org Developers
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
PK ?V4so9 9 CONTRIBUTING.mdnu W+A # Contributing to Consolidation
Thank you for your interest in contributing to the Consolidation effort! Consolidation aims to provide reusable, loosely-coupled components useful for building command-line tools. Consolidation is built on top of Symfony Console, but aims to separate the tool from the implementation details of Symfony.
Here are some of the guidelines you should follow to make the most of your efforts:
## Code Style Guidelines
Consolidation adheres to the [PSR-2 Coding Style Guide](http://www.php-fig.org/psr/psr-2/) for PHP code.
## Pull Request Guidelines
Every pull request is run through:
- phpcs -n --standard=PSR2 src
- phpunit
- [Scrutinizer](https://scrutinizer-ci.com/g/consolidation-org/annotation-command/)
It is easy to run the unit tests and code sniffer locally; simply ensure that `./vendor/bin` is in your `$PATH`, cd to the root of the project directory, and run `phpcs` and `phpunit` as shown above. To automatically fix coding standard errors, run:
- phpcbf --standard=PSR2 src
After submitting a pull request, please examine the Scrutinizer report. It is not required to fix all Scrutinizer issues; you may ignore recommendations that you disagree with. The spacing patches produced by Scrutinizer do not conform to PSR2 standards, and therefore should never be applied. DocBlock patches may be applied at your discression. Things that Scrutinizer identifies as a bug nearly always needs to be addressed.
Pull requests must pass phpcs and phpunit in order to be merged; ideally, new functionality will also include new unit tests.
PK ?V~ .travis.ymlnu W+A language: php
branches:
# Only test the master branch and SemVer tags.
only:
- master
- /^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+.*$/
php:
- 5.4
- 5.5
- 5.6
- 7.0
sudo: false
cache:
directories:
- vendor
- $HOME/.composer/cache
before_script:
- composer install
script:
- vendor/bin/phpcs --standard=PSR2 -n src
- vendor/bin/phpunit
after_success:
- travis_retry php vendor/bin/coveralls -v
PK ?VX
composer.jsonnu W+A {
"name": "consolidation/output-formatters",
"description": "Format text by applying transformations provided by plug-in formatters.",
"license": "MIT",
"authors": [
{
"name": "Greg Anderson",
"email": "greg.1.anderson@greenknowe.org"
}
],
"autoload":{
"psr-4":{
"Consolidation\\OutputFormatters\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Consolidation\\TestUtils\\": "tests/src"
}
},
"require": {
"php": ">=5.4.0",
"symfony/console": "~2.5|~3.0",
"symfony/finder": "~2.5|~3.0"
},
"require-dev": {
"phpunit/phpunit": "4.*",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "2.*"
},
"scripts": {
"cs": "phpcs --standard=PSR2 -n src",
"cbf": "phpcbf --standard=PSR2 -n src",
"test": "phpunit --colors=always"
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}
PK ?V CHANGELOG.mdnu W+A # Change Log
### 2.0.0 - 15 September 2016
Have the default `string` format convert the result into a tab-separated-value table if possible. Commands may select a single field to emit in this instance with an annotation:
@default-string-field email
By this means, a given command may by default emit a single value, but also provide more rich output that may be shown by selecting --format=table, --format=yaml or the like.
This change might cause some commands to produce output in situations that previously were not documented as producing output. Therefore, this change is considered to be not backwards-compatible with previous releases.
### 1.1.0 - 14 September 2016
Add tab-separated-value (tsv) formatter.
### 1.0.0 - 19 May 2016
First stable release.
PK ?VC?nO tests/testFormatterOptions.phpnu W+A bind($definition);
return $input;
}
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', $options->getFormat());
$this->assertEquals('table', $options->getFormat($defaults));
// Override has higher priority than configuration and defaults
$options = new FormatterOptions($configurationData, $userOptions);
$newOptions = $options->override([FormatterOptions::DEFAULT_FORMAT => 'json']);
$this->assertEquals('json', $newOptions->getFormat());
$this->assertEquals('json', $newOptions->getFormat($defaults));
$options = new FormatterOptions($configurationData, $userOptions);
$options->setConfigurationDefault(FormatterOptions::DEFAULT_FORMAT, 'php');
$this->assertEquals('table', $options->getFormat());
$options = new FormatterOptions($configurationData, $userOptions);
$options->setConfigurationData([]);
$this->assertEquals('', $options->getFormat());
// 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'));
}
}
PK ?VSBk{ { tests/testFormatters.phpnu W+A formatterManager = new FormatterManager();
}
function assertFormattedOutputMatches($expected, $format, $data, FormatterOptions $options = null, $userOptions = []) {
if (!$options) {
$options = new FormatterOptions();
}
$options->setOptions($userOptions);
$output = new BufferedOutput();
$this->formatterManager->write($output, $format, $data, $options);
$actual = preg_replace('#[ \t]*$#sm', '', $output->fetch());
$this->assertEquals(rtrim($expected), rtrim($actual));
}
function testSimpleYaml()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = <<assertFormattedOutputMatches($expected, 'yaml', $data);
}
function testNestedYaml()
{
$data = [
'one' => [
'i' => ['a', 'b', 'c'],
],
'two' => [
'ii' => ['q', 'r', 's'],
],
'three' => [
'iii' => ['t', 'u', 'v'],
],
];
$expected = <<assertFormattedOutputMatches($expected, 'yaml', $data);
}
function testSimpleJson()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = <<assertFormattedOutputMatches($expected, 'json', $data);
}
function testSerializeFormat()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = 'a:3:{s:3:"one";s:1:"a";s:3:"two";s:1:"b";s:5:"three";s:1:"c";}';
$this->assertFormattedOutputMatches($expected, 'php', $data);
}
function testNestedJson()
{
$data = [
'one' => [
'i' => ['a', 'b', 'c'],
],
'two' => [
'ii' => ['q', 'r', 's'],
],
'three' => [
'iii' => ['t', 'u', 'v'],
],
];
$expected = <<assertFormattedOutputMatches($expected, 'json', $data);
}
function testSimplePrintR()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = << a
[two] => b
[three] => c
)
EOT;
$this->assertFormattedOutputMatches($expected, 'print-r', $data);
}
function testNestedPrintR()
{
$data = [
'one' => [
'i' => ['a', 'b', 'c'],
],
'two' => [
'ii' => ['q', 'r', 's'],
],
'three' => [
'iii' => ['t', 'u', 'v'],
],
];
$expected = << Array
(
[i] => Array
(
[0] => a
[1] => b
[2] => c
)
)
[two] => Array
(
[ii] => Array
(
[0] => q
[1] => r
[2] => s
)
)
[three] => Array
(
[iii] => Array
(
[0] => t
[1] => u
[2] => v
)
)
)
EOT;
$this->assertFormattedOutputMatches($expected, 'print-r', $data);
}
function testSimpleVarExport()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = << 'a',
'two' => 'b',
'three' => 'c',
)
EOT;
$this->assertFormattedOutputMatches($expected, 'var_export', $data);
}
function testNestedVarExport()
{
$data = [
'one' => [
'i' => ['a', 'b', 'c'],
],
'two' => [
'ii' => ['q', 'r', 's'],
],
'three' => [
'iii' => ['t', 'u', 'v'],
],
];
$expected = <<
array (
'i' =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
),
'two' =>
array (
'ii' =>
array (
0 => 'q',
1 => 'r',
2 => 's',
),
),
'three' =>
array (
'iii' =>
array (
0 => 't',
1 => 'u',
2 => 'v',
),
),
)
EOT;
$this->assertFormattedOutputMatches($expected, 'var_export', $data);
}
function testList()
{
$data = [
'one' => 'a',
'two' => 'b',
'three' => 'c',
];
$expected = <<assertFormattedOutputMatches($expected, 'list', $data);
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\UnknownFormatException
* @expectedExceptionCode 1
* @expectedExceptionMessage The requested format, 'no-such-format', is not available.
*/
function testBadFormat()
{
$this->assertFormattedOutputMatches('Will fail, not return', 'no-such-format', ['a' => 'b']);
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\IncompatibleDataException
* @expectedExceptionCode 1
* @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\CsvFormatter must be one of an instance of Consolidation\OutputFormatters\StructuredData\RowsOfFields, an instance of Consolidation\OutputFormatters\StructuredData\AssociativeList or an array. Instead, a string was provided.
*/
function testBadDataTypeForCsv()
{
$this->assertFormattedOutputMatches('Will fail, not return', 'csv', 'String cannot be converted to csv');
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\IncompatibleDataException
* @expectedExceptionCode 1
* @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\JsonFormatter must be an array. Instead, a string was provided.
*/
function testBadDataTypeForJson()
{
$this->assertFormattedOutputMatches('Will fail, not return', 'json', 'String cannot be converted to json');
}
function testNoFormatterSelected()
{
$data = 'Hello';
$expected = $data;
$this->assertFormattedOutputMatches($expected, '', $data);
}
function testRenderTableAsString()
{
$data = new RowsOfFields([['f1' => 'A', 'f2' => 'B', 'f3' => 'C'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
$expected = "A\tB\tC\nx\ty\tz";
$this->assertFormattedOutputMatches($expected, 'string', $data);
}
function testRenderTableAsStringWithSingleField()
{
$data = new RowsOfFields([['f1' => 'q', 'f2' => 'r', 'f3' => 's'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
$expected = "q\nx";
$options = new FormatterOptions([FormatterOptions::DEFAULT_STRING_FIELD => 'f1']);
$this->assertFormattedOutputMatches($expected, 'string', $data, $options);
}
function testRenderTableAsStringWithSingleFieldAndUserSelectedField()
{
$data = new RowsOfFields([['f1' => 'q', 'f2' => 'r', 'f3' => 's'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
$expected = "r\ny";
$options = new FormatterOptions([FormatterOptions::DEFAULT_STRING_FIELD => 'f1']);
$this->assertFormattedOutputMatches($expected, 'string', $data, $options, ['fields' => 'f2']);
}
function testSimpleCsv()
{
$data = ['a', 'b', 'c'];
$expected = "a,b,c";
$this->assertFormattedOutputMatches($expected, 'csv', $data);
}
function testLinesOfCsv()
{
$data = [['a', 'b', 'c'], ['x', 'y', 'z']];
$expected = "a,b,c\nx,y,z";
$this->assertFormattedOutputMatches($expected, 'csv', $data);
}
function testCsvWithEscapedValues()
{
$data = ["Red apple", "Yellow lemon"];
$expected = '"Red apple","Yellow lemon"';
$this->assertFormattedOutputMatches($expected, 'csv', $data);
}
function testCsvWithEmbeddedSingleQuote()
{
$data = ["John's book", "Mary's laptop"];
$expected = <<assertFormattedOutputMatches($expected, 'csv', $data);
}
function testCsvWithEmbeddedDoubleQuote()
{
$data = ['The "best" solution'];
$expected = <<assertFormattedOutputMatches($expected, 'csv', $data);
}
function testCsvBothKindsOfQuotes()
{
$data = ["John's \"new\" book", "Mary's \"modified\" laptop"];
$expected = <<assertFormattedOutputMatches($expected, 'csv', $data);
}
function testSimpleTsv()
{
$data = ['a', 'b', 'c'];
$expected = "a\tb\tc";
$this->assertFormattedOutputMatches($expected, 'tsv', $data);
}
function testLinesOfTsv()
{
$data = [['a', 'b', 'c'], ['x', 'y', 'z']];
$expected = "a\tb\tc\nx\ty\tz";
$this->assertFormattedOutputMatches($expected, 'tsv', $data);
}
function testTsvBothKindsOfQuotes()
{
$data = ["John's \"new\" book", "Mary's \"modified\" laptop"];
$expected = "John's \"new\" book\tMary's \"modified\" laptop";
$this->assertFormattedOutputMatches($expected, 'tsv', $data);
}
function testTsvWithEscapedValues()
{
$data = ["Red apple", "Yellow lemon", "Embedded\ttab"];
$expected = "Red apple\tYellow lemon\tEmbedded\\ttab";
$this->assertFormattedOutputMatches($expected, 'tsv', $data);
}
protected function missingCellTableExampleData()
{
$data = [
[
'one' => 'a',
'two' => 'b',
'three' => 'c',
],
[
'one' => 'x',
'three' => 'z',
],
];
return new RowsOfFields($data);
}
function testTableWithMissingCell()
{
$data = $this->missingCellTableExampleData();
$expected = <<assertFormattedOutputMatches($expected, 'table', $data);
$expectedCsv = <<assertFormattedOutputMatches($expectedCsv, 'csv', $data);
$expectedTsv = <<assertFormattedOutputMatches($expectedTsv, 'tsv', $data);
$expectedTsvWithHeaders = <<assertFormattedOutputMatches($expectedTsvWithHeaders, 'tsv', $data, new FormatterOptions(), ['include-field-labels' => true]);
}
protected function simpleTableExampleData()
{
$data = [
'id-123' =>
[
'one' => 'a',
'two' => 'b',
'three' => 'c',
],
'id-456' =>
[
'one' => 'x',
'two' => 'y',
'three' => 'z',
],
];
return new RowsOfFields($data);
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
* @expectedExceptionCode 1
* @expectedExceptionMessage The format table cannot be used with the data produced by this command, which was an array. Valid formats are: csv,json,list,php,print-r,string,tsv,var_export,xml,yaml
*/
function testIncompatibleDataForTableFormatter()
{
$data = $this->simpleTableExampleData()->getArrayCopy();
$this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'table', $data);
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
* @expectedExceptionCode 1
* @expectedExceptionMessage The format sections cannot be used with the data produced by this command, which was an array. Valid formats are: csv,json,list,php,print-r,string,tsv,var_export,xml,yaml
*/
function testIncompatibleDataForSectionsFormatter()
{
$data = $this->simpleTableExampleData()->getArrayCopy();
$this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'sections', $data);
}
function testSimpleTable()
{
$data = $this->simpleTableExampleData();
$expected = <<assertFormattedOutputMatches($expected, 'table', $data);
$expectedBorderless = <<assertFormattedOutputMatches($expectedBorderless, 'table', $data, new FormatterOptions(['table-style' => 'borderless']));
$expectedJson = <<assertFormattedOutputMatches($expectedJson, 'json', $data);
$expectedCsv = <<assertFormattedOutputMatches($expectedCsv, 'csv', $data);
$expectedList = <<assertFormattedOutputMatches($expectedList, 'list', $data);
}
protected function tableWithAlternativesExampleData()
{
$data = [
'id-123' =>
[
'one' => 'a',
'two' => ['this', 'that', 'the other thing'],
'three' => 'c',
],
'id-456' =>
[
'one' => 'x',
'two' => 'y',
'three' => ['apples', 'oranges'],
],
];
return new RowsOfFieldsWithAlternatives($data);
}
function testTableWithAlternatives()
{
$data = $this->tableWithAlternativesExampleData();
$expected = <<assertFormattedOutputMatches($expected, 'table', $data);
$expectedBorderless = <<assertFormattedOutputMatches($expectedBorderless, 'table', $data, new FormatterOptions(['table-style' => 'borderless']));
$expectedJson = <<assertFormattedOutputMatches($expectedJson, 'json', $data);
$expectedCsv = <<assertFormattedOutputMatches($expectedCsv, 'csv', $data);
$expectedList = <<assertFormattedOutputMatches($expectedList, 'list', $data);
}
function testSimpleTableWithFieldLabels()
{
$data = $this->simpleTableExampleData();
$configurationData = new FormatterOptions(
[
'field-labels' => ['one' => 'Ichi', 'two' => 'Ni', 'three' => 'San'],
'row-labels' => ['id-123' => 'Walrus', 'id-456' => 'Carpenter'],
]
);
$configurationDataAnnotationFormat = new FormatterOptions(
[
'field-labels' => "one: Uno\ntwo: Dos\nthree: Tres",
]
);
$expected = <<assertFormattedOutputMatches($expected, 'table', $data, $configurationData);
$expectedSidewaysTable = <<assertFormattedOutputMatches($expectedSidewaysTable, 'table', $data, $configurationData->override(['list-orientation' => true]));
$expectedAnnotationFormatConfigData = <<assertFormattedOutputMatches($expectedAnnotationFormatConfigData, 'table', $data, $configurationDataAnnotationFormat);
$expectedWithNoFields = <<assertFormattedOutputMatches($expectedWithNoFields, 'table', $data, $configurationData, ['include-field-labels' => false]);
$expectedWithReorderedFields = <<assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['three', 'one']]);
$this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
$this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => 'San,Ichi']);
$expectedWithRegexField = <<assertFormattedOutputMatches($expectedWithRegexField, 'table', $data, $configurationData, ['fields' => ['/e$/']]);
$this->assertFormattedOutputMatches($expectedWithRegexField, 'table', $data, $configurationData, ['fields' => ['*e']]);
$expectedSections = <<assertFormattedOutputMatches($expectedSections, 'sections', $data, $configurationData);
$expectedJson = <<assertFormattedOutputMatches($expectedJson, 'json', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
}
protected function simpleListExampleData()
{
$data = [
'one' => 'apple',
'two' => 'banana',
'three' => 'carrot',
];
return new AssociativeList($data);
}
/**
* @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
* @expectedExceptionCode 1
* @expectedExceptionMessage The format table cannot be used with the data produced by this command, which was an array. Valid formats are: csv,json,list,php,print-r,string,tsv,var_export,xml,yaml
*/
function testIncompatibleListDataForTableFormatter()
{
$data = $this->simpleListExampleData();
$this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'table', $data->getArrayCopy());
}
function testSimpleList()
{
$data = $this->simpleListExampleData();
$expected = <<assertFormattedOutputMatches($expected, 'table', $data);
$expectedRotated = <<assertFormattedOutputMatches($expectedRotated, 'table', $data, new FormatterOptions(['list-orientation' => false]));
$expectedList = <<< EOT
apple
banana
carrot
EOT;
$this->assertFormattedOutputMatches($expectedList, 'list', $data);
$expectedCsv = <<< EOT
One,Two,Three
apple,banana,carrot
EOT;
$this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
$expectedCsvNoHeaders = 'apple,banana,carrot';
$this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, new FormatterOptions(), ['include-field-labels' => false]);
// Next, configure the formatter options with 'include-field-labels',
// but set --include-field-labels to turn the option back on again.
$options = new FormatterOptions(['include-field-labels' => false]);
$input = new StringInput('test --include-field-labels');
$optionDefinitions = [
new InputArgument('unused', InputArgument::REQUIRED),
new InputOption('include-field-labels', null, InputOption::VALUE_NONE),
];
$definition = new InputDefinition($optionDefinitions);
$input->bind($definition);
$testValue = $input->getOption('include-field-labels');
$this->assertTrue($testValue);
$hasFieldLabels = $input->hasOption('include-field-labels');
$this->assertTrue($hasFieldLabels);
$this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, $options);
$options->setInput($input);
$this->assertFormattedOutputMatches($expectedCsv, 'csv', $data, $options);
}
protected function associativeListWithCsvCells()
{
$data = [
'one' => 'apple',
'two' => ['banana', 'plantain'],
'three' => 'carrot',
'four' => ['peaches', 'pumpkin pie'],
];
return new AssociativeListWithCsvCells($data);
}
function testAssociativeListWithCsvCells()
{
$data = $this->associativeListWithCsvCells();
$expected = <<assertFormattedOutputMatches($expected, 'table', $data);
$expectedList = <<< EOT
apple
banana,plantain
carrot
peaches,pumpkin pie
EOT;
$this->assertFormattedOutputMatches($expectedList, 'list', $data);
$expectedCsv = <<< EOT
One,Two,Three,Four
apple,"banana,plantain",carrot,"peaches,pumpkin pie"
EOT;
$this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
$expectedCsvNoHeaders = 'apple,"banana,plantain",carrot,"peaches,pumpkin pie"';
$this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, new FormatterOptions(), ['include-field-labels' => false]);
}
function testSimpleListWithFieldLabels()
{
$data = $this->simpleListExampleData();
$configurationData = new FormatterOptions(
[
'field-labels' => ['one' => 'Ichi', 'two' => 'Ni', 'three' => 'San'],
]
);
$expected = <<assertFormattedOutputMatches($expected, 'table', $data, $configurationData);
$expectedWithReorderedFields = <<assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['three', 'one']]);
$this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
$expectedJson = <<assertFormattedOutputMatches($expectedJson, 'json', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
}
function testSimpleXml()
{
$data = [
'name' => 'primary',
'description' => 'The primary colors of the color wheel.',
'colors' =>
[
'red',
'yellow',
'blue',
],
];
$expected = <<
The primary colors of the color wheel.
red
yellow
blue
EOT;
$this->assertFormattedOutputMatches($expected, 'xml', $data);
}
function domDocumentData()
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$document = $dom->createElement('document');
$dom->appendChild($document);
$document->setAttribute('name', 'primary');
$description = $dom->createElement('description');
$document->appendChild($description);
$description->appendChild($dom->createTextNode('The primary colors of the color wheel.'));
$this->domCreateElements($dom, $document, 'color', ['red', 'yellow', 'blue']);
return $dom;
}
function domCreateElements($dom, $element, $name, $data)
{
$container = $dom->createElement("{$name}s");
$element->appendChild($container);
foreach ($data as $value) {
$child = $dom->createElement($name);
$container->appendChild($child);
$child->appendChild($dom->createTextNode($value));
}
}
function complexDomDocumentData()
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$document = $dom->createElement('document');
$dom->appendChild($document);
$document->setAttribute('name', 'widget-collection');
$description = $dom->createElement('description');
$document->appendChild($description);
$description->appendChild($dom->createTextNode('A couple of widgets.'));
$widgets = $dom->createElement('widgets');
$document->appendChild($widgets);
$widget = $dom->createElement('widget');
$widgets->appendChild($widget);
$widget->setAttribute('name', 'usual');
$this->domCreateElements($dom, $widget, 'color', ['red', 'yellow', 'blue']);
$this->domCreateElements($dom, $widget, 'shape', ['square', 'circle', 'triangle']);
$widget = $dom->createElement('widget');
$widgets->appendChild($widget);
$widget->setAttribute('name', 'unusual');
$this->domCreateElements($dom, $widget, 'color', ['muave', 'puce', 'umber']);
$this->domCreateElements($dom, $widget, 'shape', ['elipse', 'rhombus', 'trapazoid']);
return $dom;
}
function domDocumentTestValues()
{
$expectedXml = <<
The primary colors of the color wheel.
red
yellow
blue
EOT;
$expectedJson = <<
A couple of widgets.
red
yellow
blue
square
circle
triangle
muave
puce
umber
elipse
rhombus
trapazoid
EOT;
$expectedComplexJson = <<domDocumentData(),
$expectedXml,
$expectedJson,
],
[
$this->complexDomDocumentData(),
$expectedComplexXml,
$expectedComplexJson,
],
];
}
/**
* @dataProvider domDocumentTestValues
*/
function testDomData($data, $expectedXml, $expectedJson)
{
$this->assertFormattedOutputMatches($expectedXml, 'xml', $data);
$this->assertFormattedOutputMatches($expectedJson, 'json', $data);
// Check to see if we get the same xml data if we convert from
// DOM -> array -> DOM.
$expectedJsonAsArray = (array)json_decode($expectedJson);
$this->assertFormattedOutputMatches($expectedXml, 'xml', $expectedJsonAsArray);
}
/**
* @expectedException \Exception
* @expectedExceptionCode 1
* @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\XmlFormatter must be either an instance of DOMDocument or an array. Instead, a string was provided.
*/
function testDataTypeForXmlFormatter()
{
$this->assertFormattedOutputMatches('Will fail, not return', 'xml', 'Strings cannot be converted to XML');
}
}
PK ?Vdi? ? tests/testIncompatibleData.phpnu W+A 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\AssociativeList. 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\AssociativeList. 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\AssociativeList. 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\AssociativeList. Instead, an instance of Consolidation\OutputFormatters\StructuredData\AssociativeList was provided.', $tableFormatter, new AssociativeList([]));
}
/**
* @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\AssociativeList. 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);
}
}
PK ?V
* tests/src/RowsOfFieldsWithAlternatives.phpnu W+A formatterManager = new FormatterManager();
}
function testValidFormats()
{
$arrayObjectRef = new \ReflectionClass('\ArrayObject');
$associativeListRef = new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList');
$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 AssociativeList
$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: Name,Phone Number]", $inputOptions);
}
function assertInputOptionDescriptionsEquals($expected, $inputOptions)
{
$descriptions = [];
foreach ($inputOptions as $inputOption) {
$descriptions[] = $inputOption->getDescription() . ' [Default: ' . $inputOption->getDefault() . ']';
}
$this->assertEquals($expected, implode("\n", $descriptions));
}
}
PK ?Vxm@ @
.gitignorenu W+A .DS_Store
.idea
phpunit.xml
build
vendor
composer.lock
main.php
PK ?Vw"&