Passed
Push — master ( 63f627...202fbf )
by Adrien
14:30
created

FormatTest::testSplitSearchTerms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix;
6
7
use Ecodev\Felix\Format;
8
use PHPUnit\Framework\TestCase;
9
10
final class FormatTest extends TestCase
11
{
12
    public function truncateProvider(): array
13
    {
14
        return [
15
            [['abcdef', 100], 'abcdef'],
16
            [['abcdef', 6], 'abcdef'],
17
            [['abcdef', 3], 'ab…'],
18
            [['abcdef', 3, ''], 'abc'],
19
            [['abcdefghi', 5, 'foo'], 'abfoo'],
20
        ];
21
    }
22
23
    /**
24
     * @dataProvider truncateProvider
25
     */
26
    public function testTruncate(array $args, string $expected): void
27
    {
28
        $actual = Format::truncate(...$args);
29
        self::assertSame($expected, $actual);
30
    }
31
32
    /**
33
     * @dataProvider providerSplitSearchTerms
34
     */
35
    public function testSplitSearchTerms(?string $search, array $expected): void
36
    {
37
        self::assertSame($expected, Format::splitSearchTerms($search));
38
    }
39
40
    public function providerSplitSearchTerms(): iterable
41
    {
42
        yield [null, []];
43
        yield 'empty term' => ['', []];
44
        yield 'only whitespace is dropped' => ['  ', []];
45
        yield 'quoted whitespace is kept' => ['"    "', ['    ']];
46
        yield 'empty quoted term' => ['""', []];
47
        yield 'mixed empty term' => ['   ""  ""  ', []];
48
        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 ']];
49
        yield 'normal' => ['foo', ['foo']];
50
        yield 'quoted words are not split' => ['"john doe"', ['john doe']];
51
        yield 'trimmed split words' => [' foo   bar ', ['foo', 'bar']];
52
        yield 'ignore ()' => [' foo   (bar) ', ['foo', 'bar']];
53
        yield 'no duplicates' => ['a a "a" a "a" a', ['a']];
54
        yield 'combined diacritical marks are normalized' => [
55
            // This is a special "é" that is combination of letter "e" and the diacritic "◌́". It can be produced by macOS.
56
            html_entity_decode('e&#769;'),
57
            ['é'], // This is a totally normal "é"
58
        ];
59
        yield 'confusing punctuation marks are ignored, according to https://unicode-table.com/en/sets/punctuation-marks' => [
60
            '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',
61
            ['a'],
62
        ];
63
        yield 'confusing punctuation can still be used if quoted' => [
64
            '"’\'"',
65
            ['’\''],
66
        ];
67
    }
68
}
69