File: AsciiSluggerTest.php

package info (click to toggle)
symfony 6.4.25%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 138,776 kB
  • sloc: php: 1,443,643; xml: 6,601; sh: 605; javascript: 597; makefile: 188; pascal: 71
file content (107 lines) | stat: -rw-r--r-- 3,965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\String;

use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Slugger\AsciiSlugger;

class AsciiSluggerTest extends TestCase
{
    #[\PHPUnit\Framework\Attributes\DataProvider('provideSlugTests')]
    public function testSlug(string $expected, string $string, string $separator = '-', ?string $locale = null)
    {
        $slugger = new AsciiSlugger();

        $this->assertSame($expected, (string) $slugger->slug($string, $separator, $locale));
    }

    public static function provideSlugTests(): iterable
    {
        yield ['', ''];
        yield ['foo', ' foo '];
        yield ['foo-bar', 'foo bar'];

        yield ['foo-bar', 'foo@bar', '-'];
        yield ['foo-at-bar', 'foo@bar', '-', 'en'];

        yield ['e-a', 'é$!à'];
        yield ['e_a', 'é$!à', '_'];

        yield ['a', 'ä'];
        yield ['a', 'ä', '-', 'fr'];
        yield ['ae', 'ä', '-', 'de'];
        yield ['ae', 'ä', '-', 'de_fr']; // Ensure we get the parent locale
        yield [\function_exists('transliterator_transliterate') ? 'g' : '', 'ғ', '-'];
        yield [\function_exists('transliterator_transliterate') ? 'gh' : '', 'ғ', '-', 'uz'];
        yield [\function_exists('transliterator_transliterate') ? 'gh' : '', 'ғ', '-', 'uz_fr']; // Ensure we get the parent locale
    }

    #[\PHPUnit\Framework\Attributes\RequiresPhpExtension('intl')]
    #[\PHPUnit\Framework\Attributes\DataProvider('provideSlugEmojiTests')]
    public function testSlugEmoji(string $expected, string $string, ?string $locale, string|bool $emoji = true)
    {
        $slugger = new AsciiSlugger();
        $slugger = $slugger->withEmoji($emoji);

        $this->assertSame($expected, (string) $slugger->slug($string, '-', $locale));
    }

    public static function provideSlugEmojiTests(): iterable
    {
        yield [
            'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
            'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
            'fr',
        ];
        yield [
            'a-grinning-cat-black-cat-and-a-lion-go-to-national-park-smiling-face-with-heart-eyes-party-popper-yellow-heart',
            'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
            'en',
        ];
        yield [
            'a-smiley-cat-black-cat-and-a-lion-face-go-to-national-park-heart-eyes-tada-yellow-heart',
            'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
            null,
            'slack',
        ];
        yield [
            'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
            'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
            null,
            'github',
        ];
        yield [
            'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
            'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
            'en',
            'github',
        ];
        yield [
            'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
            'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
            'fr_XX', // Fallback on parent locale
        ];
    }

    #[\PHPUnit\Framework\Attributes\RequiresPhpExtension('intl')]
    public function testSlugEmojiWithSetLocale()
    {
        if (!setlocale(\LC_ALL, 'C.UTF-8')) {
            $this->markTestSkipped('Unable to switch to the "C.UTF-8" locale.');
        }

        $slugger = new AsciiSlugger();
        $slugger = $slugger->withEmoji(true);

        $this->assertSame('a-and-a-go-to', (string) $slugger->slug('a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛', '-'));
    }
}