‪TYPO3CMS  ‪main
TextCropperTest.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;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪TextCropperTest extends UnitTestCase
26 {
28 
29  protected function ‪setUp(): void
30  {
31  parent::setUp();
32  $this->subject = new ‪TextCropper();
33  }
34 
35  #[Test]
36  public function ‪cropIsMultibyteSafe(): void
37  {
38  $actual = $this->subject->crop(
39  content: 'бла',
40  numberOfChars: 3,
41  replacementForEllipsis: '...',
42  cropToSpace: false
43  );
44  self::assertEquals('бла', $actual);
45  }
46 
47  public static function ‪cropWorksDataProvicer(): \Generator
48  {
49  $plainText = 'Kasper Sk' . chr(229) . 'rh' . chr(248)
50  . 'j implemented the original version of the crop function.';
51  $textWithLinebreaks = "Lorem ipsum dolor sit amet,\n"
52  . "consetetur sadipscing elitr,\n"
53  . 'sed diam nonumy eirmod tempor invidunt ut labore e'
54  . 't dolore magna aliquyam';
55 
56  yield 'plain text; 11|...|0' => [
57  'expected' => 'Kasper Sk' . chr(229) . 'r...',
58  'content' => $plainText,
59  'numberOfChars' => 11,
60  'replacementForEllipsis' => '...',
61  'cropToSpace' => false,
62  ];
63 
64  yield 'plain text; -58|...|0' => [
65  'expected' => '...h' . chr(248) . 'j implemented the original version of the crop function.',
66  'content' => $plainText,
67  'numberOfChars' => -58,
68  'replacementForEllipsis' => '...',
69  'cropToSpace' => false,
70  ];
71 
72  yield 'plain text; 4|...|1' => [
73  'expected' => 'Kasp...',
74  'content' => $plainText,
75  'numberOfChars' => 4,
76  'replacementForEllipsis' => '...',
77  'cropToSpace' => true,
78  ];
79 
80  yield 'plain text; 20|...|1' => [
81  'expected' => 'Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j...',
82  'content' => $plainText,
83  'numberOfChars' => 20,
84  'replacementForEllipsis' => '...',
85  'cropToSpace' => true,
86  ];
87 
88  yield 'plain text; -5|...|1' => [
89  'expected' => '...tion.',
90  'content' => $plainText,
91  'numberOfChars' => -5,
92  'replacementForEllipsis' => '...',
93  'cropToSpace' => true,
94  ];
95 
96  yield 'plain text; -49|...|1' => [
97  'expected' => '... the original version of the crop function.',
98  'content' => $plainText,
99  'numberOfChars' => -49,
100  'replacementForEllipsis' => '...',
101  'cropToSpace' => true,
102  ];
103 
104  // text with linebreaks
105  yield 'text with linebreaks' => [
106  'expected' => "Lorem ipsum dolor sit amet,\nconsetetur sadipscing elitr,\ns"
107  . 'ed diam nonumy eirmod tempor invidunt ut labore e'
108  . 't dolore magna',
109  'content' => $textWithLinebreaks,
110  'numberOfChars' => 121,
111  'replacementForEllipsis' => '',
112  'cropToSpace' => false,
113  ];
114  }
115 
116  #[DataProvider('cropWorksDataProvicer')]
117  #[Test]
118  public function ‪cropWorks(string $expected, string $content, int $numberOfChars, string $replacementForEllipsis, bool $cropToSpace): void
119  {
120  $this->‪handleCharset($content, $expected);
121  self::assertEquals($expected, $this->subject->crop(
122  content: $content,
123  numberOfChars: $numberOfChars,
124  replacementForEllipsis: $replacementForEllipsis,
125  cropToSpace: $cropToSpace
126  ));
127  }
128 
135  protected function ‪handleCharset(string &‪$subject, string &$expected): void
136  {
137  ‪$subject = mb_convert_encoding(‪$subject, 'utf-8', 'iso-8859-1');
138  $expected = mb_convert_encoding($expected, 'utf-8', 'iso-8859-1');
139  }
140 }
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\handleCharset
‪handleCharset(string &$subject, string &$expected)
Definition: TextCropperTest.php:135
‪TYPO3\CMS\Core\Tests\Unit\Text
Definition: TextCropperTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\setUp
‪setUp()
Definition: TextCropperTest.php:29
‪TYPO3\CMS\Core\Text\TextCropper
Definition: TextCropper.php:21
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\cropWorksDataProvicer
‪static cropWorksDataProvicer()
Definition: TextCropperTest.php:47
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\cropIsMultibyteSafe
‪cropIsMultibyteSafe()
Definition: TextCropperTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\$subject
‪TextCropper $subject
Definition: TextCropperTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest
Definition: TextCropperTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Text\TextCropperTest\cropWorks
‪cropWorks(string $expected, string $content, int $numberOfChars, string $replacementForEllipsis, bool $cropToSpace)
Definition: TextCropperTest.php:118