2 namespace Consolidation\OutputFormatters;
4 use Consolidation\TestUtils\PropertyListWithCsvCells;
5 use Consolidation\TestUtils\RowsOfFieldsWithAlternatives;
6 use Consolidation\OutputFormatters\Options\FormatterOptions;
7 use Consolidation\OutputFormatters\StructuredData\AssociativeList;
8 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
9 use Consolidation\OutputFormatters\StructuredData\PropertyList;
10 use Symfony\Component\Console\Output\BufferedOutput;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\StringInput;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputDefinition;
18 class FormattersTests extends \PHPUnit_Framework_TestCase
20 protected $formatterManager;
23 $this->formatterManager = new FormatterManager();
26 function assertFormattedOutputMatches($expected, $format, $data, FormatterOptions $options = null, $userOptions = []) {
28 $options = new FormatterOptions();
30 $options->setOptions($userOptions);
31 $output = new BufferedOutput();
32 $this->formatterManager->write($output, $format, $data, $options);
33 $actual = preg_replace('#[ \t]*$#sm', '', $output->fetch());
34 $this->assertEquals(rtrim($expected), rtrim($actual));
37 function testSimpleYaml()
51 $this->assertFormattedOutputMatches($expected, 'yaml', $data);
54 function testNestedYaml()
58 'i' => ['a', 'b', 'c'],
61 'ii' => ['q', 'r', 's'],
64 'iii' => ['t', 'u', 'v'],
86 $this->assertFormattedOutputMatches($expected, 'yaml', $data);
89 function testSimpleJson()
105 $this->assertFormattedOutputMatches($expected, 'json', $data);
108 function testSerializeFormat()
116 $expected = 'a:3:{s:3:"one";s:1:"a";s:3:"two";s:1:"b";s:5:"three";s:1:"c";}';
118 $this->assertFormattedOutputMatches($expected, 'php', $data);
121 function testNestedJson()
125 'i' => ['a', 'b', 'c'],
128 'ii' => ['q', 'r', 's'],
131 'iii' => ['t', 'u', 'v'],
161 $this->assertFormattedOutputMatches($expected, 'json', $data);
164 function testSimplePrintR()
181 $this->assertFormattedOutputMatches($expected, 'print-r', $data);
184 function testNestedPrintR()
188 'i' => ['a', 'b', 'c'],
191 'ii' => ['q', 'r', 's'],
194 'iii' => ['t', 'u', 'v'],
237 $this->assertFormattedOutputMatches($expected, 'print-r', $data);
240 function testSimpleVarExport()
256 $this->assertFormattedOutputMatches($expected, 'var_export', $data);
259 function testNestedVarExport()
263 'i' => ['a', 'b', 'c'],
266 'ii' => ['q', 'r', 's'],
269 'iii' => ['t', 'u', 'v'],
305 $this->assertFormattedOutputMatches($expected, 'var_export', $data);
322 $this->assertFormattedOutputMatches($expected, 'list', $data);
326 * @expectedException \Consolidation\OutputFormatters\Exception\UnknownFormatException
327 * @expectedExceptionCode 1
328 * @expectedExceptionMessage The requested format, 'no-such-format', is not available.
330 function testBadFormat()
332 $this->assertFormattedOutputMatches('Will fail, not return', 'no-such-format', ['a' => 'b']);
336 * @expectedException \Consolidation\OutputFormatters\Exception\IncompatibleDataException
337 * @expectedExceptionCode 1
338 * @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\PropertyList or an array. Instead, a string was provided.
340 function testBadDataTypeForCsv()
342 $this->assertFormattedOutputMatches('Will fail, not return', 'csv', 'String cannot be converted to csv');
346 * @expectedException \Consolidation\OutputFormatters\Exception\IncompatibleDataException
347 * @expectedExceptionCode 1
348 * @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\JsonFormatter must be an array. Instead, a string was provided.
350 function testBadDataTypeForJson()
352 $this->assertFormattedOutputMatches('Will fail, not return', 'json', 'String cannot be converted to json');
355 function testNoFormatterSelected()
359 $this->assertFormattedOutputMatches($expected, '', $data);
362 function testRenderTableAsString()
364 $data = new RowsOfFields([['f1' => 'A', 'f2' => 'B', 'f3' => 'C'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
365 $expected = "A\tB\tC\nx\ty\tz";
367 $this->assertFormattedOutputMatches($expected, 'string', $data);
370 function testRenderTableAsStringWithSingleField()
372 $data = new RowsOfFields([['f1' => 'q', 'f2' => 'r', 'f3' => 's'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
375 $options = new FormatterOptions([FormatterOptions::DEFAULT_STRING_FIELD => 'f1']);
377 $this->assertFormattedOutputMatches($expected, 'string', $data, $options);
380 function testRenderTableAsStringWithSingleFieldAndUserSelectedField()
382 $data = new RowsOfFields([['f1' => 'q', 'f2' => 'r', 'f3' => 's'], ['f1' => 'x', 'f2' => 'y', 'f3' => 'z']]);
385 $options = new FormatterOptions([FormatterOptions::DEFAULT_STRING_FIELD => 'f1']);
387 $this->assertFormattedOutputMatches($expected, 'string', $data, $options, ['fields' => 'f2']);
390 function testSimpleCsv()
392 $data = ['a', 'b', 'c'];
395 $this->assertFormattedOutputMatches($expected, 'csv', $data);
398 function testLinesOfCsv()
400 $data = [['a', 'b', 'c'], ['x', 'y', 'z']];
401 $expected = "a,b,c\nx,y,z";
403 $this->assertFormattedOutputMatches($expected, 'csv', $data);
406 function testCsvWithEscapedValues()
408 $data = ["Red apple", "Yellow lemon"];
409 $expected = '"Red apple","Yellow lemon"';
411 $this->assertFormattedOutputMatches($expected, 'csv', $data);
414 function testCsvWithEmbeddedSingleQuote()
416 $data = ["John's book", "Mary's laptop"];
418 "John's book","Mary's laptop"
421 $this->assertFormattedOutputMatches($expected, 'csv', $data);
424 function testCsvWithEmbeddedDoubleQuote()
426 $data = ['The "best" solution'];
428 "The ""best"" solution"
431 $this->assertFormattedOutputMatches($expected, 'csv', $data);
434 function testCsvBothKindsOfQuotes()
436 $data = ["John's \"new\" book", "Mary's \"modified\" laptop"];
438 "John's ""new"" book","Mary's ""modified"" laptop"
441 $this->assertFormattedOutputMatches($expected, 'csv', $data);
444 function testSimpleTsv()
446 $data = ['a', 'b', 'c'];
447 $expected = "a\tb\tc";
449 $this->assertFormattedOutputMatches($expected, 'tsv', $data);
452 function testLinesOfTsv()
454 $data = [['a', 'b', 'c'], ['x', 'y', 'z']];
455 $expected = "a\tb\tc\nx\ty\tz";
457 $this->assertFormattedOutputMatches($expected, 'tsv', $data);
460 function testTsvBothKindsOfQuotes()
462 $data = ["John's \"new\" book", "Mary's \"modified\" laptop"];
463 $expected = "John's \"new\" book\tMary's \"modified\" laptop";
465 $this->assertFormattedOutputMatches($expected, 'tsv', $data);
468 function testTsvWithEscapedValues()
470 $data = ["Red apple", "Yellow lemon", "Embedded\ttab"];
471 $expected = "Red apple\tYellow lemon\tEmbedded\\ttab";
473 $this->assertFormattedOutputMatches($expected, 'tsv', $data);
476 protected function missingCellTableExampleData()
489 return new RowsOfFields($data);
492 function testTableWithMissingCell()
494 $data = $this->missingCellTableExampleData();
504 $this->assertFormattedOutputMatches($expected, 'table', $data);
506 $expectedCsv = <<<EOT
511 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
513 $expectedTsv = <<<EOT
517 $this->assertFormattedOutputMatches($expectedTsv, 'tsv', $data);
519 $expectedTsvWithHeaders = <<<EOT
524 $this->assertFormattedOutputMatches($expectedTsvWithHeaders, 'tsv', $data, new FormatterOptions(), ['include-field-labels' => true]);
527 function testTableWithWordWrapping()
529 $options = new FormatterOptions();
530 $options->setWidth(42);
534 'first' => 'This is a really long cell that contains a lot of data. When it is rendered, it should be wrapped across multiple lines.',
535 'second' => 'This is the second column of the same table. It is also very long, and should be wrapped across multiple lines, just like the first column.',
538 $data = new RowsOfFields($data);
541 ------------------- --------------------
543 ------------------- --------------------
544 This is a really This is the second
545 long cell that column of the same
546 contains a lot of table. It is also
547 data. When it is very long, and
548 rendered, it should be wrapped
549 should be wrapped across multiple
550 across multiple lines, just like
551 lines. the first column.
552 ------------------- --------------------
554 $this->assertFormattedOutputMatches($expected, 'table', $data, $options);
557 protected function simpleTableExampleData()
573 return new RowsOfFields($data);
577 * @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
578 * @expectedExceptionCode 1
579 * @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
581 function testIncompatibleDataForTableFormatter()
583 $data = $this->simpleTableExampleData()->getArrayCopy();
584 $this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'table', $data);
588 * @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
589 * @expectedExceptionCode 1
590 * @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
592 function testIncompatibleDataForSectionsFormatter()
594 $data = $this->simpleTableExampleData()->getArrayCopy();
595 $this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'sections', $data);
598 function testSimpleTable()
600 $data = $this->simpleTableExampleData();
610 $this->assertFormattedOutputMatches($expected, 'table', $data);
612 $expectedBorderless = <<<EOT
620 $this->assertFormattedOutputMatches($expectedBorderless, 'table', $data, new FormatterOptions(['table-style' => 'borderless']));
622 $expectedJson = <<<EOT
636 $this->assertFormattedOutputMatches($expectedJson, 'json', $data);
638 $expectedCsv = <<<EOT
643 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
645 $expectedList = <<<EOT
649 $this->assertFormattedOutputMatches($expectedList, 'list', $data);
652 protected function tableWithAlternativesExampleData()
658 'two' => ['this', 'that', 'the other thing'],
665 'three' => ['apples', 'oranges'],
668 return new RowsOfFieldsWithAlternatives($data);
671 function testTableWithAlternatives()
673 $data = $this->tableWithAlternativesExampleData();
676 ----- --------------------------- ----------------
678 ----- --------------------------- ----------------
679 a this|that|the other thing c
681 ----- --------------------------- ----------------
683 $this->assertFormattedOutputMatches($expected, 'table', $data);
685 $expectedBorderless = <<<EOT
686 ===== =========================== ================
688 ===== =========================== ================
689 a this|that|the other thing c
691 ===== =========================== ================
693 $this->assertFormattedOutputMatches($expectedBorderless, 'table', $data, new FormatterOptions(['table-style' => 'borderless']));
695 $expectedJson = <<<EOT
716 $this->assertFormattedOutputMatches($expectedJson, 'json', $data);
718 $expectedCsv = <<<EOT
720 a,"this|that|the other thing",c
723 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
725 $expectedList = <<<EOT
729 $this->assertFormattedOutputMatches($expectedList, 'list', $data);
732 function testSimpleTableWithFieldLabels()
734 $data = $this->simpleTableExampleData();
735 $configurationData = new FormatterOptions(
737 'field-labels' => ['one' => 'Ichi', 'two' => 'Ni', 'three' => 'San'],
738 'row-labels' => ['id-123' => 'Walrus', 'id-456' => 'Carpenter'],
741 $configurationDataAnnotationFormat = new FormatterOptions(
743 'field-labels' => "one: Uno\ntwo: Dos\nthree: Tres",
755 $this->assertFormattedOutputMatches($expected, 'table', $data, $configurationData);
757 $expectedSidewaysTable = <<<EOT
764 $this->assertFormattedOutputMatches($expectedSidewaysTable, 'table', $data, $configurationData->override(['list-orientation' => true]));
766 $expectedAnnotationFormatConfigData = <<<EOT
774 $this->assertFormattedOutputMatches($expectedAnnotationFormatConfigData, 'table', $data, $configurationDataAnnotationFormat);
776 $expectedWithNoFields = <<<EOT
782 $this->assertFormattedOutputMatches($expectedWithNoFields, 'table', $data, $configurationData, ['include-field-labels' => false]);
784 $expectedWithReorderedFields = <<<EOT
792 $this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['three', 'one']]);
793 $this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
794 $this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => 'San,Ichi']);
796 $expectedWithRegexField = <<<EOT
804 $this->assertFormattedOutputMatches($expectedWithRegexField, 'table', $data, $configurationData, ['fields' => ['/e$/']]);
805 $this->assertFormattedOutputMatches($expectedWithRegexField, 'table', $data, $configurationData, ['fields' => ['*e']]);
806 $this->assertFormattedOutputMatches($expectedWithRegexField, 'table', $data, $configurationData, ['default-fields' => ['*e']]);
808 $expectedSections = <<<EOT
820 $this->assertFormattedOutputMatches($expectedSections, 'sections', $data, $configurationData);
822 $expectedJson = <<<EOT
834 $this->assertFormattedOutputMatches($expectedJson, 'json', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
836 $expectedSingleField = <<<EOT
844 $this->assertFormattedOutputMatches($expectedSingleField, 'table', $data, $configurationData, ['field' => 'San']);
846 $expectedEmptyColumn = <<<EOT
852 $this->assertFormattedOutputMatches($expectedEmptyColumn, 'table', new RowsOfFields([]), $configurationData, ['field' => 'San']);
854 $this->assertFormattedOutputMatches('', '', new RowsOfFields([]), $configurationData, ['field' => 'San']);
855 $this->assertFormattedOutputMatches('[]', 'json', new RowsOfFields([]), $configurationData, ['field' => 'San']);
859 * @expectedException \Consolidation\OutputFormatters\Exception\UnknownFieldException
860 * @expectedExceptionCode 1
861 * @expectedExceptionMessage The requested field, 'Shi', is not defined.
863 function testNoSuchFieldException()
865 $configurationData = new FormatterOptions(
867 'field-labels' => ['one' => 'Ichi', 'two' => 'Ni', 'three' => 'San'],
868 'row-labels' => ['id-123' => 'Walrus', 'id-456' => 'Carpenter'],
871 $data = $this->simpleTableExampleData();
872 $this->assertFormattedOutputMatches('Will throw before comparing', 'table', $data, $configurationData, ['field' => 'Shi']);
875 protected function simpleListExampleData()
882 return new PropertyList($data);
885 // Test with the deprecated data structure
886 protected function simpleListExampleDataUsingAssociativeList()
893 return new AssociativeList($data);
897 * @expectedException \Consolidation\OutputFormatters\Exception\InvalidFormatException
898 * @expectedExceptionCode 1
899 * @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
901 function testIncompatibleListDataForTableFormatter()
903 $data = $this->simpleListExampleData();
904 $this->assertFormattedOutputMatches('Should throw an exception before comparing the table data', 'table', $data->getArrayCopy());
907 function testEmptyList()
909 $data = new RowsOfFields([]);
917 // If we provide field labels, then the output will change to reflect that.
918 $formatterOptionsWithFieldLables = new FormatterOptions();
919 $formatterOptionsWithFieldLables
920 ->setFieldLabels(['one' => 'I', 'two' => 'II', 'three' => 'III']);
921 $this->assertFormattedOutputMatches($expected, 'table', $data, $formatterOptionsWithFieldLables);
924 function testSimpleList()
934 $data = $this->simpleListExampleDataUsingAssociativeList();
936 $this->assertFormattedOutputMatches($expected, 'table', $data);
938 $data = $this->simpleListExampleData();
940 $this->assertFormattedOutputMatches($expected, 'table', $data);
949 // If we provide field labels, then the output will change to reflect that.
950 $formatterOptionsWithFieldLables = new FormatterOptions();
951 $formatterOptionsWithFieldLables
952 ->setFieldLabels(['one' => 'I', 'two' => 'II', 'three' => 'III']);
953 $this->assertFormattedOutputMatches($expected, 'table', $data, $formatterOptionsWithFieldLables);
955 $expectedDrushStyleTable = <<<EOT
961 // If we provide field labels, then the output will change to reflect that.
962 $formatterOptionsWithFieldLables = new FormatterOptions();
963 $formatterOptionsWithFieldLables
964 ->setTableStyle('compact')
965 ->setListDelimiter(':');
966 $this->assertFormattedOutputMatches($expectedDrushStyleTable, 'table', $data, $formatterOptionsWithFieldLables);
969 // Adding an extra field that does not exist in the data set should not change the output
970 $formatterOptionsWithExtraFieldLables = new FormatterOptions();
971 $formatterOptionsWithExtraFieldLables
972 ->setFieldLabels(['one' => 'I', 'two' => 'II', 'three' => 'III', 'four' => 'IV']);
973 $this->assertFormattedOutputMatches($expected, 'table', $data, $formatterOptionsWithExtraFieldLables);
975 $expectedRotated = <<<EOT
976 ------- -------- --------
978 ------- -------- --------
980 ------- -------- --------
982 $this->assertFormattedOutputMatches($expectedRotated, 'table', $data, new FormatterOptions(['list-orientation' => false]));
984 $expectedList = <<< EOT
989 $this->assertFormattedOutputMatches($expectedList, 'list', $data);
991 $expectedReorderedList = <<< EOT
995 $options = new FormatterOptions([FormatterOptions::FIELDS => 'three,one']);
996 $this->assertFormattedOutputMatches($expectedReorderedList, 'list', $data, $options);
998 $expectedCsv = <<< EOT
1002 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
1004 $expectedCsvNoHeaders = 'apple,banana,carrot';
1005 $this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, new FormatterOptions(), ['include-field-labels' => false]);
1007 // Next, configure the formatter options with 'include-field-labels',
1008 // but set --include-field-labels to turn the option back on again.
1009 $options = new FormatterOptions(['include-field-labels' => false]);
1010 $input = new StringInput('test --include-field-labels');
1011 $optionDefinitions = [
1012 new InputArgument('unused', InputArgument::REQUIRED),
1013 new InputOption('include-field-labels', null, InputOption::VALUE_NONE),
1015 $definition = new InputDefinition($optionDefinitions);
1016 $input->bind($definition);
1017 $testValue = $input->getOption('include-field-labels');
1018 $this->assertTrue($testValue);
1019 $hasFieldLabels = $input->hasOption('include-field-labels');
1020 $this->assertTrue($hasFieldLabels);
1022 $this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, $options);
1023 $options->setInput($input);
1024 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data, $options);
1027 protected function associativeListWithRenderer()
1031 'two' => ['banana', 'plantain'],
1032 'three' => 'carrot',
1033 'four' => ['peaches', 'pumpkin pie'],
1035 $list = new PropertyList($data);
1037 $list->addRendererFunction(
1038 function ($key, $cellData, FormatterOptions $options)
1040 if (is_array($cellData)) {
1041 return implode(',', $cellData);
1050 protected function associativeListWithCsvCells()
1054 'two' => ['banana', 'plantain'],
1055 'three' => 'carrot',
1056 'four' => ['peaches', 'pumpkin pie'],
1058 return new PropertyListWithCsvCells($data);
1061 function testPropertyListWithCsvCells()
1063 $this->doPropertyListWithCsvCells($this->associativeListWithRenderer());
1064 $this->doPropertyListWithCsvCells($this->associativeListWithCsvCells());
1067 function doPropertyListWithCsvCells($data)
1070 ------- ---------------------
1074 Four peaches,pumpkin pie
1075 ------- ---------------------
1077 $this->assertFormattedOutputMatches($expected, 'table', $data);
1079 $expectedList = <<< EOT
1085 $this->assertFormattedOutputMatches($expectedList, 'list', $data);
1087 $expectedCsv = <<< EOT
1089 apple,"banana,plantain",carrot,"peaches,pumpkin pie"
1091 $this->assertFormattedOutputMatches($expectedCsv, 'csv', $data);
1093 $expectedCsvNoHeaders = 'apple,"banana,plantain",carrot,"peaches,pumpkin pie"';
1094 $this->assertFormattedOutputMatches($expectedCsvNoHeaders, 'csv', $data, new FormatterOptions(), ['include-field-labels' => false]);
1096 $expectedTsv = <<< EOT
1097 apple\tbanana,plantain\tcarrot\tpeaches,pumpkin pie
1099 $this->assertFormattedOutputMatches($expectedTsv, 'tsv', $data);
1103 function testSimpleListWithFieldLabels()
1105 $data = $this->simpleListExampleData();
1106 $configurationData = new FormatterOptions(
1108 'field-labels' => ['one' => 'Ichi', 'two' => 'Ni', 'three' => 'San'],
1119 $this->assertFormattedOutputMatches($expected, 'table', $data, $configurationData);
1121 $expectedWithReorderedFields = <<<EOT
1127 $this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['three', 'one']]);
1128 $this->assertFormattedOutputMatches($expectedWithReorderedFields, 'table', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
1130 $expectedJson = <<<EOT
1136 $this->assertFormattedOutputMatches($expectedJson, 'json', $data, $configurationData, ['fields' => ['San', 'Ichi']]);
1139 function testSimpleXml()
1142 'name' => 'primary',
1143 'description' => 'The primary colors of the color wheel.',
1153 <?xml version="1.0" encoding="UTF-8"?>
1154 <document name="primary">
1155 <description>The primary colors of the color wheel.</description>
1158 <color>yellow</color>
1164 $this->assertFormattedOutputMatches($expected, 'xml', $data);
1167 function domDocumentData()
1169 $dom = new \DOMDocument('1.0', 'UTF-8');
1171 $document = $dom->createElement('document');
1172 $dom->appendChild($document);
1174 $document->setAttribute('name', 'primary');
1175 $description = $dom->createElement('description');
1176 $document->appendChild($description);
1177 $description->appendChild($dom->createTextNode('The primary colors of the color wheel.'));
1179 $this->domCreateElements($dom, $document, 'color', ['red', 'yellow', 'blue']);
1184 function domCreateElements($dom, $element, $name, $data)
1186 $container = $dom->createElement("{$name}s");
1187 $element->appendChild($container);
1188 foreach ($data as $value) {
1189 $child = $dom->createElement($name);
1190 $container->appendChild($child);
1191 $child->appendChild($dom->createTextNode($value));
1195 function complexDomDocumentData()
1197 $dom = new \DOMDocument('1.0', 'UTF-8');
1199 $document = $dom->createElement('document');
1200 $dom->appendChild($document);
1202 $document->setAttribute('name', 'widget-collection');
1203 $description = $dom->createElement('description');
1204 $document->appendChild($description);
1205 $description->appendChild($dom->createTextNode('A couple of widgets.'));
1207 $widgets = $dom->createElement('widgets');
1208 $document->appendChild($widgets);
1210 $widget = $dom->createElement('widget');
1211 $widgets->appendChild($widget);
1212 $widget->setAttribute('name', 'usual');
1213 $this->domCreateElements($dom, $widget, 'color', ['red', 'yellow', 'blue']);
1214 $this->domCreateElements($dom, $widget, 'shape', ['square', 'circle', 'triangle']);
1216 $widget = $dom->createElement('widget');
1217 $widgets->appendChild($widget);
1218 $widget->setAttribute('name', 'unusual');
1219 $this->domCreateElements($dom, $widget, 'color', ['muave', 'puce', 'umber']);
1220 $this->domCreateElements($dom, $widget, 'shape', ['elipse', 'rhombus', 'trapazoid']);
1225 function domDocumentTestValues()
1228 $expectedXml = <<<EOT
1229 <?xml version="1.0" encoding="UTF-8"?>
1230 <document name="primary">
1231 <description>The primary colors of the color wheel.</description>
1234 <color>yellow</color>
1240 $expectedJson = <<<EOT
1243 "description": "The primary colors of the color wheel.",
1252 $expectedComplexXml = <<<EOT
1253 <?xml version="1.0" encoding="UTF-8"?>
1254 <document name="widget-collection">
1255 <description>A couple of widgets.</description>
1257 <widget name="usual">
1260 <color>yellow</color>
1264 <shape>square</shape>
1265 <shape>circle</shape>
1266 <shape>triangle</shape>
1269 <widget name="unusual">
1271 <color>muave</color>
1273 <color>umber</color>
1276 <shape>elipse</shape>
1277 <shape>rhombus</shape>
1278 <shape>trapazoid</shape>
1285 $expectedComplexJson = <<<EOT
1287 "name": "widget-collection",
1288 "description": "A couple of widgets.",
1322 $this->domDocumentData(),
1327 $this->complexDomDocumentData(),
1328 $expectedComplexXml,
1329 $expectedComplexJson,
1335 * @dataProvider domDocumentTestValues
1337 function testDomData($data, $expectedXml, $expectedJson)
1339 $this->assertFormattedOutputMatches($expectedXml, 'xml', $data);
1340 $this->assertFormattedOutputMatches($expectedJson, 'json', $data);
1342 // Check to see if we get the same xml data if we convert from
1343 // DOM -> array -> DOM.
1344 $expectedJsonAsArray = (array)json_decode($expectedJson);
1345 $this->assertFormattedOutputMatches($expectedXml, 'xml', $expectedJsonAsArray);
1349 * @expectedException \Exception
1350 * @expectedExceptionCode 1
1351 * @expectedExceptionMessage Data provided to Consolidation\OutputFormatters\Formatters\XmlFormatter must be either an instance of DOMDocument or an array. Instead, a string was provided.
1353 function testDataTypeForXmlFormatter()
1355 $this->assertFormattedOutputMatches('Will fail, not return', 'xml', 'Strings cannot be converted to XML');