|
1 | | -<?php |
| 1 | +<?php declare(strict_types=1); |
2 | 2 |
|
3 | 3 | namespace Sunrise\Slugger\Tests; |
4 | 4 |
|
| 5 | +/** |
| 6 | + * Import classes |
| 7 | + */ |
5 | 8 | use PHPUnit\Framework\TestCase; |
| 9 | +use Sunrise\Slugger\Exception\Exception; |
6 | 10 | use Sunrise\Slugger\Exception\UnableToCreateTransliteratorException; |
7 | 11 | use Sunrise\Slugger\Exception\UnableToTransliterateException; |
8 | | -use Sunrise\Slugger\Exception\UnsupportedTransliteratorIdentifierException; |
9 | 12 | use Sunrise\Slugger\Slugger; |
10 | 13 | use Sunrise\Slugger\SluggerInterface; |
| 14 | +use RuntimeException; |
11 | 15 |
|
| 16 | +/** |
| 17 | + * SluggerTest |
| 18 | + */ |
12 | 19 | class SluggerTest extends TestCase |
13 | 20 | { |
14 | | - private const RUSSIAN_LATIN_TRANSLITERATOR_ID = 'Russian-Latin/BGN'; |
15 | | - private const CYRILLIC_LATIN_TRANSLITERATOR_ID = 'Cyrillic-Latin'; |
16 | 21 |
|
17 | | - public function testConstructor() |
18 | | - { |
19 | | - $slugger = new Slugger(); |
20 | | - |
21 | | - $this->assertInstanceOf(SluggerInterface::class, $slugger); |
22 | | - } |
23 | | - |
24 | | - public function testTransliteratorId() |
25 | | - { |
26 | | - $slugger = new Slugger(); |
27 | | - |
28 | | - $slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID); |
29 | | - $this->assertEquals(self::RUSSIAN_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId()); |
30 | | - |
31 | | - $slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID); |
32 | | - $this->assertEquals(self::CYRILLIC_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId()); |
33 | | - } |
34 | | - |
35 | | - public function testDefaultTransliteratorId() |
36 | | - { |
37 | | - $slugger = new Slugger(); |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private const RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID = 'Russian-Latin/BGN'; |
38 | 26 |
|
39 | | - $this->assertEquals(self::RUSSIAN_LATIN_TRANSLITERATOR_ID, $slugger->getTransliteratorId()); |
40 | | - } |
41 | | - |
42 | | - public function testUnsupportedTransliterationId() |
43 | | - { |
44 | | - $this->expectException(UnsupportedTransliteratorIdentifierException::class); |
45 | | - $this->expectExceptionMessage('The transliterator ID "Morrowind-Oblivion/KFC" is not supported'); |
46 | | - |
47 | | - $slugger = new Slugger(); |
48 | | - |
49 | | - $slugger->setTransliteratorId('Morrowind-Oblivion/KFC'); |
50 | | - } |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + private const CYRILLIC_LATIN_TRANSLITERATOR_BASIC_ID = 'Cyrillic-Latin'; |
51 | 31 |
|
52 | | - public function testSupportedTransliteratorIds() |
| 32 | + /** |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + public function testConstructor() : void |
53 | 36 | { |
54 | 37 | $slugger = new Slugger(); |
55 | 38 |
|
56 | | - $this->assertEquals(\transliterator_list_ids(), $slugger->getSupportedTransliteratorIds()); |
57 | | - } |
58 | | - |
59 | | - public function testTransliterateRussianLatin() |
60 | | - { |
61 | | - $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
62 | | - $output = 'syesh yeshche etikh myagkikh frantsuzskikh bulok da vypey chayu'; |
63 | | - $compound = 'Any-Latin; Latin-ASCII; [^\x20\x41-\x5A\x61-\x7A] Remove'; |
64 | | - |
65 | | - $slugger = new Slugger(); |
66 | | - $slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID); |
67 | | - |
68 | | - $this->assertEquals($output, $slugger->transliterate($input, $compound)); |
| 39 | + $this->assertInstanceOf(SluggerInterface::class, $slugger); |
69 | 40 | } |
70 | 41 |
|
71 | | - public function testTransliterateCyrillicLatin() |
| 42 | + /** |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function testConstructorWithUnsupportedTransliteratorBasicId() : void |
72 | 46 | { |
73 | | - $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
74 | | - $output = 'ses ese etih magkih francuzskih bulok da vypej cau'; |
75 | | - $compound = 'Any-Latin; Latin-ASCII; [^\x20\x41-\x5A\x61-\x7A] Remove'; |
76 | | - |
77 | | - $slugger = new Slugger(); |
78 | | - $slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID); |
| 47 | + $this->expectException(UnableToCreateTransliteratorException::class); |
| 48 | + $this->expectExceptionMessage('Unable to create transliterator'); |
79 | 49 |
|
80 | | - $this->assertEquals($output, $slugger->transliterate($input, $compound)); |
| 50 | + new Slugger('Morrowind-Oblivion/KFC'); |
81 | 51 | } |
82 | 52 |
|
83 | | - public function testSlugifyRussianLatin() |
| 53 | + /** |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + public function testSlugify() : void |
84 | 57 | { |
85 | 58 | $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
86 | 59 | $output = 'syesh-yeshche-etikh-myagkikh-frantsuzskikh-bulok-da-vypey-chayu'; |
87 | | - |
88 | 60 | $slugger = new Slugger(); |
89 | 61 |
|
90 | | - $slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID); |
91 | | - |
92 | 62 | $this->assertEquals($output, $slugger->slugify($input)); |
93 | 63 | } |
94 | 64 |
|
95 | | - public function testSlugifyCyrillicLatin() |
| 65 | + /** |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testSlugifyWithNumbers() : void |
96 | 69 | { |
97 | | - $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
98 | | - $output = 'ses-ese-etih-magkih-francuzskih-bulok-da-vypej-cau'; |
99 | | - |
| 70 | + $input = '0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz'; |
| 71 | + $output = '0123456789-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz'; |
100 | 72 | $slugger = new Slugger(); |
101 | 73 |
|
102 | | - $slugger->setTransliteratorId(self::CYRILLIC_LATIN_TRANSLITERATOR_ID); |
103 | | - |
104 | 74 | $this->assertEquals($output, $slugger->slugify($input)); |
105 | 75 | } |
106 | 76 |
|
107 | | - public function testSlugifyWithDelimiter() |
| 77 | + /** |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testSlugifyWithDelimiter() : void |
108 | 81 | { |
109 | 82 | $input = ' А Б В '; |
110 | 83 | $output = 'a_b_v'; |
111 | | - |
112 | 84 | $slugger = new Slugger(); |
113 | 85 |
|
114 | | - $slugger->setTransliteratorId(self::RUSSIAN_LATIN_TRANSLITERATOR_ID); |
115 | | - |
116 | 86 | $this->assertEquals($output, $slugger->slugify($input, '_')); |
117 | 87 | } |
118 | 88 |
|
119 | | - public function testSlugifyWithNumbers() |
| 89 | + /** |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + public function testSlugifyWithRussianLatinTransliteratorBasicId() : void |
120 | 93 | { |
121 | | - $input = '0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz'; |
122 | | - $output = '0123456789-abcdefghijklmnopqrstuvwxyz-abcdefghijklmnopqrstuvwxyz'; |
123 | | - |
124 | | - $slugger = new Slugger(); |
| 94 | + $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
| 95 | + $output = 'syesh-yeshche-etikh-myagkikh-frantsuzskikh-bulok-da-vypey-chayu'; |
| 96 | + $slugger = new Slugger(self::RUSSIAN_LATIN_TRANSLITERATOR_BASIC_ID); |
125 | 97 |
|
126 | 98 | $this->assertEquals($output, $slugger->slugify($input)); |
127 | 99 | } |
128 | 100 |
|
129 | | - public function testTransliterateWithInvalidCompound() |
| 101 | + /** |
| 102 | + * @return void |
| 103 | + */ |
| 104 | + public function testSlugifyWithCyrillicLatinTransliteratorBasicId() : void |
130 | 105 | { |
131 | | - $expectedMessage = 'Unable to create transliterator with compound "Russian-Latin/BGN; UndefinedCommand()"'; |
132 | | - |
133 | | - $this->expectException(UnableToCreateTransliteratorException::class); |
134 | | - $this->expectExceptionMessage($expectedMessage); |
135 | | - |
136 | | - $slugger = new Slugger(); |
| 106 | + $input = 'съешь ещё этих мягких французских булок, да выпей чаю'; |
| 107 | + $output = 'ses-ese-etih-magkih-francuzskih-bulok-da-vypej-cau'; |
| 108 | + $slugger = new Slugger(self::CYRILLIC_LATIN_TRANSLITERATOR_BASIC_ID); |
137 | 109 |
|
138 | | - $slugger->transliterate('', 'UndefinedCommand()'); |
| 110 | + $this->assertEquals($output, $slugger->slugify($input)); |
139 | 111 | } |
140 | 112 |
|
141 | | - public function testExceptions() |
| 113 | + /** |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public function testExceptions() : void |
142 | 117 | { |
143 | | - $this->assertInstanceOf(\RuntimeException::class, new UnableToCreateTransliteratorException('')); |
144 | | - $this->assertInstanceOf(\RuntimeException::class, new UnableToTransliterateException('')); |
145 | | - $this->assertInstanceOf(\RuntimeException::class, new UnsupportedTransliteratorIdentifierException('')); |
| 118 | + $this->assertInstanceOf(\RuntimeException::class, new Exception()); |
| 119 | + $this->assertInstanceOf(Exception::class, new UnableToCreateTransliteratorException()); |
| 120 | + $this->assertInstanceOf(Exception::class, new UnableToTransliterateException()); |
146 | 121 | } |
147 | 122 | } |
0 commit comments