‪TYPO3CMS  11.5
ArrayConverterTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪ArrayConverterTest extends UnitTestCase
30 {
32 
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36  $this->converter = new ‪ArrayConverter();
37  }
38 
42  public function ‪checkMetadata(): void
43  {
44  self::assertEquals(['array', 'string'], $this->converter->getSupportedSourceTypes(), 'Source types do not match');
45  self::assertEquals('array', $this->converter->getSupportedTargetType(), 'Target type does not match');
46  self::assertEquals(10, $this->converter->getPriority(), 'Priority does not match');
47  }
48 
53  {
54  $sourceArray = ['Foo' => 'Bar', 'Baz'];
55  self::assertEquals($sourceArray, $this->converter->convertFrom($sourceArray, 'array'));
56  }
57 
61  public function ‪stringToArrayDataProvider(): array
62  {
63  return [
64  'Empty string to empty array' => ['', []],
65  ];
66  }
67 
75  public function ‪canConvertFromEmptyString(string $source, array $expectedResult): void
76  {
77  self::assertEquals($expectedResult, $this->converter->convertFrom($source, 'array'));
78  }
79 
84  {
85  return [
86  'Sentence with two spaces string converts to array, delimiter is space' => ['foo bar', $this->‪getStringConf(' ', true), ['foo', 'bar']],
87  'String List with comma converts to array, delimiter: ,' => ['foo,bar', $this->‪getStringConf(','), ['foo', 'bar']],
88  'String List with comma converts to array; no empty: true, delimiter: ,' => ['foo,,bar', $this->‪getStringConf(',', true), ['foo', 'bar']],
89  'Long sentence splits into two parts; delimiter: space' => ['Foo bar is a long sentence', $this->‪getStringConf(' ', false, 2), ['Foo', 'bar is a long sentence']],
90  'All available configuration options' => ['This. Is. Awesome... Right?', $this->‪getStringConf('.', true, 4), ['This', 'Is', 'Awesome', 'Right?']],
91  ];
92  }
93 
94  private function ‪getStringConf(?string $delimiter = null, ?bool $removeEmptyValues = null, ?int $limit = null): ‪PropertyMappingConfigurationInterface
95  {
96  $configuration = new ‪PropertyMappingConfiguration();
97  if ($delimiter) {
98  $configuration->setTypeConverterOption(ArrayConverter::class, 'delimiter', $delimiter);
99  }
100  if ($removeEmptyValues) {
101  $configuration->setTypeConverterOption(ArrayConverter::class, 'removeEmptyValues', $removeEmptyValues);
102  }
103  if ($limit) {
104  $configuration->setTypeConverterOption(ArrayConverter::class, 'limit', $limit);
105  }
106  $configuration->setTypeConverterOptions(ArrayConverter::class, [
107  'delimiter' => $delimiter,
108  'removeEmptyValues' => $removeEmptyValues,
109  'limit' => $limit,
110  ]);
111  return $configuration;
112  }
113 
122  public function ‪canConvertWithConfigurationFromString(string $source, ‪PropertyMappingConfigurationInterface $configuration, array $expectedResult): void
123  {
124  self::assertEquals($expectedResult, $this->converter->convertFrom($source, 'array', [], $configuration));
125  }
126 
130  public function ‪canConvertFromDataProvider(): array
131  {
132  return [
133  'Can convert empty string' => ['', true],
134  'Can convert not empty string' => ['foo', true],
135  'Can not convert number' => [12, false],
136  'Can convert array' => [['foo'], true],
137  ];
138  }
139 
147  public function ‪canConvertFromReturnsCorrectBooleans($source, bool $expectedResult): void
148  {
149  self::assertSame($expectedResult, $this->converter->canConvertFrom($source, 'array'));
150  }
151 
156  {
157  $this->expectException(TypeConverterException::class);
158  $this->expectExceptionCode(1582877555);
159  $this->converter->convertFrom('foo', 'array', [], new ‪PropertyMappingConfiguration());
160  }
161 
166  {
167  $result = $this->converter->convertFrom('foo', 'array', [], null);
168  self::assertSame('foo', $result);
169  }
170 }
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\returnsSourceUnchangedIfNonEmptyValueWithNoConfigurationIsGiven
‪returnsSourceUnchangedIfNonEmptyValueWithNoConfigurationIsGiven()
Definition: ArrayConverterTest.php:165
‪TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
Definition: TypeConverterException.php:25
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\checkMetadata
‪checkMetadata()
Definition: ArrayConverterTest.php:42
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\canConvertFromReturnsCorrectBooleans
‪canConvertFromReturnsCorrectBooleans($source, bool $expectedResult)
Definition: ArrayConverterTest.php:147
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\throwsTypeConverterExceptionIfDelimiterIsNotGiven
‪throwsTypeConverterExceptionIfDelimiterIsNotGiven()
Definition: ArrayConverterTest.php:155
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\convertFromDoesNotModifyTheSourceArray
‪convertFromDoesNotModifyTheSourceArray()
Definition: ArrayConverterTest.php:52
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\setUp
‪setUp()
Definition: ArrayConverterTest.php:33
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter
Definition: ArrayConverterTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\canConvertFromEmptyString
‪canConvertFromEmptyString(string $source, array $expectedResult)
Definition: ArrayConverterTest.php:75
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\canConvertWithConfigurationFromString
‪canConvertWithConfigurationFromString(string $source, PropertyMappingConfigurationInterface $configuration, array $expectedResult)
Definition: ArrayConverterTest.php:122
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface
Definition: PropertyMappingConfigurationInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\stringToArrayWithConfigurationDataProvider
‪array stringToArrayWithConfigurationDataProvider()
Definition: ArrayConverterTest.php:83
‪TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
Definition: PropertyMappingConfiguration.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\getStringConf
‪getStringConf(?string $delimiter=null, ?bool $removeEmptyValues=null, ?int $limit=null)
Definition: ArrayConverterTest.php:94
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\canConvertFromDataProvider
‪array canConvertFromDataProvider()
Definition: ArrayConverterTest.php:130
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\$converter
‪ArrayConverter $converter
Definition: ArrayConverterTest.php:31
‪TYPO3\CMS\Extbase\Property\TypeConverter\ArrayConverter
Definition: ArrayConverter.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest\stringToArrayDataProvider
‪array stringToArrayDataProvider()
Definition: ArrayConverterTest.php:61
‪TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter\ArrayConverterTest
Definition: ArrayConverterTest.php:30