We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | class Hyphenator_Test extends PHP_Typography_Testcase { |
||
| 36 | /** |
||
| 37 | * Hyphenator fixture. |
||
| 38 | * |
||
| 39 | * @var \PHP_Typography\Hyphenator |
||
| 40 | */ |
||
| 41 | protected $h; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sets up the fixture, for example, opens a network connection. |
||
| 45 | * This method is called before a test is executed. |
||
| 46 | */ |
||
| 47 | protected function setUp() { |
||
| 48 | $this->h = new \PHP_Typography\Hyphenator(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Tears down the fixture, for example, closes a network connection. |
||
| 53 | * This method is called after a test is executed. |
||
| 54 | */ |
||
| 55 | protected function tearDown() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Tests __construct. |
||
| 60 | * |
||
| 61 | * @covers ::__construct |
||
| 62 | * |
||
| 63 | * @uses PHP_Typography\Strings::functions |
||
| 64 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 65 | * @uses PHP_Typography\Hyphenator\Trie_Node::__construct |
||
| 66 | * @uses PHP_Typography\Hyphenator\Trie_Node::build_trie |
||
| 67 | * @uses PHP_Typography\Hyphenator\Trie_Node::get_node |
||
| 68 | */ |
||
| 69 | public function test_constructor() { |
||
| 70 | $h = $this->h; |
||
| 71 | |||
| 72 | $this->assertNotNull( $h ); |
||
| 73 | $this->assertInstanceOf( '\PHP_Typography\Hyphenator', $h ); |
||
| 74 | |||
| 75 | $h2 = new \PHP_Typography\Hyphenator( 'en-US', [ 'foo-bar' ] ); |
||
| 76 | $this->assertNotNull( $h2 ); |
||
| 77 | $this->assertInstanceOf( '\PHP_Typography\Hyphenator', $h2 ); |
||
| 78 | $this->assertAttributeSame( 'en-US', 'language', $h2 ); |
||
| 79 | $this->assertAttributeCount( 1, 'custom_exceptions', $h2 ); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Tests set_language. |
||
| 84 | * |
||
| 85 | * @covers ::set_language |
||
| 86 | * |
||
| 87 | * @uses PHP_Typography\Hyphenator\Trie_Node::__construct |
||
| 88 | * @uses PHP_Typography\Hyphenator\Trie_Node::build_trie |
||
| 89 | * @uses PHP_Typography\Hyphenator\Trie_Node::get_node |
||
| 90 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 91 | */ |
||
| 92 | public function test_set_language() { |
||
| 93 | $h = $this->h; |
||
| 94 | $h->set_language( 'en-US' ); |
||
| 95 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty English-US pattern array' ); |
||
| 96 | $this->assertAttributeNotEmpty( 'pattern_exceptions', $h, 'Empty pattern exceptions array' ); |
||
| 97 | |||
| 98 | $h->set_language( 'foobar' ); |
||
| 99 | $this->assertAttributeEmpty( 'pattern_trie', $h ); |
||
| 100 | $this->assertAttributeEmpty( 'pattern_exceptions', $h ); |
||
| 101 | |||
| 102 | $h->set_language( 'no' ); |
||
| 103 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty Norwegian pattern array' ); |
||
| 104 | $this->assertAttributeNotEmpty( 'pattern_exceptions', $h, 'Empty pattern exceptions array' ); // Norwegian has exceptions. |
||
| 105 | |||
| 106 | $h->set_language( 'de' ); |
||
| 107 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty German pattern array' ); |
||
| 108 | $this->assertAttributeEmpty( 'pattern_exceptions', $h, 'Unexpected pattern exceptions found' ); // no exceptions in the German pattern file. |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Tests set_language. |
||
| 113 | * |
||
| 114 | * @covers ::set_language |
||
| 115 | * |
||
| 116 | * @uses ::set_custom_exceptions |
||
| 117 | * @uses ::merge_hyphenation_exceptions |
||
| 118 | * @uses PHP_Typography\Hyphenator\Trie_Node::__construct |
||
| 119 | * @uses PHP_Typography\Hyphenator\Trie_Node::build_trie |
||
| 120 | * @uses PHP_Typography\Hyphenator\Trie_Node::get_node |
||
| 121 | * @uses PHP_Typography\Strings::functions |
||
| 122 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 123 | */ |
||
| 124 | public function test_set_language_with_custom_exceptions() { |
||
| 125 | $h = $this->h; |
||
| 126 | |||
| 127 | $h->set_custom_exceptions( [ |
||
| 128 | 'KINGdesk' => 'KING-desk', |
||
| 129 | ] ); |
||
| 130 | $h->set_language( 'en-US' ); |
||
| 131 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 132 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty pattern array' ); |
||
| 133 | $this->assertAttributeNotEmpty( 'pattern_exceptions', $h, 'Empty pattern exceptions array' ); |
||
| 134 | |||
| 135 | $h->set_language( 'de' ); |
||
| 136 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty pattern array' ); |
||
| 137 | $this->assertAttributeEmpty( 'pattern_exceptions', $h, 'Unexpected pattern exceptions found' ); // no exceptions in the German pattern file. |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Tests set_language. |
||
| 142 | * |
||
| 143 | * @covers ::set_language |
||
| 144 | * |
||
| 145 | * @uses PHP_Typography\Hyphenator\Trie_Node::__construct |
||
| 146 | * @uses PHP_Typography\Hyphenator\Trie_Node::build_trie |
||
| 147 | * @uses PHP_Typography\Hyphenator\Trie_Node::get_node |
||
| 148 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 149 | */ |
||
| 150 | public function test_set_same_hyphenation_language() { |
||
| 151 | $h = $this->h; |
||
| 152 | |||
| 153 | $h->set_language( 'en-US' ); |
||
| 154 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty pattern array' ); |
||
| 155 | $this->assertAttributeNotEmpty( 'pattern_exceptions', $h, 'Empty pattern exceptions array' ); |
||
| 156 | |||
| 157 | $h->set_language( 'en-US' ); |
||
| 158 | $this->assertAttributeNotEmpty( 'pattern_trie', $h, 'Empty pattern array' ); |
||
| 159 | $this->assertAttributeNotEmpty( 'pattern_exceptions', $h, 'Empty pattern exceptions array' ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Provides data for testing set_custom_exceptions. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | function provide_set_custom_exceptions_data() { |
||
| 168 | return [ |
||
| 169 | [ [ 'Hu-go', 'Fö-ba-ß' ], 2, 2 ], |
||
| 170 | [ [], 0, 2 ], |
||
| 171 | ]; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Tests set_custom_exceptions. |
||
| 176 | * |
||
| 177 | * @covers ::set_custom_exceptions |
||
| 178 | * |
||
| 179 | * @uses PHP_Typography\Strings::functions |
||
| 180 | * |
||
| 181 | * @dataProvider provide_set_custom_exceptions_data |
||
| 182 | * |
||
| 183 | * @param array $exceptions Custom exceptions. |
||
| 184 | * @param int $count Number of exceptions to expect. |
||
| 185 | * @param int $times Number of iterations. |
||
| 186 | */ |
||
| 187 | public function test_set_custom_exceptions( $exceptions, $count, $times ) { |
||
| 188 | $h = $this->h; |
||
| 189 | |||
| 190 | for ( $i = 0; $i < $times; ++$i ) { |
||
| 191 | $h->set_custom_exceptions( $exceptions ); |
||
| 192 | |||
| 193 | if ( ! empty( $exceptions ) ) { |
||
| 194 | |||
| 195 | // Exceptions have to be strings. |
||
| 196 | $this->assertAttributeContainsOnly( 'string', 'custom_exceptions', $h ); |
||
| 197 | |||
| 198 | // Assert count. |
||
| 199 | $this->assertAttributeCount( $count, 'custom_exceptions', $h ); |
||
| 200 | } else { |
||
| 201 | $this->assertAttributeEmpty( 'custom_exceptions', $h ); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Assert existence of individual exceptions. |
||
| 205 | foreach ( $exceptions as $exception ) { |
||
| 206 | $exception = mb_strtolower( $exception ); // Exceptions are stored all lowercase. |
||
| 207 | $this->assertAttributeContains( $exception, 'custom_exceptions', $h, "Exception $exception not found in round $i" ); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Tests set_custom_exceptions. |
||
| 214 | * |
||
| 215 | * @covers ::set_custom_exceptions |
||
| 216 | * |
||
| 217 | * @uses ::merge_hyphenation_exceptions |
||
| 218 | * @uses PHP_Typography\Hyphenator\Trie_Node::__construct |
||
| 219 | * @uses PHP_Typography\Hyphenator\Trie_Node::build_trie |
||
| 220 | * @uses PHP_Typography\Hyphenator\Trie_Node::get_node |
||
| 221 | * @uses PHP_Typography\Strings::functions |
||
| 222 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 223 | */ |
||
| 224 | public function test_set_custom_exceptions_again() { |
||
| 225 | $h = $this->h; |
||
| 226 | $exceptions = [ 'Hu-go', 'Fö-ba-ß' ]; |
||
| 227 | $h->set_custom_exceptions( $exceptions ); |
||
| 228 | $h->set_language( 'de' ); // German has no pattern exceptions. |
||
| 229 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 230 | $this->assertAttributeNotEmpty( 'merged_exception_patterns', $h ); |
||
| 231 | |||
| 232 | $exceptions = [ 'Hu-go' ]; |
||
| 233 | $h->set_custom_exceptions( $exceptions ); |
||
| 234 | $this->assertAttributeEmpty( 'merged_exception_patterns', $h ); |
||
| 235 | |||
| 236 | $this->assertAttributeContainsOnly( 'string', 'custom_exceptions', $h ); |
||
| 237 | $this->assertAttributeContains( 'hu-go', 'custom_exceptions', $h ); |
||
| 238 | $this->assertAttributeCount( 1, 'custom_exceptions', $h ); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Tests set_custom_exceptions. |
||
| 243 | * |
||
| 244 | * @covers ::set_custom_exceptions |
||
| 245 | * |
||
| 246 | * @uses PHP_Typography\Strings::functions |
||
| 247 | */ |
||
| 248 | public function test_set_custom_exceptions_unknown_encoding() { |
||
| 249 | $h = $this->h; |
||
| 250 | $exceptions = [ 'Hu-go', mb_convert_encoding( 'Fö-ba-ß', 'ISO-8859-2' ) ]; |
||
| 251 | $h->set_custom_exceptions( $exceptions ); |
||
| 252 | |||
| 253 | $this->assertAttributeContainsOnly( 'string', 'custom_exceptions', $h ); |
||
| 254 | $this->assertAttributeContains( 'hu-go', 'custom_exceptions', $h ); |
||
| 255 | $this->assertAttributeNotContains( 'fö-ba-ß', 'custom_exceptions', $h ); |
||
| 256 | $this->assertAttributeCount( 1, 'custom_exceptions', $h ); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Provide data for testing hyphenation. |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function provide_hyphenate_data() { |
||
| 265 | return [ |
||
| 266 | [ 'A few words to hyphenate like KINGdesk Really there should be more hyphenation here', 'A few words to hy|phen|ate like KING|desk Re|al|ly there should be more hy|phen|ation here', 'en-US', true ], // fake tokenizer doesn't split off punctuation. |
||
| 267 | // Not working with newer de pattern file: [ 'Sauerstofffeldflasche', 'Sau|er|stoff|feld|fla|sche', 'de', true ],. |
||
| 268 | [ 'Sauerstofffeldflasche', 'Sauer|stoff|feld|fla|sche', 'de', true ], |
||
| 269 | // Not working with newer de pattern file: [ 'Sauerstoff Feldflasche', 'Sau|er|stoff Feld|fla|sche', 'de', true ],. |
||
| 270 | [ 'Sauerstoff Feldflasche', 'Sauer|stoff Feld|fla|sche', 'de', true ], // Compound words would not be hyphenated separately. |
||
| 271 | [ 'Sauerstoff-Feldflasche', 'Sauerstoff-Feldflasche', 'de', false ], |
||
| 272 | [ 'A', 'A', 'de', true ], |
||
| 273 | [ 'table', 'ta|ble', 'en-US', false ], |
||
| 274 | [ 'KINGdesk', 'KINGdesk', 'en-US', false ], |
||
| 275 | [ 'italienisch', 'ita|lie|nisch', 'de', false ], |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Tests hyphenate. |
||
| 281 | * |
||
| 282 | * @covers ::hyphenate |
||
| 283 | * @covers ::hyphenate_word |
||
| 284 | * @covers ::lookup_word_pattern |
||
| 285 | * |
||
| 286 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 287 | * @uses PHP_Typography\Text_Parser\Token |
||
| 288 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 289 | * @uses PHP_Typography\Strings::functions |
||
| 290 | * |
||
| 291 | * @dataProvider provide_hyphenate_data |
||
| 292 | * |
||
| 293 | * @param string $html HTML input. |
||
| 294 | * @param string $result Expected result. |
||
| 295 | * @param string $lang Language code. |
||
| 296 | * @param bool $hyphenate_title_case Hyphenate words in Title Case. |
||
| 297 | */ |
||
| 298 | public function test_hyphenate( $html, $result, $lang, $hyphenate_title_case ) { |
||
| 299 | $h = $this->h; |
||
| 300 | $h->set_language( $lang ); |
||
| 301 | $h->set_custom_exceptions( [ 'KING-desk' ] ); |
||
| 302 | |||
| 303 | $this->assertTokensSame( $result, $h->hyphenate( $this->tokenize_sentence( $html ), '|', $hyphenate_title_case, 2, 2, 2 ) ); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Provide data for tessting hyphenation with custom exceptions. |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | public function provide_hyphenate_with_exceptions_data() { |
||
| 312 | return [ |
||
| 313 | [ 'KINGdesk', 'KING|desk', [ 'KING-desk' ], 'en-US', true ], |
||
| 314 | [ 'Geschäftsübernahme', 'Ge|schäfts|über|nah|me', [], 'de', true ], |
||
| 315 | [ 'Geschäftsübernahme', 'Ge|schäfts|über|nah|me', [ 'Ge-schäfts-über-nah-me' ], 'de', true ], |
||
| 316 | [ 'Trinkwasserinstallation', 'Trink|was|ser|in|stal|la|ti|on', [], 'de', true, true, true, false ], |
||
| 317 | [ 'Trinkwasserinstallation', 'Trink|wasser|in|stal|la|tion', [ 'Trink-wasser-in-stal-la-tion' ], 'de', true ], |
||
| 318 | [ 'Trinkwasserinstallation', 'Trink|wasser|in|stal|la|tion', [ 'Trink-wasser-in-stal-la-tion' ], 'en-US', true ], |
||
| 319 | ]; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Tests hyphenate. |
||
| 324 | * |
||
| 325 | * @covers ::hyphenate |
||
| 326 | * @covers ::hyphenate_word |
||
| 327 | * @covers ::lookup_word_pattern |
||
| 328 | * |
||
| 329 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 330 | * @uses PHP_Typography\Text_Parser\Token |
||
| 331 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 332 | * @uses PHP_Typography\Strings::functions |
||
| 333 | * |
||
| 334 | * @dataProvider provide_hyphenate_with_exceptions_data |
||
| 335 | * |
||
| 336 | * @param string $html HTML input. |
||
| 337 | * @param string $result Expected result. |
||
| 338 | * @param array $exceptions Custom hyphenation exceptions. |
||
| 339 | * @param string $lang Language code. |
||
| 340 | * @param bool $hyphenate_title_case Hyphenate words in Title Case. |
||
| 341 | */ |
||
| 342 | public function test_hyphenate_with_exceptions( $html, $result, $exceptions, $lang, $hyphenate_title_case ) { |
||
| 343 | $h = $this->h; |
||
| 344 | $h->set_language( $lang ); |
||
| 345 | $h->set_custom_exceptions( $exceptions ); |
||
| 346 | |||
| 347 | $this->assertTokensSame( $result, $h->hyphenate( $this->tokenize_sentence( $html ), '|', $hyphenate_title_case, 2, 2, 2 ) ); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Tests hyphenate. |
||
| 352 | * |
||
| 353 | * @covers ::hyphenate |
||
| 354 | * @covers ::hyphenate_word |
||
| 355 | * @covers ::lookup_word_pattern |
||
| 356 | * |
||
| 357 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 358 | * @uses PHP_Typography\Text_Parser\Token |
||
| 359 | * @uses PHP_Typography\Strings::functions |
||
| 360 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 361 | * @uses \mb_convert_encoding |
||
| 362 | */ |
||
| 363 | public function test_hyphenate_wrong_encoding() { |
||
| 364 | $this->h->set_language( 'de' ); |
||
| 365 | |||
| 366 | $tokens = $this->tokenize( mb_convert_encoding( 'Änderungsmeldung', 'ISO-8859-2' ) ); |
||
| 367 | $hyphenated = $this->h->hyphenate( $tokens, '|', true, 2, 2, 2 ); |
||
| 368 | $this->assertTokensSame( $hyphenated, $tokens, 'Wrong encoding, value should be unchanged.' ); |
||
| 369 | |||
| 370 | $tokens = $this->tokenize( 'Änderungsmeldung' ); |
||
| 371 | $hyphenated = $this->h->hyphenate( $tokens, '|', true, 2, 2, 2 ); |
||
| 372 | $this->assertTokensNotSame( $hyphenated, $tokens, 'Correct encoding, string should have been hyphenated.' ); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Tests hyphenate. |
||
| 377 | * |
||
| 378 | * @covers ::hyphenate |
||
| 379 | * @covers ::hyphenate_word |
||
| 380 | * @covers ::lookup_word_pattern |
||
| 381 | * |
||
| 382 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 383 | * @uses PHP_Typography\Text_Parser\Token |
||
| 384 | * @uses PHP_Typography\Strings::functions |
||
| 385 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 386 | */ |
||
| 387 | public function test_hyphenate_no_title_case() { |
||
| 388 | $this->h->set_language( 'de' ); |
||
| 389 | |||
| 390 | $tokens = $this->tokenize( 'Änderungsmeldung' ); |
||
| 391 | $hyphenated = $this->h->hyphenate( $tokens, '|', false, 2, 2, 2 ); |
||
| 392 | $this->assertEquals( $tokens, $hyphenated ); |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Tests hyphenate. |
||
| 397 | * |
||
| 398 | * @covers ::hyphenate |
||
| 399 | * @covers ::hyphenate_word |
||
| 400 | * @covers ::lookup_word_pattern |
||
| 401 | * |
||
| 402 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 403 | * @uses PHP_Typography\Text_Parser\Token |
||
| 404 | * @uses PHP_Typography\Strings::functions |
||
| 405 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 406 | */ |
||
| 407 | public function test_hyphenate_invalid() { |
||
| 408 | $this->h->set_language( 'de' ); |
||
| 409 | |||
| 410 | $tokens = $this->tokenize( 'Änderungsmeldung' ); |
||
| 411 | $hyphenated = $this->h->hyphenate( $tokens, '|', true, 2, 0, 2 ); |
||
| 412 | $this->assertEquals( $tokens, $hyphenated ); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Tests hyphenate. |
||
| 417 | * |
||
| 418 | * @covers ::hyphenate |
||
| 419 | * @covers ::hyphenate_word |
||
| 420 | * @covers ::lookup_word_pattern |
||
| 421 | * |
||
| 422 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 423 | * @uses PHP_Typography\Text_Parser\Token |
||
| 424 | * @uses PHP_Typography\Strings::functions |
||
| 425 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 426 | */ |
||
| 427 | public function test_hyphenate_no_custom_exceptions() { |
||
| 428 | $this->h->set_language( 'en-US' ); |
||
| 429 | |||
| 430 | // Again, no punctuation due to the fake tokenization. |
||
| 431 | $this->assertTokensSame( |
||
| 432 | 'A few words to hy|phen|ate like KINGdesk Re|al|ly there should be more hy|phen|ation here', |
||
| 433 | $this->h->hyphenate( $this->tokenize_sentence( 'A few words to hyphenate like KINGdesk Really there should be more hyphenation here' ), '|', true, 2, 2, 2 ) |
||
| 434 | ); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Tests hyphenate. |
||
| 439 | * |
||
| 440 | * @covers ::hyphenate |
||
| 441 | * @covers ::hyphenate_word |
||
| 442 | * @covers ::lookup_word_pattern |
||
| 443 | * |
||
| 444 | * @uses ReflectionClass |
||
| 445 | * @uses ReflectionProperty |
||
| 446 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 447 | * @uses PHP_Typography\Text_Parser\Token |
||
| 448 | * @uses PHP_Typography\Strings::functions |
||
| 449 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 450 | */ |
||
| 451 | public function test_hyphenate_no_exceptions_at_all() { |
||
| 452 | $this->h->set_language( 'en-US' ); |
||
| 453 | |||
| 454 | // Unset some internal stuff. |
||
| 455 | $ref = new \ReflectionClass( '\PHP_Typography\Hyphenator' ); |
||
| 456 | $prop = $ref->getProperty( 'pattern_exceptions' ); |
||
| 457 | $prop->setAccessible( true ); |
||
| 458 | $prop->setValue( $this->h, [] ); |
||
| 459 | $prop = $ref->getProperty( 'merged_exception_patterns' ); |
||
| 460 | $prop->setAccessible( true ); |
||
| 461 | $prop->setValue( $this->h, null ); |
||
| 462 | |||
| 463 | // Again, no punctuation due to the fake tokenization. |
||
| 464 | $this->assertTokensSame( |
||
| 465 | 'A few words to hy|phen|ate like KINGdesk Re|al|ly there should be more hy|phen|ation here', |
||
| 466 | $this->h->hyphenate( $this->tokenize_sentence( 'A few words to hyphenate like KINGdesk Really there should be more hyphenation here' ), '|', true, 2, 2, 2 ) |
||
| 467 | ); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Tests convert_hyphenation_exception_to_pattern. |
||
| 472 | * |
||
| 473 | * @covers ::convert_hyphenation_exception_to_pattern |
||
| 474 | * |
||
| 475 | * @uses PHP_Typography\Strings::functions |
||
| 476 | */ |
||
| 477 | public function test_convert_hyphenation_exception_to_pattern() { |
||
| 478 | $h = $this->h; |
||
| 479 | $this->assertSame( [ |
||
| 480 | 4 => 9, |
||
| 481 | ], $this->invokeMethod( $h, 'convert_hyphenation_exception_to_pattern', [ 'KING-desk' ] ) ); |
||
| 482 | |||
| 483 | $this->assertSame( [ |
||
| 484 | 2 => 9, |
||
| 485 | ], $this->invokeMethod( $h, 'convert_hyphenation_exception_to_pattern', [ 'ta-ble' ] ) ); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Tests convert_hyphenation_exception_to_pattern. |
||
| 490 | * |
||
| 491 | * @covers ::convert_hyphenation_exception_to_pattern |
||
| 492 | * |
||
| 493 | * @uses PHP_Typography\Strings::functions |
||
| 494 | * @uses \mb_convert_encoding |
||
| 495 | */ |
||
| 496 | public function test_convert_hyphenation_exception_to_pattern_unknown_encoding() { |
||
| 497 | $h = $this->h; |
||
| 498 | $exception = mb_convert_encoding( 'Fö-ba-ß' , 'ISO-8859-2' ); |
||
| 499 | |||
| 500 | $this->assertNull( $this->invokeMethod( $h, 'convert_hyphenation_exception_to_pattern', [ $exception ] ) ); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Tests merge_hyphenation_exceptions. |
||
| 505 | * |
||
| 506 | * @covers ::merge_hyphenation_exceptions |
||
| 507 | * |
||
| 508 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 509 | * @uses PHP_Typography\Strings::mb_str_split |
||
| 510 | * @uses PHP_Typography\Strings::functions |
||
| 511 | */ |
||
| 512 | public function test_merge_hyphenation_exceptions() { |
||
| 513 | $h = $this->h; |
||
| 514 | $h->set_custom_exceptions( [ 'Hu-go', 'Fä-vi-ken' ] ); |
||
| 515 | |||
| 516 | $h->set_language( 'en-US' ); // w/ pattern exceptions. |
||
| 517 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 518 | $this->assertAttributeNotCount( 0, 'merged_exception_patterns', $h ); |
||
| 519 | $this->assertAttributeNotCount( 1, 'merged_exception_patterns', $h ); |
||
| 520 | $this->assertAttributeNotCount( 2, 'merged_exception_patterns', $h ); |
||
| 521 | $this->assertAttributeArrayHasKey( 'hugo', 'merged_exception_patterns', $h ); |
||
| 522 | $this->assertAttributeArrayHasKey( 'fäviken', 'merged_exception_patterns', $h ); |
||
| 523 | |||
| 524 | $h->set_language( 'de' ); // w/o pattern exceptions. |
||
| 525 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 526 | $this->assertAttributeCount( 2, 'merged_exception_patterns', $h ); |
||
| 527 | $this->assertAttributeArrayHasKey( 'hugo', 'merged_exception_patterns', $h ); |
||
| 528 | $this->assertAttributeArrayHasKey( 'fäviken', 'merged_exception_patterns', $h ); |
||
| 529 | |||
| 530 | $h->set_language( 'en-US' ); // w/ pattern exceptions. |
||
| 531 | $h->set_custom_exceptions( [] ); |
||
| 532 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 533 | $this->assertAttributeNotCount( 0, 'merged_exception_patterns', $h ); |
||
| 534 | $this->assertAttributeArrayNotHasKey( 'hugo', 'merged_exception_patterns', $h ); |
||
| 535 | $this->assertAttributeArrayNotHasKey( 'fäviken', 'merged_exception_patterns', $h ); |
||
| 536 | |||
| 537 | $h->set_language( 'de' ); // w/o pattern exceptions. |
||
| 538 | $this->invokeMethod( $h, 'merge_hyphenation_exceptions', [] ); |
||
| 539 | $this->assertAttributeCount( 0, 'merged_exception_patterns', $h ); |
||
| 540 | $this->assertAttributeArrayNotHasKey( 'hugo', 'merged_exception_patterns', $h ); |
||
| 541 | $this->assertAttributeArrayNotHasKey( 'fäviken', 'merged_exception_patterns', $h ); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Provide data for testing is_odd. |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | public function provide_is_odd_data() { |
||
| 550 | return [ |
||
| 551 | [ 0, false ], |
||
| 552 | [ 1, true ], |
||
| 553 | [ 2, false ], |
||
| 554 | [ 5, true ], |
||
| 555 | [ 68, false ], |
||
| 556 | [ 781, true ], |
||
| 557 | ]; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Test is_odd. |
||
| 562 | * |
||
| 563 | * @covers ::is_odd |
||
| 564 | * @dataProvider provide_is_odd_data |
||
| 565 | * |
||
| 566 | * @param int $number A number. |
||
| 567 | * @param bool $result Expected result. |
||
| 568 | */ |
||
| 569 | public function test_is_odd( $number, $result ) { |
||
| 570 | if ( $result ) { |
||
| 571 | $this->assertTrue( $this->invokeStaticMethod( \PHP_Typography\Hyphenator::class, 'is_odd', [ $number ] ) ); |
||
| 572 | } else { |
||
| 573 | $this->assertFalse( $this->invokeStaticMethod( \PHP_Typography\Hyphenator::class, 'is_odd', [ $number ] ) ); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Test get_object_hash function. |
||
| 579 | * |
||
| 580 | * @covers ::get_object_hash |
||
| 581 | */ |
||
| 582 | public function test_get_object_hash() { |
||
| 583 | $hash1 = $this->invokeStaticMethod( \PHP_Typography\Hyphenator::class, 'get_object_hash', [ 666 ] ); |
||
| 584 | $this->assertInternalType( 'string', $hash1 ); |
||
| 585 | $this->assertGreaterThan( 0, strlen( $hash1 ) ); |
||
| 586 | |||
| 587 | $hash2 = $this->invokeStaticMethod( \PHP_Typography\Hyphenator::class, 'get_object_hash', [ new \stdClass() ] ); |
||
| 588 | $this->assertInternalType( 'string', $hash2 ); |
||
| 589 | $this->assertGreaterThan( 0, strlen( $hash2 ) ); |
||
| 590 | |||
| 591 | $this->assertNotEquals( $hash1, $hash2 ); |
||
| 592 | } |
||
| 593 | } |
||
| 594 |