From 8ec5dcbc89c3fb5e1f6325c7dbeebbcf897143ba Mon Sep 17 00:00:00 2001 From: aegypius Date: Sun, 7 Jan 2024 07:28:29 +0000 Subject: [PATCH] test(parametervalidator): add tests for value extractor --- .../ParameterValueExtractor.php | 2 +- .../Tests/ParameterValueExtractorTest.php | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/ParameterValidator/ParameterValueExtractor.php b/src/ParameterValidator/ParameterValueExtractor.php index ad64c6ab21..3ca727c18c 100644 --- a/src/ParameterValidator/ParameterValueExtractor.php +++ b/src/ParameterValidator/ParameterValueExtractor.php @@ -46,7 +46,7 @@ public static function getSeparator(string $collectionFormat): string return match ($collectionFormat) { 'csv' => ',', 'ssv' => ' ', - 'tsv' => '\t', + 'tsv' => "\t", 'pipes' => '|', default => throw new \InvalidArgumentException(sprintf('Unknown collection format %s', $collectionFormat)), }; diff --git a/src/ParameterValidator/Tests/ParameterValueExtractorTest.php b/src/ParameterValidator/Tests/ParameterValueExtractorTest.php index 310c80c432..5cabfc604a 100644 --- a/src/ParameterValidator/Tests/ParameterValueExtractorTest.php +++ b/src/ParameterValidator/Tests/ParameterValueExtractorTest.php @@ -87,4 +87,45 @@ public function provideGetSeparatorCases(): iterable ]; } } + + /** + * @dataProvider provideGetValueCases + * + * @param int[]|string[] $expectedValue + * @param int|int[]|string|string[] $value + */ + public function testGetValue(array $expectedValue, int|string|array $value, string $collectionFormat): void + { + self::assertSame($expectedValue, ParameterValueExtractor::getValue($value, $collectionFormat)); + } + + /** + * @return iterable + */ + public function provideGetValueCases(): iterable + { + yield 'empty input' => [ + [], [], 'csv', + ]; + + yield 'comma separated value' => [ + ['foo', 'bar'], 'foo,bar', 'csv', + ]; + + yield 'space separated value' => [ + ['foo', 'bar'], 'foo bar', 'ssv', + ]; + + yield 'tab separated value' => [ + ['foo', 'bar'], "foo\tbar", 'tsv', + ]; + + yield 'pipe separated value' => [ + ['foo', 'bar'], 'foo|bar', 'pipes', + ]; + + yield 'array values' => [ + ['foo', 'bar'], ['foo', 'bar'], 'csv', + ]; + } }