|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Twig\Extension; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
use Sonata\AdminBundle\Twig\Extension\UnicodeString; |
|
18
|
|
|
use Symfony\Component\String\Exception\InvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Javier Spagnoletti <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
final class UnicodeStringTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testCreateFromStringWithInvalidUtf8Input(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
28
|
|
|
|
|
29
|
|
|
static::createFromString("\xE9"); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testAscii(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$s = static::createFromString('Dieser Wert sollte größer oder gleich'); |
|
|
|
|
|
|
35
|
|
|
$this->assertSame('Dieser Wert sollte grosser oder gleich', (string) $s->ascii()); |
|
|
|
|
|
|
36
|
|
|
$this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII'])); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @dataProvider provideTruncate |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $preserve = false): void |
|
43
|
|
|
{ |
|
44
|
|
|
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $preserve); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$this->assertSame((string) static::createFromString($expected), (string) $instance); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function provideTruncate(): iterable |
|
50
|
|
|
{ |
|
51
|
|
|
yield from [ |
|
52
|
|
|
['', '', 3, ''], |
|
53
|
|
|
['', 'foo', 0, '...'], |
|
54
|
|
|
['foo', 'foo', 0, '...', true], |
|
55
|
|
|
['fo', 'foobar', 2, ''], |
|
56
|
|
|
['foobar', 'foobar', 10, ''], |
|
57
|
|
|
['foobar', 'foobar', 10, '...', true], |
|
58
|
|
|
['foo', 'foo', 3, '...'], |
|
59
|
|
|
['fo', 'foobar', 2, '...'], |
|
60
|
|
|
['...', 'foobar', 3, '...'], |
|
61
|
|
|
['fo...', 'foobar', 5, '...'], |
|
62
|
|
|
['foobar...', 'foobar foo', 6, '...', true], |
|
63
|
|
|
['foobar...', 'foobar foo', 7, '...', true], |
|
64
|
|
|
['foobar foo...', 'foobar foo a', 10, '...', true], |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @dataProvider wordwrapProvider |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testWordwrap(string $expected, string $actual, int $length, string $break, bool $cut = false): void |
|
72
|
|
|
{ |
|
73
|
|
|
$instance = static::createFromString($actual); |
|
|
|
|
|
|
74
|
|
|
$actual = $instance->wordwrap($length, $break, $cut); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$this->assertSame((string) $expected, (string) $actual); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function wordwrapProvider(): iterable |
|
80
|
|
|
{ |
|
81
|
|
|
yield from [ |
|
82
|
|
|
[ |
|
83
|
|
|
'Lo-re-m-Ip-su-m', |
|
84
|
|
|
'Lorem Ipsum', |
|
85
|
|
|
2, |
|
86
|
|
|
'-', |
|
87
|
|
|
true, |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
|
|
'Lorem-Ipsum', |
|
91
|
|
|
'Lorem Ipsum', |
|
92
|
|
|
2, |
|
93
|
|
|
'-', |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
'Lor-em-Ips-um', |
|
97
|
|
|
'Lorem Ipsum', |
|
98
|
|
|
3, |
|
99
|
|
|
'-', |
|
100
|
|
|
true, |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
'L-o-r-e-m-I-p-s-u-m', |
|
104
|
|
|
'Lorem Ipsum', |
|
105
|
|
|
1, |
|
106
|
|
|
'-', |
|
107
|
|
|
true, |
|
108
|
|
|
], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private static function createFromString(string $string): UnicodeString |
|
113
|
|
|
{ |
|
114
|
|
|
return new UnicodeString($string); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.