Passed
Pull Request — master (#17)
by Adrien
13:27
created

FormatTest::providerSplitSearchTerms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 26
rs 9.584
cc 1
nc 1
nop 0
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('e&#769;'),
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