1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix; |
6
|
|
|
|
7
|
|
|
use Ecodev\Felix\Format; |
8
|
|
|
use Money\Money; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class FormatTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public static function providerTruncate(): array |
14
|
|
|
{ |
15
|
|
|
return [ |
16
|
|
|
[['abcdef', 100], 'abcdef'], |
17
|
|
|
[['abcdef', 6], 'abcdef'], |
18
|
|
|
[['abcdef', 3], 'ab…'], |
19
|
|
|
[['abcdef', 3, ''], 'abc'], |
20
|
|
|
[['abcdefghi', 5, 'foo'], 'abfoo'], |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @dataProvider providerTruncate |
26
|
|
|
*/ |
27
|
|
|
public function testTruncate(array $args, string $expected): void |
28
|
|
|
{ |
29
|
|
|
$actual = Format::truncate(...$args); |
30
|
|
|
self::assertSame($expected, $actual); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @dataProvider providerSplitSearchTerms |
35
|
|
|
*/ |
36
|
|
|
public function testSplitSearchTerms(?string $search, array $expected): void |
37
|
|
|
{ |
38
|
|
|
self::assertSame($expected, Format::splitSearchTerms($search)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function providerSplitSearchTerms(): iterable |
42
|
|
|
{ |
43
|
|
|
yield [null, []]; |
44
|
|
|
yield 'empty term' => ['', []]; |
45
|
|
|
yield 'only whitespace is dropped' => [' ', []]; |
46
|
|
|
yield 'quoted whitespace is kept' => ['" "', [' ']]; |
47
|
|
|
yield 'empty quoted term' => ['""', []]; |
48
|
|
|
yield 'mixed empty term' => [' "" "" ', []]; |
49
|
|
|
yield 'mixed quoted and non-quoted' => [' a b "john doe" c d e " f g h i j k l m n o p q r s t u v w x y z " ', ['a', 'b', 'c', 'd', 'e', 'john doe', ' f g h i j k l m n o p q r s t u v w x y z ']]; |
50
|
|
|
yield 'normal' => ['foo', ['foo']]; |
51
|
|
|
yield 'quoted words are not split' => ['"john doe"', ['john doe']]; |
52
|
|
|
yield 'trimmed split words' => [' foo bar ', ['foo', 'bar']]; |
53
|
|
|
yield 'ignore ()' => [' foo (bar) ', ['foo', 'bar']]; |
54
|
|
|
yield 'no duplicates' => ['a a "a" a "a" a', ['a']]; |
55
|
|
|
yield 'combined diacritical marks are normalized' => [ |
56
|
|
|
// This is a special "é" that is combination of letter "e" and the diacritic "◌́". It can be produced by macOS. |
57
|
|
|
html_entity_decode('é'), |
58
|
|
|
['é'], // This is a totally normal "é" |
59
|
|
|
]; |
60
|
|
|
yield 'confusing punctuation marks are ignored, according to https://unicode-table.com/en/sets/punctuation-marks' => [ |
61
|
|
|
'a\'.a।a։a。a۔a⳹a܁a።a᙮a᠃a⳾a꓿a꘎a꛳a࠽a᭟a,a،a、a՝a߸a፣a᠈a꓾a꘍a꛵a᭞a⁇a⁉a⁈a‽a❗a‼a⸘a?a;a¿a؟a՞a܆a፧a⳺a⳻a꘏a꛷a𑅃a꫱a!a¡a߹a᥄a·a𐎟a𐏐a𒑰a፡a a𐤟a࠰a—a–a‒a‐a⁃a﹣a-a֊a᠆a;a·a؛a፤a꛶a․a:a፥a꛴a᭝a…a︙aຯa«a‹a»a›a„a‚a“a‟a‘a‛a”a’a"a', |
62
|
|
|
['a'], |
63
|
|
|
]; |
64
|
|
|
yield 'confusing punctuation can still be used if quoted' => [ |
65
|
|
|
'"’\'"', |
66
|
|
|
['’\''], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testMoney(): void |
71
|
|
|
{ |
72
|
|
|
self::assertSame('12.34', Format::money(Money::CHF(1234))); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|