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:
Complex classes like Settings_Test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Settings_Test, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Settings_Test extends PHP_Typography_Testcase { |
||
| 44 | /** |
||
| 45 | * Settings fixture. |
||
| 46 | * |
||
| 47 | * @var \PHP_Typography\Settings |
||
| 48 | */ |
||
| 49 | protected $settings; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Sets up the fixture, for example, opens a network connection. |
||
| 53 | * This method is called before a test is executed. |
||
| 54 | */ |
||
| 55 | protected function setUp() { |
||
| 56 | $this->settings = new \PHP_Typography\Settings( false ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Tears down the fixture, for example, closes a network connection. |
||
| 61 | * This method is called after a test is executed. |
||
| 62 | */ |
||
| 63 | protected function tearDown() { |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Tests set_defaults. |
||
| 68 | * |
||
| 69 | * @covers ::set_defaults |
||
| 70 | * |
||
| 71 | * @uses PHP_Typography\Settings\Dash_Style::get_styled_dashes |
||
| 72 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 73 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 74 | * @uses PHP_Typography\Arrays::array_map_assoc |
||
| 75 | */ |
||
| 76 | public function test_set_defaults() { |
||
| 77 | $second_settings = new \PHP_Typography\Settings( false ); |
||
| 78 | $this->assertAttributeEmpty( 'data', $second_settings ); |
||
| 79 | $second_settings->set_defaults(); |
||
| 80 | $this->assertAttributeNotEmpty( 'data', $second_settings ); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Tests initialization. |
||
| 85 | * |
||
| 86 | * @covers ::init |
||
| 87 | * @covers ::__construct |
||
| 88 | * |
||
| 89 | * @uses ::set_defaults |
||
| 90 | * @uses PHP_Typography\Settings\Dash_Style::get_styled_dashes |
||
| 91 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 92 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 93 | * @uses PHP_Typography\Arrays::array_map_assoc |
||
| 94 | */ |
||
| 95 | public function test_initialization() { |
||
| 96 | $s = $this->settings; |
||
| 97 | |||
| 98 | // No defaults. |
||
| 99 | $this->assertAttributeEmpty( 'data', $s ); |
||
| 100 | |||
| 101 | // After set_defaults(). |
||
| 102 | $s->set_defaults(); |
||
| 103 | $this->assertAttributeNotEmpty( 'data', $s ); |
||
| 104 | |||
| 105 | $second_settings = new \PHP_Typography\Settings( true ); |
||
| 106 | $this->assertAttributeNotEmpty( 'data', $second_settings ); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Tests __get. |
||
| 112 | * |
||
| 113 | * @covers ::__get |
||
| 114 | * |
||
| 115 | * @uses ::offsetGet |
||
| 116 | */ |
||
| 117 | public function test___get() { |
||
| 118 | $s = $this->settings; |
||
| 119 | |||
| 120 | $s['new_key'] = 42; |
||
| 121 | $this->assertEquals( 42, $s->new_key ); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Tests __set. |
||
| 126 | * |
||
| 127 | * @covers ::__set |
||
| 128 | * |
||
| 129 | * @uses ::__get |
||
| 130 | * @uses ::__isset |
||
| 131 | */ |
||
| 132 | public function test___set() { |
||
| 133 | $s = $this->settings; |
||
| 134 | |||
| 135 | $this->assertFalse( isset( $s->new_key ) ); |
||
| 136 | $s->new_key = 42; |
||
| 137 | $this->assertTrue( isset( $s->new_key ) ); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Tests __isset. |
||
| 142 | * |
||
| 143 | * @covers ::__isset |
||
| 144 | */ |
||
| 145 | public function test___isset() { |
||
| 146 | $s = $this->settings; |
||
| 147 | |||
| 148 | $this->assertFalse( isset( $s->new_key ) ); |
||
| 149 | $s->new_key = 42; |
||
| 150 | $this->assertTrue( isset( $s->new_key ) ); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Tests __unset. |
||
| 155 | * |
||
| 156 | * @covers ::__unset |
||
| 157 | */ |
||
| 158 | public function test___unset() { |
||
| 159 | $s = $this->settings; |
||
| 160 | |||
| 161 | $s->new_key = 42; |
||
| 162 | $this->assertTrue( isset( $s->new_key ) ); |
||
| 163 | |||
| 164 | unset( $s->new_key ); |
||
| 165 | $this->assertFalse( isset( $s->new_key ) ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Tests offsetSet. |
||
| 170 | * |
||
| 171 | * @covers ::offsetSet |
||
| 172 | * |
||
| 173 | * @uses ::offsetGet |
||
| 174 | * @uses ::offsetExists |
||
| 175 | */ |
||
| 176 | public function test_offsetSet() { |
||
| 177 | $s = $this->settings; |
||
| 178 | |||
| 179 | $this->assertFalse( isset( $s[0] ) ); |
||
| 180 | $s[] = 666; |
||
| 181 | $this->assertEquals( 666, $s[0] ); |
||
| 182 | |||
| 183 | $this->assertFalse( isset( $s['new_key'] ) ); |
||
| 184 | $s['new_key'] = 42; |
||
| 185 | $this->assertEquals( 42, $s['new_key'] ); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Tests offsetExists. |
||
| 190 | * |
||
| 191 | * @covers ::offsetExists |
||
| 192 | * |
||
| 193 | * @uses ::offsetSet |
||
| 194 | */ |
||
| 195 | public function test_offsetExists() { |
||
| 196 | $s = $this->settings; |
||
| 197 | |||
| 198 | $this->assertFalse( isset( $s['new_key'] ) ); |
||
| 199 | $s['new_key'] = 42; |
||
| 200 | $this->assertTrue( isset( $s['new_key'] ) ); |
||
| 201 | |||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Tests offsetUnset. |
||
| 206 | * |
||
| 207 | * @covers ::offsetUnset |
||
| 208 | * |
||
| 209 | * @uses ::offsetSet |
||
| 210 | * @uses ::offsetGet |
||
| 211 | * @uses ::offsetExists |
||
| 212 | */ |
||
| 213 | public function test_offsetUnset() { |
||
| 214 | $s = $this->settings; |
||
| 215 | |||
| 216 | $s['new_key'] = 42; |
||
| 217 | $this->assertTrue( isset( $s['new_key'] ) ); |
||
| 218 | |||
| 219 | unset( $s['new_key'] ); |
||
| 220 | $this->assertFalse( isset( $s['new_key'] ) ); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Tests offsetGet. |
||
| 225 | * |
||
| 226 | * @covers ::offsetGet |
||
| 227 | * |
||
| 228 | * @uses ::offsetSet |
||
| 229 | */ |
||
| 230 | public function test_offsetGet() { |
||
| 231 | $s = $this->settings; |
||
| 232 | $this->assertNull( $s['new_key'] ); |
||
| 233 | |||
| 234 | $s['new_key'] = 42; |
||
| 235 | $this->assertEquals( 42, $s['new_key'] ); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Tests primary_quote_style. |
||
| 240 | * |
||
| 241 | * @covers ::primary_quote_style |
||
| 242 | */ |
||
| 243 | public function test_primary_quote_style() { |
||
| 244 | $s = $this->settings; |
||
| 245 | |||
| 246 | $this->assertInstanceOf( Quotes::class, $s->primary_quote_style(), 'Primary quote style is not an instance of Quotes.' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Tests secondary_quote_style. |
||
| 251 | * |
||
| 252 | * @covers ::secondary_quote_style |
||
| 253 | */ |
||
| 254 | public function test_secondary_quote_style() { |
||
| 255 | $s = $this->settings; |
||
| 256 | |||
| 257 | $this->assertInstanceOf( Quotes::class, $s->secondary_quote_style(), 'Secondary quote style is not an instance of Quotes.' ); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Tests dash_style. |
||
| 262 | * |
||
| 263 | * @covers ::dash_style |
||
| 264 | */ |
||
| 265 | public function test_dash_style() { |
||
| 266 | $s = $this->settings; |
||
| 267 | |||
| 268 | $this->assertInstanceOf( Dashes::class, $s->dash_style(), 'Dash style is not an instance of Dashes.' ); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Tests custom_units. |
||
| 273 | * |
||
| 274 | * @covers ::custom_units |
||
| 275 | */ |
||
| 276 | public function test_custom_units() { |
||
| 277 | $s = $this->settings; |
||
| 278 | |||
| 279 | $this->assertInternalType( 'string', $s->custom_units(), 'The result of custom_units() is not a string.' ); |
||
| 280 | } |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Tests set_ignore_parser_errors. |
||
| 285 | * |
||
| 286 | * @covers ::set_ignore_parser_errors |
||
| 287 | */ |
||
| 288 | public function test_set_ignore_parser_errors() { |
||
| 289 | $s = $this->settings; |
||
| 290 | |||
| 291 | $s->set_ignore_parser_errors( true ); |
||
| 292 | $this->assertTrue( $s['parserErrorsIgnore'] ); |
||
| 293 | |||
| 294 | $s->set_ignore_parser_errors( false ); |
||
| 295 | $this->assertFalse( $s['parserErrorsIgnore'] ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Tests set_parser_errors_handler. |
||
| 300 | * |
||
| 301 | * @covers ::set_parser_errors_handler |
||
| 302 | */ |
||
| 303 | public function test_set_parser_errors_handler() { |
||
| 304 | $s = $this->settings; |
||
| 305 | |||
| 306 | // Default: no handler. |
||
| 307 | $this->assertEmpty( $s['parserErrorsHandler'] ); |
||
| 308 | |||
| 309 | // Valid handler. |
||
| 310 | $s->set_parser_errors_handler( function( $errors ) { |
||
| 311 | return []; |
||
| 312 | } ); |
||
| 313 | $this->assertInternalType( 'callable', $s['parserErrorsHandler'] ); |
||
| 314 | $old_handler = $s['parserErrorsHandler']; |
||
| 315 | |||
| 316 | // Invalid handler, previous handler not changed. |
||
| 317 | $s->set_parser_errors_handler( 'foobar' ); |
||
| 318 | $this->assertInternalType( 'callable', $s['parserErrorsHandler'] ); |
||
| 319 | $this->assertSame( $old_handler, $s['parserErrorsHandler'] ); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Tests set_tags_to_ignore. |
||
| 324 | * |
||
| 325 | * @covers ::set_tags_to_ignore |
||
| 326 | * |
||
| 327 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 328 | */ |
||
| 329 | public function test_set_tags_to_ignore() { |
||
| 330 | $s = $this->settings; |
||
| 331 | $always_ignore = [ 'iframe', 'textarea', 'button', 'select', 'optgroup', 'option', 'map', 'style', 'head', 'title', 'script', 'applet', 'object', 'param' ]; |
||
| 332 | $self_closing_tags = [ 'area', 'base', 'basefont', 'br', 'frame', 'hr', 'img', 'input', 'link', 'meta' ]; |
||
| 333 | |||
| 334 | // Default tags. |
||
| 335 | $s->set_tags_to_ignore( [ 'code', 'head', 'kbd', 'object', 'option', 'pre', 'samp', 'script', 'noscript', 'noembed', 'select', 'style', 'textarea', 'title', 'var', 'math' ] ); |
||
| 336 | $this->assertArraySubset( [ 'code', 'head', 'kbd', 'object', 'option', 'pre', 'samp', 'script', 'noscript', 'noembed', 'select', 'style', 'textarea', 'title', 'var', 'math' ], $s['ignoreTags'] ); |
||
| 337 | foreach ( $always_ignore as $tag ) { |
||
| 338 | $this->assertContains( $tag, $s['ignoreTags'] ); |
||
| 339 | } |
||
| 340 | foreach ( $self_closing_tags as $tag ) { |
||
| 341 | $this->assertNotContains( $tag, $s['ignoreTags'] ); |
||
| 342 | } |
||
| 343 | |||
| 344 | // Auto-close tag and something else. |
||
| 345 | $s->set_tags_to_ignore( [ 'img', 'foo' ] ); |
||
| 346 | $this->assertContains( 'foo', $s['ignoreTags'] ); |
||
| 347 | foreach ( $self_closing_tags as $tag ) { |
||
| 348 | $this->assertNotContains( $tag, $s['ignoreTags'] ); |
||
| 349 | } |
||
| 350 | foreach ( $always_ignore as $tag ) { |
||
| 351 | $this->assertContains( $tag, $s['ignoreTags'] ); |
||
| 352 | } |
||
| 353 | |||
| 354 | $s->set_tags_to_ignore( 'img foo \ ' ); // should not result in an error. |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Tests set_classes_to_ignore. |
||
| 359 | * |
||
| 360 | * @covers ::set_classes_to_ignore |
||
| 361 | * |
||
| 362 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 363 | */ |
||
| 364 | public function test_set_classes_to_ignore() { |
||
| 365 | $s = $this->settings; |
||
| 366 | |||
| 367 | $s->set_classes_to_ignore( 'foo bar' ); |
||
| 368 | $this->assertContains( 'foo', $this->settings['ignoreClasses'] ); |
||
| 369 | $this->assertContains( 'bar', $this->settings['ignoreClasses'] ); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Tests set_ids_to_ignore. |
||
| 374 | * |
||
| 375 | * @covers ::set_ids_to_ignore |
||
| 376 | * |
||
| 377 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 378 | */ |
||
| 379 | public function test_set_ids_to_ignore() { |
||
| 380 | $s = $this->settings; |
||
| 381 | |||
| 382 | $s->set_ids_to_ignore( 'foobar barfoo' ); |
||
| 383 | $this->assertContains( 'foobar', $this->settings['ignoreIDs'] ); |
||
| 384 | $this->assertContains( 'barfoo', $this->settings['ignoreIDs'] ); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Tests set_smart_quotes. |
||
| 389 | * |
||
| 390 | * @covers ::set_smart_quotes |
||
| 391 | */ |
||
| 392 | public function test_set_smart_quotes() { |
||
| 393 | $this->settings->set_smart_quotes( true ); |
||
| 394 | $this->assertTrue( $this->settings['smartQuotes'] ); |
||
| 395 | |||
| 396 | $this->settings->set_smart_quotes( false ); |
||
| 397 | $this->assertFalse( $this->settings['smartQuotes'] ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Tests set_smart_quotes_primary. |
||
| 402 | * |
||
| 403 | * @covers ::set_smart_quotes_primary |
||
| 404 | * @covers ::get_quote_style |
||
| 405 | * @covers ::get_style |
||
| 406 | * |
||
| 407 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 408 | */ |
||
| 409 | public function test_set_smart_quotes_primary() { |
||
| 410 | $s = $this->settings; |
||
| 411 | |||
| 412 | $quote_styles = [ |
||
| 413 | 'doubleCurled', |
||
| 414 | 'doubleCurledReversed', |
||
| 415 | 'doubleLow9', |
||
| 416 | 'doubleLow9Reversed', |
||
| 417 | 'singleCurled', |
||
| 418 | 'singleCurledReversed', |
||
| 419 | 'singleLow9', |
||
| 420 | 'singleLow9Reversed', |
||
| 421 | 'doubleGuillemetsFrench', |
||
| 422 | 'doubleGuillemets', |
||
| 423 | 'doubleGuillemetsReversed', |
||
| 424 | 'singleGuillemets', |
||
| 425 | 'singleGuillemetsReversed', |
||
| 426 | 'cornerBrackets', |
||
| 427 | 'whiteCornerBracket', |
||
| 428 | ]; |
||
| 429 | |||
| 430 | foreach ( $quote_styles as $style ) { |
||
| 431 | $s->set_smart_quotes_primary( $style ); |
||
| 432 | |||
| 433 | $this->assertSmartQuotesStyle( $style, $s->primary_quote_style()->open(), $s->primary_quote_style()->close() ); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Tests set_smart_quotes_primary with an invalid input. |
||
| 439 | * |
||
| 440 | * @covers ::set_smart_quotes_primary |
||
| 441 | * @covers ::get_quote_style |
||
| 442 | * @covers ::get_style |
||
| 443 | * |
||
| 444 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 445 | * |
||
| 446 | * @expectedException \DomainException |
||
| 447 | * @expectedExceptionMessageRegExp /^Invalid quote style \w+\.$/ |
||
| 448 | */ |
||
| 449 | public function test_set_smart_quotes_primary_invalid() { |
||
| 450 | $s = $this->settings; |
||
| 451 | |||
| 452 | $s->set_smart_quotes_primary( 'invalidStyleName' ); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Tests set_smart_quotes_primary with a Quotes object. |
||
| 457 | * |
||
| 458 | * @covers ::set_smart_quotes_primary |
||
| 459 | * @covers ::get_quote_style |
||
| 460 | * @covers ::get_style |
||
| 461 | */ |
||
| 462 | public function test_set_smart_quotes_primary_to_object() { |
||
| 463 | $s = $this->settings; |
||
| 464 | |||
| 465 | // Create a stub for the Token_Fixer interface. |
||
| 466 | $fake_quotes = $this->createMock( Quotes::class ); |
||
| 467 | $fake_quotes->method( 'open' )->willReturn( 'x' ); |
||
| 468 | $fake_quotes->method( 'close' )->willReturn( 'y' ); |
||
| 469 | |||
| 470 | $s->set_smart_quotes_primary( $fake_quotes ); |
||
| 471 | |||
| 472 | $this->assertSame( 'x', $s->primary_quote_style()->open() ); |
||
| 473 | $this->assertSame( 'y', $s->primary_quote_style()->close() ); |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Tests set_smart_quotes_secondary. |
||
| 478 | * |
||
| 479 | * @covers ::set_smart_quotes_secondary |
||
| 480 | * @covers ::get_quote_style |
||
| 481 | * @covers ::get_style |
||
| 482 | * |
||
| 483 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 484 | */ |
||
| 485 | public function test_set_smart_quotes_secondary() { |
||
| 486 | $s = $this->settings; |
||
| 487 | $quote_styles = [ |
||
| 488 | 'doubleCurled', |
||
| 489 | 'doubleCurledReversed', |
||
| 490 | 'doubleLow9', |
||
| 491 | 'doubleLow9Reversed', |
||
| 492 | 'singleCurled', |
||
| 493 | 'singleCurledReversed', |
||
| 494 | 'singleLow9', |
||
| 495 | 'singleLow9Reversed', |
||
| 496 | 'doubleGuillemetsFrench', |
||
| 497 | 'doubleGuillemets', |
||
| 498 | 'doubleGuillemetsReversed', |
||
| 499 | 'singleGuillemets', |
||
| 500 | 'singleGuillemetsReversed', |
||
| 501 | 'cornerBrackets', |
||
| 502 | 'whiteCornerBracket', |
||
| 503 | ]; |
||
| 504 | |||
| 505 | foreach ( $quote_styles as $style ) { |
||
| 506 | $s->set_smart_quotes_secondary( $style ); |
||
| 507 | |||
| 508 | $this->assertSmartQuotesStyle( $style, $s->secondary_quote_style()->open(), $s->secondary_quote_style()->close() ); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Tests set_smart_quotes_secondary with an invalid input. |
||
| 514 | * |
||
| 515 | * @covers ::set_smart_quotes_secondary |
||
| 516 | * @covers ::get_quote_style |
||
| 517 | * @covers ::get_style |
||
| 518 | * |
||
| 519 | * @uses PHP_Typography\Settings\Quote_Style::get_styled_quotes |
||
| 520 | * |
||
| 521 | * @expectedException \DomainException |
||
| 522 | * @expectedExceptionMessageRegExp /^Invalid quote style \w+\.$/ |
||
| 523 | */ |
||
| 524 | public function test_set_smart_quotes_secondary_invalid() { |
||
| 525 | $s = $this->settings; |
||
| 526 | |||
| 527 | $s->set_smart_quotes_secondary( 'invalidStyleName' ); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Tests set_smart_quotes_secondary with a Quotes object. |
||
| 532 | * |
||
| 533 | * @covers ::set_smart_quotes_secondary |
||
| 534 | * @covers ::get_quote_style |
||
| 535 | * @covers ::get_style |
||
| 536 | */ |
||
| 537 | public function test_set_smart_quotes_secondary_to_object() { |
||
| 538 | $s = $this->settings; |
||
| 539 | |||
| 540 | // Create a stub for the Token_Fixer interface. |
||
| 541 | $fake_quotes = $this->createMock( Quotes::class ); |
||
| 542 | $fake_quotes->method( 'open' )->willReturn( 'xx' ); |
||
| 543 | $fake_quotes->method( 'close' )->willReturn( 'yy' ); |
||
| 544 | |||
| 545 | $s->set_smart_quotes_secondary( $fake_quotes ); |
||
| 546 | |||
| 547 | $this->assertSame( 'xx', $s->secondary_quote_style()->open() ); |
||
| 548 | $this->assertSame( 'yy', $s->secondary_quote_style()->close() ); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Test set_smart_dashes. |
||
| 553 | * |
||
| 554 | * @covers ::set_smart_dashes |
||
| 555 | */ |
||
| 556 | public function test_set_smart_dashes() { |
||
| 557 | $this->settings->set_smart_dashes( true ); |
||
| 558 | $this->assertTrue( $this->settings['smartDashes'] ); |
||
| 559 | |||
| 560 | $this->settings->set_smart_dashes( false ); |
||
| 561 | $this->assertFalse( $this->settings['smartDashes'] ); |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Test set_smart_dashes_style. |
||
| 566 | * |
||
| 567 | * @covers ::set_smart_dashes_style |
||
| 568 | * @covers ::get_style |
||
| 569 | * |
||
| 570 | * @uses PHP_Typography\Settings\Dash_Style::get_styled_dashes |
||
| 571 | */ |
||
| 572 | public function test_set_smart_dashes_style() { |
||
| 573 | $s = $this->settings; |
||
| 574 | |||
| 575 | $s->set_smart_dashes_style( 'traditionalUS' ); |
||
| 576 | $dashes = $s->dash_style(); |
||
| 577 | |||
| 578 | $this->assertSame( U::EM_DASH, $dashes->parenthetical_dash() ); |
||
| 579 | $this->assertSame( U::EN_DASH, $dashes->interval_dash() ); |
||
| 580 | $this->assertSame( U::THIN_SPACE, $dashes->parenthetical_space() ); |
||
| 581 | $this->assertSame( U::THIN_SPACE, $dashes->interval_space() ); |
||
| 582 | |||
| 583 | $s->set_smart_dashes_style( 'international' ); |
||
| 584 | $dashes = $s->dash_style(); |
||
| 585 | |||
| 586 | $this->assertSame( U::EN_DASH, $dashes->parenthetical_dash() ); |
||
| 587 | $this->assertSame( U::EN_DASH, $dashes->interval_dash() ); |
||
| 588 | $this->assertSame( ' ', $dashes->parenthetical_space() ); |
||
| 589 | $this->assertSame( U::HAIR_SPACE, $dashes->interval_space() ); |
||
| 590 | |||
| 591 | } |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Test set_smart_dashes_style with a Dashes object. |
||
| 595 | * |
||
| 596 | * @covers ::set_smart_dashes_style |
||
| 597 | * @covers ::get_style |
||
| 598 | */ |
||
| 599 | public function test_set_smart_dashes_style_with_object() { |
||
| 600 | $s = $this->settings; |
||
| 601 | |||
| 602 | // Create a stub for the Token_Fixer interface. |
||
| 603 | $fake_dashes = $this->createMock( Dashes::class ); |
||
| 604 | $fake_dashes->method( 'parenthetical_dash' )->willReturn( 'a' ); |
||
| 605 | $fake_dashes->method( 'parenthetical_space' )->willReturn( 'b' ); |
||
| 606 | $fake_dashes->method( 'interval_dash' )->willReturn( 'c' ); |
||
| 607 | $fake_dashes->method( 'interval_space' )->willReturn( 'd' ); |
||
| 608 | |||
| 609 | $s->set_smart_dashes_style( $fake_dashes ); |
||
| 610 | $dashes = $s->dash_style(); |
||
| 611 | |||
| 612 | $this->assertSame( 'a', $dashes->parenthetical_dash() ); |
||
| 613 | $this->assertSame( 'b', $dashes->parenthetical_space() ); |
||
| 614 | $this->assertSame( 'c', $dashes->interval_dash() ); |
||
| 615 | $this->assertSame( 'd', $dashes->interval_space() ); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Tests set_smart_dashes_style. |
||
| 620 | * |
||
| 621 | * @covers ::set_smart_dashes_style |
||
| 622 | * @covers ::get_style |
||
| 623 | * |
||
| 624 | * @uses PHP_Typography\Settings\Dash_Style::get_styled_dashes |
||
| 625 | * |
||
| 626 | * @expectedException \DomainException |
||
| 627 | * @expectedExceptionMessageRegExp /^Invalid dash style \w+.$/ |
||
| 628 | */ |
||
| 629 | public function test_set_smart_dashes_style_invalid() { |
||
| 630 | $s = $this->settings; |
||
| 631 | |||
| 632 | $s->set_smart_dashes_style( 'invalidStyleName' ); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Tests set_smart_ellipses. |
||
| 637 | * |
||
| 638 | * @covers ::set_smart_ellipses |
||
| 639 | */ |
||
| 640 | public function test_set_smart_ellipses() { |
||
| 641 | $this->settings->set_smart_ellipses( true ); |
||
| 642 | $this->assertTrue( $this->settings['smartEllipses'] ); |
||
| 643 | |||
| 644 | $this->settings->set_smart_ellipses( false ); |
||
| 645 | $this->assertFalse( $this->settings['smartEllipses'] ); |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Tests set_smart_diacritics. |
||
| 650 | * |
||
| 651 | * @covers ::set_smart_diacritics |
||
| 652 | */ |
||
| 653 | public function test_set_smart_diacritics() { |
||
| 654 | $this->settings->set_smart_diacritics( true ); |
||
| 655 | $this->assertTrue( $this->settings['smartDiacritics'] ); |
||
| 656 | |||
| 657 | $this->settings->set_smart_diacritics( false ); |
||
| 658 | $this->assertFalse( $this->settings['smartDiacritics'] ); |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Tests set_diacritic_language. |
||
| 663 | * |
||
| 664 | * @covers ::set_diacritic_language |
||
| 665 | * @covers ::update_diacritics_replacement_arrays |
||
| 666 | * @covers ::parse_diacritics_rules |
||
| 667 | */ |
||
| 668 | public function test_set_diacritic_language() { |
||
| 669 | $this->settings->set_diacritic_language( 'en-US' ); |
||
| 670 | $this->assertGreaterThan( 0, count( $this->settings['diacriticWords'] ) ); |
||
| 671 | |||
| 672 | $this->settings->set_diacritic_language( 'foobar' ); |
||
| 673 | $this->assertFalse( isset( $this->settings['diacriticWords'] ) ); |
||
| 674 | |||
| 675 | $this->settings->set_diacritic_language( 'de-DE' ); |
||
| 676 | $this->assertTrue( isset( $this->settings['diacriticWords'] ) ); |
||
| 677 | $this->assertGreaterThan( 0, count( $this->settings['diacriticWords'] ) ); |
||
| 678 | |||
| 679 | // Nothing changed since the last call. |
||
| 680 | $this->settings->set_diacritic_language( 'de-DE' ); |
||
| 681 | $this->assertTrue( isset( $this->settings['diacriticWords'] ) ); |
||
| 682 | $this->assertGreaterThan( 0, count( $this->settings['diacriticWords'] ) ); |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Provide data for testing set_diacritic_custom_replacements. |
||
| 687 | */ |
||
| 688 | public function provide_set_diacritic_custom_replacements_data() { |
||
| 689 | return [ |
||
| 690 | [ |
||
| 691 | '"foo" => "fóò", "bar" => "bâr"' . ", 'ha' => 'hä'", |
||
| 692 | [ 'foo', 'bar', 'ha' ], |
||
| 693 | [ 'fóò', 'bâr', 'hä' ], |
||
| 694 | ], |
||
| 695 | [ |
||
| 696 | '"fo\'o" => "fó\'ò", "bar" => "bâr"' . ", 'h\"a' => 'h\"ä'", |
||
| 697 | [ "fo'o", 'bar', 'h"a' ], |
||
| 698 | [ "fó'ò", 'bâr', 'h"ä' ], |
||
| 699 | ], |
||
| 700 | [ |
||
| 701 | [ |
||
| 702 | 'fööbar' => 'fúbar', |
||
| 703 | ], |
||
| 704 | [ 'fööbar' ], |
||
| 705 | [ 'fúbar' ], |
||
| 706 | ], |
||
| 707 | |||
| 708 | [ |
||
| 709 | [ |
||
| 710 | ' ' => 'fúbar', |
||
| 711 | ], |
||
| 712 | [], |
||
| 713 | [], |
||
| 714 | ], |
||
| 715 | |||
| 716 | [ |
||
| 717 | [ |
||
| 718 | 'fööbar' => '', |
||
| 719 | ], |
||
| 720 | [], |
||
| 721 | [], |
||
| 722 | ], |
||
| 723 | ]; |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Tests set_diacritic_custom_replacements. |
||
| 728 | * |
||
| 729 | * @covers ::set_diacritic_custom_replacements |
||
| 730 | * @covers ::parse_diacritics_replacement_string |
||
| 731 | * @covers ::update_diacritics_replacement_arrays |
||
| 732 | * @covers ::parse_diacritics_rules |
||
| 733 | * |
||
| 734 | * @uses PHP_Typography\Arrays::array_map_assoc |
||
| 735 | * |
||
| 736 | * @dataProvider provide_set_diacritic_custom_replacements_data |
||
| 737 | * |
||
| 738 | * @param string|array $input Custom replacements string or array. |
||
| 739 | * @param array $keys Expected keys. |
||
| 740 | * @param array $values Expected values. |
||
| 741 | */ |
||
| 742 | public function test_set_diacritic_custom_replacements( $input, array $keys, array $values ) { |
||
| 743 | $s = $this->settings; |
||
| 744 | |||
| 745 | $s->set_diacritic_custom_replacements( $input ); |
||
| 746 | |||
| 747 | foreach ( $keys as $key ) { |
||
| 748 | $this->assertArrayHasKey( $key, $s['diacriticCustomReplacements'] ); |
||
| 749 | } |
||
| 750 | |||
| 751 | foreach ( $values as $value ) { |
||
| 752 | $this->assertContains( $value, $s['diacriticCustomReplacements'] ); |
||
| 753 | } |
||
| 754 | |||
| 755 | $this->assertCount( count( $keys ), $s['diacriticCustomReplacements'] ); |
||
| 756 | $this->assertCount( count( $values ), $s['diacriticCustomReplacements'] ); |
||
| 757 | } |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Test set_smart_marks. |
||
| 761 | * |
||
| 762 | * @covers ::set_smart_marks |
||
| 763 | */ |
||
| 764 | public function test_set_smart_marks() { |
||
| 765 | $this->settings->set_smart_marks( true ); |
||
| 766 | $this->assertTrue( $this->settings['smartMarks'] ); |
||
| 767 | |||
| 768 | $this->settings->set_smart_marks( false ); |
||
| 769 | $this->assertFalse( $this->settings['smartMarks'] ); |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Tests set_smart_math. |
||
| 774 | * |
||
| 775 | * @covers ::set_smart_math |
||
| 776 | */ |
||
| 777 | public function test_set_smart_math() { |
||
| 778 | $this->settings->set_smart_math( true ); |
||
| 779 | $this->assertTrue( $this->settings['smartMath'] ); |
||
| 780 | |||
| 781 | $this->settings->set_smart_math( false ); |
||
| 782 | $this->assertFalse( $this->settings['smartMath'] ); |
||
| 783 | } |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Tests set_smart_exponents. |
||
| 787 | * |
||
| 788 | * @covers ::set_smart_exponents |
||
| 789 | */ |
||
| 790 | public function test_set_smart_exponents() { |
||
| 791 | $this->settings->set_smart_exponents( true ); |
||
| 792 | $this->assertTrue( $this->settings['smartExponents'] ); |
||
| 793 | |||
| 794 | $this->settings->set_smart_exponents( false ); |
||
| 795 | $this->assertFalse( $this->settings['smartExponents'] ); |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Tests set_smart_fractions. |
||
| 800 | * |
||
| 801 | * @covers ::set_smart_fractions |
||
| 802 | */ |
||
| 803 | public function test_set_smart_fractions() { |
||
| 804 | $this->settings->set_smart_fractions( true ); |
||
| 805 | $this->assertTrue( $this->settings['smartFractions'] ); |
||
| 806 | |||
| 807 | $this->settings->set_smart_fractions( false ); |
||
| 808 | $this->assertFalse( $this->settings['smartFractions'] ); |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Tests set_smart_ordinal_suffix. |
||
| 813 | * |
||
| 814 | * @covers ::set_smart_ordinal_suffix |
||
| 815 | */ |
||
| 816 | public function test_set_smart_ordinal_suffix() { |
||
| 817 | $this->settings->set_smart_ordinal_suffix( true ); |
||
| 818 | $this->assertTrue( $this->settings['smartOrdinalSuffix'] ); |
||
| 819 | |||
| 820 | $this->settings->set_smart_ordinal_suffix( false ); |
||
| 821 | $this->assertFalse( $this->settings['smartOrdinalSuffix'] ); |
||
| 822 | } |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Tests set_single_character_word_spacing. |
||
| 826 | * |
||
| 827 | * @covers ::set_single_character_word_spacing |
||
| 828 | */ |
||
| 829 | public function test_set_single_character_word_spacing() { |
||
| 830 | $this->settings->set_single_character_word_spacing( true ); |
||
| 831 | $this->assertTrue( $this->settings['singleCharacterWordSpacing'] ); |
||
| 832 | |||
| 833 | $this->settings->set_single_character_word_spacing( false ); |
||
| 834 | $this->assertFalse( $this->settings['singleCharacterWordSpacing'] ); |
||
| 835 | } |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Tests set_fraction_spacing. |
||
| 839 | * |
||
| 840 | * @covers ::set_fraction_spacing |
||
| 841 | */ |
||
| 842 | public function test_set_fraction_spacing() { |
||
| 843 | $this->settings->set_fraction_spacing( true ); |
||
| 844 | $this->assertTrue( $this->settings['fractionSpacing'] ); |
||
| 845 | |||
| 846 | $this->settings->set_fraction_spacing( false ); |
||
| 847 | $this->assertFalse( $this->settings['fractionSpacing'] ); |
||
| 848 | } |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Tests set_unit_spacing. |
||
| 852 | * |
||
| 853 | * @covers ::set_unit_spacing |
||
| 854 | */ |
||
| 855 | public function test_set_unit_spacing() { |
||
| 856 | $this->settings->set_unit_spacing( true ); |
||
| 857 | $this->assertTrue( $this->settings['unitSpacing'] ); |
||
| 858 | |||
| 859 | $this->settings->set_unit_spacing( false ); |
||
| 860 | $this->assertFalse( $this->settings['unitSpacing'] ); |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Tests set_numbered_abbreviation_spacing. |
||
| 865 | * |
||
| 866 | * @covers ::set_numbered_abbreviation_spacing |
||
| 867 | */ |
||
| 868 | public function test_set_numbered_abbreviation_spacing() { |
||
| 869 | $this->settings->set_numbered_abbreviation_spacing( true ); |
||
| 870 | $this->assertTrue( $this->settings['numberedAbbreviationSpacing'] ); |
||
| 871 | |||
| 872 | $this->settings->set_numbered_abbreviation_spacing( false ); |
||
| 873 | $this->assertFalse( $this->settings['numberedAbbreviationSpacing'] ); |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Tests set_french_punctuation_spacing. |
||
| 878 | * |
||
| 879 | * @covers ::set_french_punctuation_spacing |
||
| 880 | */ |
||
| 881 | public function test_set_french_punctuation_spacing() { |
||
| 882 | $this->settings->set_french_punctuation_spacing( true ); |
||
| 883 | $this->assertTrue( $this->settings['frenchPunctuationSpacing'] ); |
||
| 884 | |||
| 885 | $this->settings->set_french_punctuation_spacing( false ); |
||
| 886 | $this->assertFalse( $this->settings['frenchPunctuationSpacing'] ); |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Tests set_units. |
||
| 891 | * |
||
| 892 | * @covers ::set_units |
||
| 893 | * @covers ::update_unit_pattern |
||
| 894 | * |
||
| 895 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 896 | */ |
||
| 897 | public function test_set_units() { |
||
| 898 | $units_as_array = [ 'foo', 'bar', 'xx/yy' ]; |
||
| 899 | $units_as_string = implode( ', ', $units_as_array ); |
||
| 900 | |||
| 901 | $this->settings->set_units( $units_as_array ); |
||
| 902 | foreach ( $units_as_array as $unit ) { |
||
| 903 | $this->assertContains( $unit, $this->settings['units'] ); |
||
| 904 | } |
||
| 905 | |||
| 906 | $this->settings->set_units( [] ); |
||
| 907 | foreach ( $units_as_array as $unit ) { |
||
| 908 | $this->assertNotContains( $unit, $this->settings['units'] ); |
||
| 909 | } |
||
| 910 | |||
| 911 | $this->settings->set_units( $units_as_string ); |
||
| 912 | foreach ( $units_as_array as $unit ) { |
||
| 913 | $this->assertContains( $unit, $this->settings['units'] ); |
||
| 914 | } |
||
| 915 | } |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Tests set_dash_spacing. |
||
| 919 | * |
||
| 920 | * @covers ::set_dash_spacing |
||
| 921 | */ |
||
| 922 | public function test_set_dash_spacing() { |
||
| 923 | $this->settings->set_dash_spacing( true ); |
||
| 924 | $this->assertTrue( $this->settings['dashSpacing'] ); |
||
| 925 | |||
| 926 | $this->settings->set_dash_spacing( false ); |
||
| 927 | $this->assertFalse( $this->settings['dashSpacing'] ); |
||
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Tests set_space_collapse. |
||
| 932 | * |
||
| 933 | * @covers ::set_space_collapse |
||
| 934 | */ |
||
| 935 | public function test_set_space_collapse() { |
||
| 936 | $this->settings->set_space_collapse( true ); |
||
| 937 | $this->assertTrue( $this->settings['spaceCollapse'] ); |
||
| 938 | |||
| 939 | $this->settings->set_space_collapse( false ); |
||
| 940 | $this->assertFalse( $this->settings['spaceCollapse'] ); |
||
| 941 | } |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Tests set_dewidow. |
||
| 945 | * |
||
| 946 | * @covers ::set_dewidow |
||
| 947 | */ |
||
| 948 | public function test_set_dewidow() { |
||
| 949 | $this->settings->set_dewidow( true ); |
||
| 950 | $this->assertTrue( $this->settings['dewidow'] ); |
||
| 951 | |||
| 952 | $this->settings->set_dewidow( false ); |
||
| 953 | $this->assertFalse( $this->settings['dewidow'] ); |
||
| 954 | } |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Tests set_max_dewidow_length. |
||
| 958 | * |
||
| 959 | * @covers ::set_max_dewidow_length |
||
| 960 | */ |
||
| 961 | public function test_set_max_dewidow_length() { |
||
| 962 | $this->settings->set_max_dewidow_length( 10 ); |
||
| 963 | $this->assertSame( 10, $this->settings['dewidowMaxLength'] ); |
||
| 964 | |||
| 965 | $this->settings->set_max_dewidow_length( 1 ); |
||
| 966 | $this->assertSame( 5, $this->settings['dewidowMaxLength'] ); |
||
| 967 | |||
| 968 | $this->settings->set_max_dewidow_length( 2 ); |
||
| 969 | $this->assertSame( 2, $this->settings['dewidowMaxLength'] ); |
||
| 970 | } |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Tests set_max_dewidow_pull. |
||
| 974 | * |
||
| 975 | * @covers ::set_max_dewidow_pull |
||
| 976 | */ |
||
| 977 | public function test_set_max_dewidow_pull() { |
||
| 978 | $this->settings->set_max_dewidow_pull( 10 ); |
||
| 979 | $this->assertSame( 10, $this->settings['dewidowMaxPull'] ); |
||
| 980 | |||
| 981 | $this->settings->set_max_dewidow_pull( 1 ); |
||
| 982 | $this->assertSame( 5, $this->settings['dewidowMaxPull'] ); |
||
| 983 | |||
| 984 | $this->settings->set_max_dewidow_pull( 2 ); |
||
| 985 | $this->assertSame( 2, $this->settings['dewidowMaxPull'] ); |
||
| 986 | } |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Tests set_wrap_hard_hyphens. |
||
| 990 | * |
||
| 991 | * @covers ::set_wrap_hard_hyphens |
||
| 992 | */ |
||
| 993 | public function test_set_wrap_hard_hyphens() { |
||
| 994 | $this->settings->set_wrap_hard_hyphens( true ); |
||
| 995 | $this->assertTrue( $this->settings['hyphenHardWrap'] ); |
||
| 996 | |||
| 997 | $this->settings->set_wrap_hard_hyphens( false ); |
||
| 998 | $this->assertFalse( $this->settings['hyphenHardWrap'] ); |
||
| 999 | } |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Tests set_url_wrap. |
||
| 1003 | * |
||
| 1004 | * @covers ::set_url_wrap |
||
| 1005 | */ |
||
| 1006 | public function test_set_url_wrap() { |
||
| 1007 | $this->settings->set_url_wrap( true ); |
||
| 1008 | $this->assertTrue( $this->settings['urlWrap'] ); |
||
| 1009 | |||
| 1010 | $this->settings->set_url_wrap( false ); |
||
| 1011 | $this->assertFalse( $this->settings['urlWrap'] ); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Tests set_email_wrap. |
||
| 1016 | * |
||
| 1017 | * @covers ::set_email_wrap |
||
| 1018 | */ |
||
| 1019 | public function test_set_email_wrap() { |
||
| 1020 | $this->settings->set_email_wrap( true ); |
||
| 1021 | $this->assertTrue( $this->settings['emailWrap'] ); |
||
| 1022 | |||
| 1023 | $this->settings->set_email_wrap( false ); |
||
| 1024 | $this->assertFalse( $this->settings['emailWrap'] ); |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Tests set_min_after_url_wrap. |
||
| 1029 | * |
||
| 1030 | * @covers ::set_min_after_url_wrap |
||
| 1031 | */ |
||
| 1032 | public function test_set_min_after_url_wrap() { |
||
| 1033 | $this->settings->set_min_after_url_wrap( 10 ); |
||
| 1034 | $this->assertSame( 10, $this->settings['urlMinAfterWrap'] ); |
||
| 1035 | |||
| 1036 | $this->settings->set_min_after_url_wrap( 0 ); |
||
| 1037 | $this->assertSame( 5, $this->settings['urlMinAfterWrap'] ); |
||
| 1038 | |||
| 1039 | $this->settings->set_min_after_url_wrap( 1 ); |
||
| 1040 | $this->assertSame( 1, $this->settings['urlMinAfterWrap'] ); |
||
| 1041 | } |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Tests set_style_ampersands. |
||
| 1045 | * |
||
| 1046 | * @covers ::set_style_ampersands |
||
| 1047 | */ |
||
| 1048 | public function test_set_style_ampersands() { |
||
| 1049 | $this->settings->set_style_ampersands( true ); |
||
| 1050 | $this->assertTrue( $this->settings['styleAmpersands'] ); |
||
| 1051 | |||
| 1052 | $this->settings->set_style_ampersands( false ); |
||
| 1053 | $this->assertFalse( $this->settings['styleAmpersands'] ); |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Tests set_style_caps. |
||
| 1058 | * |
||
| 1059 | * @covers ::set_style_caps |
||
| 1060 | */ |
||
| 1061 | public function test_set_style_caps() { |
||
| 1062 | $this->settings->set_style_caps( true ); |
||
| 1063 | $this->assertTrue( $this->settings['styleCaps'] ); |
||
| 1064 | |||
| 1065 | $this->settings->set_style_caps( false ); |
||
| 1066 | $this->assertFalse( $this->settings['styleCaps'] ); |
||
| 1067 | } |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Tests set_style_initial_quotes. |
||
| 1071 | * |
||
| 1072 | * @covers ::set_style_initial_quotes |
||
| 1073 | */ |
||
| 1074 | public function test_set_style_initial_quotes() { |
||
| 1075 | $this->settings->set_style_initial_quotes( true ); |
||
| 1076 | $this->assertTrue( $this->settings['styleInitialQuotes'] ); |
||
| 1077 | |||
| 1078 | $this->settings->set_style_initial_quotes( false ); |
||
| 1079 | $this->assertFalse( $this->settings['styleInitialQuotes'] ); |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Tests set_style_numbers. |
||
| 1084 | * |
||
| 1085 | * @covers ::set_style_numbers |
||
| 1086 | */ |
||
| 1087 | public function test_set_style_numbers() { |
||
| 1088 | $this->settings->set_style_numbers( true ); |
||
| 1089 | $this->assertTrue( $this->settings['styleNumbers'] ); |
||
| 1090 | |||
| 1091 | $this->settings->set_style_numbers( false ); |
||
| 1092 | $this->assertFalse( $this->settings['styleNumbers'] ); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Tests set_style_hanging_punctuation. |
||
| 1097 | * |
||
| 1098 | * @covers ::set_style_hanging_punctuation |
||
| 1099 | */ |
||
| 1100 | public function test_set_style_hanging_punctuation() { |
||
| 1101 | $this->settings->set_style_hanging_punctuation( true ); |
||
| 1102 | $this->assertTrue( $this->settings['styleHangingPunctuation'] ); |
||
| 1103 | |||
| 1104 | $this->settings->set_style_hanging_punctuation( false ); |
||
| 1105 | $this->assertFalse( $this->settings['styleHangingPunctuation'] ); |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Tests set_initial_quote_tags. |
||
| 1110 | * |
||
| 1111 | * @covers ::set_initial_quote_tags |
||
| 1112 | */ |
||
| 1113 | public function test_set_initial_quote_tags() { |
||
| 1114 | $tags_as_array = [ 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'div' ]; |
||
| 1115 | $tags_as_string = implode( ', ', $tags_as_array ); |
||
| 1116 | |||
| 1117 | $this->settings->set_initial_quote_tags( $tags_as_array ); |
||
| 1118 | foreach ( $tags_as_array as $tag ) { |
||
| 1119 | $this->assertArrayHasKey( $tag, $this->settings['initialQuoteTags'] ); |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | $this->settings->set_initial_quote_tags( [] ); |
||
| 1123 | foreach ( $tags_as_array as $tag ) { |
||
| 1124 | $this->assertArrayNotHasKey( $tag, $this->settings['initialQuoteTags'] ); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | $this->settings->set_initial_quote_tags( $tags_as_string ); |
||
| 1128 | foreach ( $tags_as_array as $tag ) { |
||
| 1129 | $this->assertArrayHasKey( $tag, $this->settings['initialQuoteTags'] ); |
||
| 1130 | } |
||
| 1131 | } |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Tests set_hyphenation. |
||
| 1135 | * |
||
| 1136 | * @covers ::set_hyphenation |
||
| 1137 | */ |
||
| 1138 | public function test_set_hyphenation() { |
||
| 1139 | $this->settings->set_hyphenation( true ); |
||
| 1140 | $this->assertTrue( $this->settings['hyphenation'] ); |
||
| 1141 | |||
| 1142 | $this->settings->set_hyphenation( false ); |
||
| 1143 | $this->assertFalse( $this->settings['hyphenation'] ); |
||
| 1144 | } |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Provide data for set_hyphenation_language testing. |
||
| 1148 | * |
||
| 1149 | * @return array |
||
| 1150 | */ |
||
| 1151 | public function provide_hyphenation_language_data() { |
||
| 1152 | return [ |
||
| 1153 | [ 'en-US', true ], |
||
| 1154 | [ 'foobar', false ], |
||
| 1155 | [ 'no', true ], |
||
| 1156 | [ 'de', true ], |
||
| 1157 | ]; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | /** |
||
| 1161 | * Tests set_hyphenation_language. |
||
| 1162 | * |
||
| 1163 | * @covers ::set_hyphenation_language |
||
| 1164 | * |
||
| 1165 | * @uses PHP_Typography\Hyphenator::__construct |
||
| 1166 | * @uses PHP_Typography\Hyphenator::set_language |
||
| 1167 | * |
||
| 1168 | * @dataProvider provide_hyphenation_language_data |
||
| 1169 | * |
||
| 1170 | * @param string $lang Language code. |
||
| 1171 | * @param bool $success Expected success status. |
||
| 1172 | */ |
||
| 1173 | public function test_set_hyphenation_language( $lang, $success ) { |
||
| 1174 | $s = $this->settings; |
||
| 1175 | $s['hyphenationExceptions'] = []; // necessary for full coverage. |
||
| 1176 | |||
| 1177 | $s->set_hyphenation_language( $lang ); |
||
| 1178 | |||
| 1179 | // If the hyphenator object has not instantiated yet, hyphenLanguage will be set nonetheless. |
||
| 1180 | if ( $success || ! isset( $s->hyphenator ) ) { |
||
| 1181 | $this->assertSame( $lang, $s['hyphenLanguage'] ); |
||
| 1182 | } else { |
||
| 1183 | $this->assertFalse( isset( $s['hyphenLanguage'] ) ); |
||
| 1184 | } |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | /** |
||
| 1188 | * Tests set_hyphenation_language. |
||
| 1189 | * |
||
| 1190 | * @covers ::set_hyphenation_language |
||
| 1191 | * |
||
| 1192 | * @uses PHP_Typography\Hyphenator::__construct |
||
| 1193 | * @uses PHP_Typography\Hyphenator::set_language |
||
| 1194 | * @uses PHP_Typography\Hyphenator\Trie_Node |
||
| 1195 | * |
||
| 1196 | * @dataProvider provide_hyphenation_language_data |
||
| 1197 | * |
||
| 1198 | * @param string $lang Language code. |
||
| 1199 | * @param bool $success Expected success status. |
||
| 1200 | */ |
||
| 1201 | public function test_set_hyphenation_language_again( $lang, $success ) { |
||
| 1202 | $s = $this->settings; |
||
| 1203 | $s['hyphenationExceptions'] = []; // necessary for full coverage. |
||
| 1204 | |||
| 1205 | for ( $i = 0; $i < 2; ++$i ) { |
||
| 1206 | $s->set_hyphenation_language( $lang ); |
||
| 1207 | |||
| 1208 | // If the hyphenator object has not instantiated yet, hyphenLanguage will be set nonetheless. |
||
| 1209 | if ( $success ) { |
||
| 1210 | $this->assertSame( $lang, $s['hyphenLanguage'], "Round $i, success" ); |
||
| 1211 | } elseif ( ! isset( $s->hyphenator ) ) { |
||
| 1212 | $this->assertSame( $lang, $s['hyphenLanguage'], "Round $i, no hyphenator" ); |
||
| 1213 | // Clear hyphenation language if there was no hypehnator object. |
||
| 1214 | unset( $s['hyphenLanguage'] ); |
||
| 1215 | } else { |
||
| 1216 | $this->assertFalse( isset( $s['hyphenLanguage'] ), "Round $i, unsuccessful" ); |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Tests set_min_length_hyphenation. |
||
| 1224 | * |
||
| 1225 | * @covers ::set_min_length_hyphenation |
||
| 1226 | * |
||
| 1227 | * @uses PHP_Typography\Hyphenator::__construct |
||
| 1228 | */ |
||
| 1229 | public function test_set_min_length_hyphenation() { |
||
| 1230 | $this->settings->set_min_length_hyphenation( 1 ); // too low, resets to default 5. |
||
| 1231 | $this->assertSame( 5, $this->settings['hyphenMinLength'] ); |
||
| 1232 | |||
| 1233 | $this->settings->set_min_length_hyphenation( 2 ); |
||
| 1234 | $this->assertSame( 2, $this->settings['hyphenMinLength'] ); |
||
| 1235 | |||
| 1236 | $this->settings->set_min_length_hyphenation( 66 ); |
||
| 1237 | $this->assertSame( 66, $this->settings['hyphenMinLength'] ); |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * Tests set_min_before_hyphenation. |
||
| 1242 | * |
||
| 1243 | * @covers ::set_min_before_hyphenation |
||
| 1244 | */ |
||
| 1245 | public function test_set_min_before_hyphenation() { |
||
| 1246 | $this->settings->set_min_before_hyphenation( 0 ); // too low, resets to default 3. |
||
| 1247 | $this->assertSame( 3, $this->settings['hyphenMinBefore'] ); |
||
| 1248 | |||
| 1249 | $this->settings->set_min_before_hyphenation( 1 ); |
||
| 1250 | $this->assertSame( 1, $this->settings['hyphenMinBefore'] ); |
||
| 1251 | |||
| 1252 | $this->settings->set_min_before_hyphenation( 66 ); |
||
| 1253 | $this->assertSame( 66, $this->settings['hyphenMinBefore'] ); |
||
| 1254 | |||
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Tests set_min_after_hyphenation. |
||
| 1259 | * |
||
| 1260 | * @covers ::set_min_after_hyphenation |
||
| 1261 | */ |
||
| 1262 | public function test_set_min_after_hyphenation() { |
||
| 1263 | $this->settings->set_min_after_hyphenation( 0 ); // too low, resets to default 2. |
||
| 1264 | $this->assertSame( 2, $this->settings['hyphenMinAfter'] ); |
||
| 1265 | |||
| 1266 | $this->settings->set_min_after_hyphenation( 1 ); |
||
| 1267 | $this->assertSame( 1, $this->settings['hyphenMinAfter'] ); |
||
| 1268 | |||
| 1269 | $this->settings->set_min_after_hyphenation( 66 ); |
||
| 1270 | $this->assertSame( 66, $this->settings['hyphenMinAfter'] ); |
||
| 1271 | } |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Tests set_hyphenate_headings. |
||
| 1275 | * |
||
| 1276 | * @covers ::set_hyphenate_headings |
||
| 1277 | */ |
||
| 1278 | public function test_set_hyphenate_headings() { |
||
| 1279 | $this->settings->set_hyphenate_headings( true ); |
||
| 1280 | $this->assertTrue( $this->settings['hyphenateTitle'] ); |
||
| 1281 | |||
| 1282 | $this->settings->set_hyphenate_headings( false ); |
||
| 1283 | $this->assertFalse( $this->settings['hyphenateTitle'] ); |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Tests set_hyphenate_all_caps. |
||
| 1288 | * |
||
| 1289 | * @covers ::set_hyphenate_all_caps |
||
| 1290 | */ |
||
| 1291 | public function test_set_hyphenate_all_caps() { |
||
| 1292 | $this->settings->set_hyphenate_all_caps( true ); |
||
| 1293 | $this->assertTrue( $this->settings['hyphenateAllCaps'] ); |
||
| 1294 | |||
| 1295 | $this->settings->set_hyphenate_all_caps( false ); |
||
| 1296 | $this->assertFalse( $this->settings['hyphenateAllCaps'] ); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Tests set_hyphenate_title_case. |
||
| 1301 | * |
||
| 1302 | * @covers ::set_hyphenate_title_case |
||
| 1303 | */ |
||
| 1304 | public function test_set_hyphenate_title_case() { |
||
| 1305 | $this->settings->set_hyphenate_title_case( true ); |
||
| 1306 | $this->assertTrue( $this->settings['hyphenateTitleCase'] ); |
||
| 1307 | |||
| 1308 | $this->settings->set_hyphenate_title_case( false ); |
||
| 1309 | $this->assertFalse( $this->settings['hyphenateTitleCase'] ); |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Tests set_hyphenate_compounds. |
||
| 1314 | * |
||
| 1315 | * @covers ::set_hyphenate_compounds |
||
| 1316 | */ |
||
| 1317 | public function test_set_hyphenate_compounds() { |
||
| 1318 | $this->settings->set_hyphenate_compounds( true ); |
||
| 1319 | $this->assertTrue( $this->settings['hyphenateCompounds'] ); |
||
| 1320 | |||
| 1321 | $this->settings->set_hyphenate_compounds( false ); |
||
| 1322 | $this->assertFalse( $this->settings['hyphenateCompounds'] ); |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Tests set_hyphenation_exceptions. |
||
| 1327 | * |
||
| 1328 | * @covers ::set_hyphenation_exceptions |
||
| 1329 | * |
||
| 1330 | * @uses PHP_Typography\Hyphenator::__construct |
||
| 1331 | * @uses PHP_Typography\Hyphenator::set_custom_exceptions |
||
| 1332 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 1333 | */ |
||
| 1334 | public function test_set_hyphenation_exceptions_array() { |
||
| 1335 | $s = $this->settings; |
||
| 1336 | |||
| 1337 | $exceptions = [ 'Hu-go', 'Fö-ba-ß' ]; |
||
| 1338 | $s->set_hyphenation_exceptions( $exceptions ); |
||
| 1339 | $this->assertContainsOnly( 'string', $s['hyphenationCustomExceptions'] ); |
||
| 1340 | $this->assertCount( 2, $s['hyphenationCustomExceptions'] ); |
||
| 1341 | |||
| 1342 | $exceptions = [ 'bar-foo' ]; |
||
| 1343 | $s->set_hyphenation_exceptions( $exceptions ); |
||
| 1344 | $this->assertContainsOnly( 'string', $s['hyphenationCustomExceptions'] ); |
||
| 1345 | $this->assertCount( 1, $s['hyphenationCustomExceptions'] ); |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Tests set_hyphenation_exceptions. |
||
| 1350 | * |
||
| 1351 | * @covers ::set_hyphenation_exceptions |
||
| 1352 | * |
||
| 1353 | * @uses PHP_Typography\Hyphenator::__construct |
||
| 1354 | * @uses PHP_Typography\Hyphenator::set_custom_exceptions |
||
| 1355 | * @uses PHP_Typography\Strings::maybe_split_parameters |
||
| 1356 | */ |
||
| 1357 | public function test_set_hyphenation_exceptions_string() { |
||
| 1358 | $s = $this->settings; |
||
| 1359 | $exceptions = 'Hu-go, Fö-ba-ß'; |
||
| 1360 | |||
| 1361 | $s->set_hyphenation_exceptions( $exceptions ); |
||
| 1362 | $this->assertContainsOnly( 'string', $s['hyphenationCustomExceptions'] ); |
||
| 1363 | $this->assertCount( 2, $s['hyphenationCustomExceptions'] ); |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Tests get_hash. |
||
| 1368 | * |
||
| 1369 | * @covers ::get_hash |
||
| 1370 | */ |
||
| 1371 | public function test_get_hash() { |
||
| 1372 | $s = $this->settings; |
||
| 1373 | |||
| 1374 | $s->set_smart_quotes( true ); |
||
| 1375 | $hash1 = $s->get_hash( 10 ); |
||
| 1376 | $this->assertEquals( 10, strlen( $hash1 ) ); |
||
| 1377 | |||
| 1378 | $s->set_smart_quotes( false ); |
||
| 1379 | $hash2 = $s->get_hash( 10 ); |
||
| 1380 | $this->assertEquals( 10, strlen( $hash2 ) ); |
||
| 1381 | |||
| 1382 | $this->assertNotEquals( $hash1, $hash2 ); |
||
| 1383 | } |
||
| 1384 | |||
| 1385 | /** |
||
| 1386 | * Tests set_true_no_break_narrow_space and no_break_narrow_space. |
||
| 1387 | * |
||
| 1388 | * @covers ::set_true_no_break_narrow_space |
||
| 1389 | * @covers ::no_break_narrow_space |
||
| 1390 | */ |
||
| 1391 | public function test_set_true_no_break_narrow_space() { |
||
| 1392 | $s = $this->settings; |
||
| 1393 | |||
| 1394 | $s->set_true_no_break_narrow_space(); // defaults to false. |
||
| 1395 | $this->assertSame( $s->no_break_narrow_space(), U::NO_BREAK_SPACE ); |
||
| 1396 | |||
| 1397 | $s->set_true_no_break_narrow_space( true ); // defaults to false. |
||
| 1398 | $this->assertSame( $s->no_break_narrow_space(), U::NO_BREAK_NARROW_SPACE ); |
||
| 1399 | } |
||
| 1400 | } |
||
| 1401 |