‪TYPO3CMS  ‪main
StringFragmentSplitterTest.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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
27 final class ‪StringFragmentSplitterTest extends UnitTestCase
28 {
29  public static function ‪stringIsSplitDataProvider(): \Generator
30  {
31  $expressionPattern = new ‪StringFragmentPattern(‪StringFragmentSplitter::TYPE_EXPRESSION, '\\$[^$]+\\$');
33 
34  yield 'Hello World! (unmatched with `raw` only)' => [
35  'Hello World!',
36  [$expressionPattern, $otherPattern],
37  0,
38  [
39  ‪StringFragment::raw('Hello World!'),
40  ],
41  ];
42  yield 'Hello World! (unmatched as null)' => [
43  'Hello World!',
44  [$expressionPattern, $otherPattern],
46  null,
47  ];
48  yield 'Hello$expr$World!' => [
49  'Hello$expr$World!',
50  [$expressionPattern, $otherPattern],
51  0,
52  [
53  ‪StringFragment::raw('Hello'),
55  ‪StringFragment::raw('World!'),
56  ],
57  ];
58  yield 'Hello $expr$ World!' => [
59  'Hello $expr$ World!',
60  [$expressionPattern, $otherPattern],
61  0,
62  [
63  ‪StringFragment::raw('Hello '),
65  ‪StringFragment::raw(' World!'),
66  ],
67  ];
68  yield '$expr$ combined with !other!' => [
69  '$expr$ combined with !other!',
70  [$expressionPattern, $otherPattern],
71  0,
72  [
74  ‪StringFragment::raw(' combined with '),
76  ],
77  ];
78  }
79 
84  #[DataProvider('stringIsSplitDataProvider')]
85  #[Test]
86  public function ‪stringIsSplit(string $value, array $patterns, int $flags, ?array $expectations): void
87  {
88  $splitter = new ‪StringFragmentSplitter(...$patterns);
89  $collection = $splitter->split($value, $flags);
90 
91  if ($expectations === null) {
92  self::assertNull($collection);
93  } else {
94  self::assertEquals($expectations, $collection->getFragments());
95  }
96  }
97 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\String
Definition: StringFragmentCollectionTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Utility\String\StringFragmentSplitterTest\stringIsSplitDataProvider
‪static stringIsSplitDataProvider()
Definition: StringFragmentSplitterTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Utility\String\StringFragmentSplitterTest
Definition: StringFragmentSplitterTest.php:28
‪TYPO3\CMS\Core\Utility\String\StringFragment\expression
‪static expression(string $value)
Definition: StringFragment.php:33
‪TYPO3\CMS\Core\Tests\Unit\Utility\String\StringFragmentSplitterTest\stringIsSplit
‪stringIsSplit(string $value, array $patterns, int $flags, ?array $expectations)
Definition: StringFragmentSplitterTest.php:86
‪TYPO3\CMS\Core\Utility\String\StringFragment\raw
‪static raw(string $value)
Definition: StringFragment.php:28
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter
Definition: StringFragmentSplitter.php:27
‪TYPO3\CMS\Core\Utility\String\StringFragmentPattern
Definition: StringFragmentPattern.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\TYPE_EXPRESSION
‪const TYPE_EXPRESSION
Definition: StringFragmentSplitter.php:36
‪TYPO3\CMS\Core\Utility\String\StringFragment
Definition: StringFragment.php:24
‪TYPO3\CMS\Core\Utility\String\StringFragmentSplitter\FLAG_UNMATCHED_AS_NULL
‪const FLAG_UNMATCHED_AS_NULL
Definition: StringFragmentSplitter.php:42