1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Context\Encoder;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
class CsvEncoderContextBuilderTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;
private CsvEncoderContextBuilder $contextBuilder;
protected function setUp(): void
{
$this->contextBuilder = new CsvEncoderContextBuilder();
}
/**
* @param array<string, mixed> $values
*/
#[\PHPUnit\Framework\Attributes\DataProvider('withersDataProvider')]
public function testWithers(array $values)
{
$context = $this->contextBuilder
->withDelimiter($values[CsvEncoder::DELIMITER_KEY])
->withEnclosure($values[CsvEncoder::ENCLOSURE_KEY])
->withKeySeparator($values[CsvEncoder::KEY_SEPARATOR_KEY])
->withHeaders($values[CsvEncoder::HEADERS_KEY])
->withEscapedFormulas($values[CsvEncoder::ESCAPE_FORMULAS_KEY])
->withAsCollection($values[CsvEncoder::AS_COLLECTION_KEY])
->withNoHeaders($values[CsvEncoder::NO_HEADERS_KEY])
->withEndOfLine($values[CsvEncoder::END_OF_LINE])
->withOutputUtf8Bom($values[CsvEncoder::OUTPUT_UTF8_BOM_KEY])
->toArray();
$this->assertSame($values, $context);
}
/**
* @return iterable<array{0: array<string, mixed>|}>
*/
public static function withersDataProvider(): iterable
{
yield 'With values' => [[
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => '"',
CsvEncoder::KEY_SEPARATOR_KEY => '_',
CsvEncoder::HEADERS_KEY => ['h1', 'h2'],
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
CsvEncoder::AS_COLLECTION_KEY => true,
CsvEncoder::NO_HEADERS_KEY => false,
CsvEncoder::END_OF_LINE => 'EOL',
CsvEncoder::OUTPUT_UTF8_BOM_KEY => false,
]];
yield 'With null values' => [[
CsvEncoder::DELIMITER_KEY => null,
CsvEncoder::ENCLOSURE_KEY => null,
CsvEncoder::KEY_SEPARATOR_KEY => null,
CsvEncoder::HEADERS_KEY => null,
CsvEncoder::ESCAPE_FORMULAS_KEY => null,
CsvEncoder::AS_COLLECTION_KEY => null,
CsvEncoder::NO_HEADERS_KEY => null,
CsvEncoder::END_OF_LINE => null,
CsvEncoder::OUTPUT_UTF8_BOM_KEY => null,
]];
}
public function testWithersWithoutValue()
{
$context = $this->contextBuilder
->withDelimiter(null)
->withEnclosure(null)
->withKeySeparator(null)
->withHeaders(null)
->withEscapedFormulas(null)
->withAsCollection(null)
->withNoHeaders(null)
->withEndOfLine(null)
->withOutputUtf8Bom(null)
->toArray();
$this->assertSame([
CsvEncoder::DELIMITER_KEY => null,
CsvEncoder::ENCLOSURE_KEY => null,
CsvEncoder::KEY_SEPARATOR_KEY => null,
CsvEncoder::HEADERS_KEY => null,
CsvEncoder::ESCAPE_FORMULAS_KEY => null,
CsvEncoder::AS_COLLECTION_KEY => null,
CsvEncoder::NO_HEADERS_KEY => null,
CsvEncoder::END_OF_LINE => null,
CsvEncoder::OUTPUT_UTF8_BOM_KEY => null,
], $context);
}
public function testCannotSetMultipleBytesAsDelimiter()
{
$this->expectException(InvalidArgumentException::class);
$this->contextBuilder->withDelimiter('ọ');
}
public function testCannotSetMultipleBytesAsEnclosure()
{
$this->expectException(InvalidArgumentException::class);
$this->contextBuilder->withEnclosure('ọ');
}
#[\PHPUnit\Framework\Attributes\Group('legacy')]
public function testCannotSetMultipleBytesAsEscapeChar()
{
$this->expectUserDeprecationMessage('Since symfony/serializer 7.2: The "Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder::withEscapeChar" method is deprecated. It will be removed in 8.0.');
$this->expectException(InvalidArgumentException::class);
$this->contextBuilder->withEscapeChar('ọ');
}
#[\PHPUnit\Framework\Attributes\Group('legacy')]
public function testWithEscapeCharIsDeprecated()
{
$this->expectUserDeprecationMessage('Since symfony/serializer 7.2: The "Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder::withEscapeChar" method is deprecated. It will be removed in 8.0.');
$context = $this->contextBuilder->withEscapeChar('\\');
$this->assertSame(['csv_escape_char' => '\\'], $context->toArray());
}
}
|