Skip to content

Commit

Permalink
Merge branch '4.3' into 4.4
Browse files Browse the repository at this point in the history
* 4.3:
  Fix tests
  [Console] Fix commands description with numeric namespaces
  [HttpFoundation] Fixed typo
  [EventDispatcher] Better error reporting when arguments to dispatch() are swapped
  [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used in constructor
  [Form] Keep preferred_choices order for choice groups
  [Debug] work around failing chdir() on Darwin
  [PhpUnitBridge] Read configuration CLI directive
  [DI] Missing test on YamlFileLoader
  Revert "minor #34608 [Process] add tests for php executable finder if file does not exist (ahmedash95)"
  Simpler example for Apache basic auth workaround
  [Console] Fix trying to access array offset on value of type int
  [Config] Remove extra sprintf arg
  [VarDumper] notice on potential undefined index
  [HttpClient] turn exception into log when the request has no content-type
  [Process] add tests for php executable finder if file does not exist
  [Cache] Make sure we get the correct number of values from redis::mget()
  [TwigBridge] Add row_attr to all form themes
  [Serializer] Fix MetadataAwareNameConverter usage with string group
  • Loading branch information
nicolas-grekas committed Nov 28, 2019
2 parents c0cce11 + 12ce178 commit 842637b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
self::HEADERS_KEY => [],
self::KEY_SEPARATOR_KEY => '.',
self::NO_HEADERS_KEY => false,
self::AS_COLLECTION_KEY => false,
self::OUTPUT_UTF8_BOM_KEY => false,
];

Expand Down Expand Up @@ -106,7 +107,7 @@ public function encode($data, $format, array $context = [])

$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));

if (!($context[self::NO_HEADERS_KEY] ?? false)) {
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
}

Expand Down Expand Up @@ -164,7 +165,7 @@ public function decode($data, $format, array $context = [])
if (null === $headers) {
$nbHeaders = $nbCols;

if ($context[self::NO_HEADERS_KEY] ?? false) {
if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
for ($i = 0; $i < $nbCols; ++$i) {
$headers[] = [$i];
}
Expand Down Expand Up @@ -204,7 +205,7 @@ public function decode($data, $format, array $context = [])
}
fclose($handle);

if ($context[self::AS_COLLECTION_KEY] ?? false) {
if ($context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY]) {
return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion NameConverter/MetadataAwareNameConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function getCacheValueForAttributesMetadata(string $class, array $contex
if (!$groups && ($context[AbstractNormalizer::GROUPS] ?? [])) {
continue;
}
if ($groups && !array_intersect($groups, $context[AbstractNormalizer::GROUPS] ?? [])) {
if ($groups && !array_intersect($groups, (array) ($context[AbstractNormalizer::GROUPS] ?? []))) {
continue;
}

Expand Down
53 changes: 53 additions & 0 deletions Tests/Encoder/CsvEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ public function testEncodeCustomSettingsPassedInContext()
]));
}

public function testEncodeCustomSettingsPassedInConstructor()
{
$encoder = new CsvEncoder([
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
]);
$value = ['a' => 'he\'llo', 'c' => ['d' => 'foo']];

$this->assertSame(<<<'CSV'
a;c-d
'he''llo';foo

CSV
, $encoder->encode($value, 'csv'));
}

public function testEncodeEmptyArray()
{
$this->assertEquals("\n\n", $this->encoder->encode([], 'csv'));
Expand Down Expand Up @@ -374,6 +392,15 @@ public function testEncodeWithoutHeader()
, $this->encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
$this->assertSame(<<<'CSV'
a,b
c,d

CSV
, $encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
}

public function testEncodeArrayObject()
Expand Down Expand Up @@ -562,6 +589,23 @@ public function testDecodeCustomSettingsPassedInContext()
]));
}

public function testDecodeCustomSettingsPassedInConstructor()
{
$encoder = new CsvEncoder([
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]);
$expected = [['a' => 'hell\'o', 'bar' => ['baz' => 'b']]];
$this->assertEquals($expected, $encoder->decode(<<<'CSV'
a;bar-baz
'hell''o';b;c
CSV
, 'csv'));
}

public function testDecodeMalformedCollection()
{
$expected = [
Expand Down Expand Up @@ -591,6 +635,15 @@ public function testDecodeWithoutHeader()
a,b
c,d

CSV
, 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
$this->assertEquals([['a', 'b'], ['c', 'd']], $encoder->decode(<<<'CSV'
a,b
c,d

CSV
, 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
Expand Down
2 changes: 2 additions & 0 deletions Tests/NameConverter/MetadataAwareNameConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ public function attributeAndContextProvider()
return [
['buz', 'buz', ['groups' => ['a']]],
['buzForExport', 'buz', ['groups' => ['b']]],
['buz', 'buz', ['groups' => 'a']],
['buzForExport', 'buz', ['groups' => 'b']],
['buz', 'buz', ['groups' => ['c']]],
['buz', 'buz', []],
];
Expand Down

0 comments on commit 842637b

Please sign in to comment.