| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\String\Tests; |
| 13 |
|
| 14 |
use PHPUnit\Framework\TestCase; |
| 15 |
use Symfony\Component\String\AbstractString; |
| 16 |
use Symfony\Component\String\ByteString; |
| 17 |
use Symfony\Component\String\CodePointString; |
| 18 |
use Symfony\Component\String\Exception\InvalidArgumentException; |
| 19 |
use Symfony\Component\String\UnicodeString; |
| 20 |
|
| 21 |
abstract class AbstractAsciiTestCase extends TestCase |
| 22 |
{ |
| 23 |
protected static function createFromString(string $string): AbstractString |
| 24 |
{ |
| 25 |
throw new \BadMethodCallException('This method must be implemented by subclasses.'); |
| 26 |
} |
| 27 |
|
| 28 |
public function testCreateFromString() |
| 29 |
{ |
| 30 |
$bytes = static::createFromString('Symfony is a PHP framework!'); |
| 31 |
|
| 32 |
$this->assertSame('Symfony is a PHP framework!', (string) $bytes); |
| 33 |
$this->assertSame(27, $bytes->length()); |
| 34 |
$this->assertFalse($bytes->isEmpty()); |
| 35 |
} |
| 36 |
|
| 37 |
public function testCreateFromEmptyString() |
| 38 |
{ |
| 39 |
$instance = static::createFromString(''); |
| 40 |
|
| 41 |
$this->assertSame('', (string) $instance); |
| 42 |
$this->assertSame(0, $instance->length()); |
| 43 |
$this->assertTrue($instance->isEmpty()); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @dataProvider provideBytesAt |
| 48 |
*/ |
| 49 |
public function testBytesAt(array $expected, string $string, int $offset, int $form = null) |
| 50 |
{ |
| 51 |
if (2 !== grapheme_strlen('च्छे') && 'नमस्ते' === $string) { |
| 52 |
$this->markTestSkipped('Skipping due to issue ICU-21661.'); |
| 53 |
} |
| 54 |
|
| 55 |
$instance = static::createFromString($string); |
| 56 |
$instance = $form ? $instance->normalize($form) : $instance; |
| 57 |
|
| 58 |
$this->assertSame($expected, $instance->bytesAt($offset)); |
| 59 |
} |
| 60 |
|
| 61 |
public static function provideBytesAt(): array |
| 62 |
{ |
| 63 |
return [ |
| 64 |
[[], '', 0], |
| 65 |
[[], 'a', 1], |
| 66 |
[[0x62], 'abc', 1], |
| 67 |
[[0x63], 'abcde', -3], |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @dataProvider provideIndexOf |
| 73 |
*/ |
| 74 |
public function testContainsAny(?int $result, string $string, $needle) |
| 75 |
{ |
| 76 |
$instance = static::createFromString($string); |
| 77 |
|
| 78 |
$this->assertSame(null !== $instance->indexOf($needle), $instance->containsAny($needle)); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @dataProvider provideIndexOfIgnoreCase |
| 83 |
*/ |
| 84 |
public function testContainsAnyIgnoreCase(?int $result, string $string, $needle) |
| 85 |
{ |
| 86 |
$instance = static::createFromString($string); |
| 87 |
|
| 88 |
$this->assertSame(null !== $instance->ignoreCase()->indexOf($needle), $instance->ignoreCase()->containsAny($needle)); |
| 89 |
} |
| 90 |
|
| 91 |
public function testUnwrap() |
| 92 |
{ |
| 93 |
$expected = ['hello', 'world']; |
| 94 |
|
| 95 |
$s = static::createFromString(''); |
| 96 |
|
| 97 |
$actual = $s::unwrap([static::createFromString('hello'), static::createFromString('world')]); |
| 98 |
|
| 99 |
$this->assertEquals($expected, $actual); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @dataProvider wordwrapProvider |
| 104 |
*/ |
| 105 |
public function testWordwrap($expected, $actual, $length, $break, $cut = false) |
| 106 |
{ |
| 107 |
$instance = static::createFromString($actual); |
| 108 |
$actual = $instance->wordwrap($length, $break, $cut); |
| 109 |
|
| 110 |
$this->assertEquals($expected, $actual); |
| 111 |
} |
| 112 |
|
| 113 |
public function wordwrapProvider() |
| 114 |
{ |
| 115 |
return [ |
| 116 |
[ |
| 117 |
'Lo-re-m-Ip-su-m', |
| 118 |
'Lorem Ipsum', |
| 119 |
2, |
| 120 |
'-', |
| 121 |
true, |
| 122 |
], |
| 123 |
[ |
| 124 |
'Lorem-Ipsum', |
| 125 |
'Lorem Ipsum', |
| 126 |
2, |
| 127 |
'-', |
| 128 |
], |
| 129 |
[ |
| 130 |
'Lor-em-Ips-um', |
| 131 |
'Lorem Ipsum', |
| 132 |
3, |
| 133 |
'-', |
| 134 |
true, |
| 135 |
], |
| 136 |
[ |
| 137 |
'L-o-r-e-m-I-p-s-u-m', |
| 138 |
'Lorem Ipsum', |
| 139 |
1, |
| 140 |
'-', |
| 141 |
true, |
| 142 |
], |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @dataProvider provideWrap |
| 148 |
*/ |
| 149 |
public function testWrap(array $expected, array $values) |
| 150 |
{ |
| 151 |
$s = static::createFromString(''); |
| 152 |
|
| 153 |
$this->assertEquals($expected, $s::wrap($values)); |
| 154 |
} |
| 155 |
|
| 156 |
public static function provideWrap(): array |
| 157 |
{ |
| 158 |
return [ |
| 159 |
[[], []], |
| 160 |
[ |
| 161 |
['abc' => static::createFromString('foo'), 1, static::createFromString('bar'), 'baz' => true], |
| 162 |
['abc' => 'foo', 1, 'bar', 'baz' => true], |
| 163 |
], |
| 164 |
[ |
| 165 |
['a' => ['b' => static::createFromString('c'), [static::createFromString('d')]], static::createFromString('e')], |
| 166 |
['a' => ['b' => 'c', ['d']], 'e'], |
| 167 |
], |
| 168 |
]; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @dataProvider provideLength |
| 173 |
*/ |
| 174 |
public function testLength(int $length, string $string) |
| 175 |
{ |
| 176 |
if (2 !== grapheme_strlen('च्छे') && '� |
| 177 |
नुच्छेद' === $string) { |
| 178 |
$this->markTestSkipped('Skipping due to issue ICU-21661.'); |
| 179 |
} |
| 180 |
|
| 181 |
$instance = static::createFromString($string); |
| 182 |
|
| 183 |
$this->assertSame($length, $instance->length()); |
| 184 |
} |
| 185 |
|
| 186 |
public static function provideLength(): array |
| 187 |
{ |
| 188 |
return [ |
| 189 |
[1, 'a'], |
| 190 |
[2, 'is'], |
| 191 |
[3, 'PHP'], |
| 192 |
[4, 'Java'], |
| 193 |
[7, 'Symfony'], |
| 194 |
[10, 'pineapples'], |
| 195 |
[22, 'Symfony is super cool!'], |
| 196 |
]; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* @dataProvider provideIndexOf |
| 201 |
*/ |
| 202 |
public function testIndexOf(?int $result, string $string, $needle, int $offset) |
| 203 |
{ |
| 204 |
$instance = static::createFromString($string); |
| 205 |
|
| 206 |
$this->assertSame($result, $instance->indexOf($needle, $offset)); |
| 207 |
} |
| 208 |
|
| 209 |
public static function provideIndexOf(): array |
| 210 |
{ |
| 211 |
return [ |
| 212 |
[null, 'abc', '', 0], |
| 213 |
[null, 'ABC', '', 0], |
| 214 |
[null, 'abc', 'd', 0], |
| 215 |
[null, 'abc', 'a', 3], |
| 216 |
[null, 'ABC', 'c', 0], |
| 217 |
[null, 'ABC', 'c', 2], |
| 218 |
[null, 'abc', 'a', -1], |
| 219 |
[null, '123abc', 'B', -3], |
| 220 |
[null, '123abc', 'b', 6], |
| 221 |
[0, 'abc', ['a', 'e'], 0], |
| 222 |
[0, 'abc', 'a', 0], |
| 223 |
[1, 'abc', 'b', 1], |
| 224 |
[2, 'abc', 'c', 1], |
| 225 |
[4, 'abacabab', 'ab', 1], |
| 226 |
[4, '123abc', 'b', -3], |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @dataProvider provideIndexOfIgnoreCase |
| 232 |
*/ |
| 233 |
public function testIndexOfIgnoreCase(?int $result, string $string, $needle, int $offset) |
| 234 |
{ |
| 235 |
$instance = static::createFromString($string); |
| 236 |
|
| 237 |
$this->assertSame($result, $instance->ignoreCase()->indexOf($needle, $offset)); |
| 238 |
} |
| 239 |
|
| 240 |
public static function provideIndexOfIgnoreCase(): array |
| 241 |
{ |
| 242 |
return [ |
| 243 |
[null, 'ABC', '', 0], |
| 244 |
[null, 'ABC', '', 0], |
| 245 |
[null, 'abc', 'a', 3], |
| 246 |
[null, 'abc', 'A', 3], |
| 247 |
[null, 'abc', 'a', -1], |
| 248 |
[null, 'abc', 'A', -1], |
| 249 |
[null, '123abc', 'B', 6], |
| 250 |
[0, 'ABC', ['a', 'e'], 0], |
| 251 |
[0, 'ABC', 'a', 0], |
| 252 |
[0, 'ABC', 'A', 0], |
| 253 |
[1, 'ABC', 'b', 0], |
| 254 |
[1, 'ABC', 'b', 1], |
| 255 |
[2, 'ABC', 'c', 0], |
| 256 |
[2, 'ABC', 'c', 2], |
| 257 |
[4, 'ABACaBAB', 'Ab', 1], |
| 258 |
[4, '123abc', 'B', -3], |
| 259 |
]; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* @dataProvider provideIndexOfLast |
| 264 |
*/ |
| 265 |
public function testIndexOfLast(?int $result, string $string, $needle, int $offset) |
| 266 |
{ |
| 267 |
$instance = static::createFromString($string); |
| 268 |
|
| 269 |
$this->assertSame($result, $instance->indexOfLast($needle, $offset)); |
| 270 |
} |
| 271 |
|
| 272 |
public static function provideIndexOfLast(): array |
| 273 |
{ |
| 274 |
return [ |
| 275 |
[null, 'abc', '', 0], |
| 276 |
[null, 'abc', '', -2], |
| 277 |
[null, 'elegant', 'z', -1], |
| 278 |
[0, 'abc', ['abc'], 0], |
| 279 |
[5, 'DEJAAAA', 'A', -2], |
| 280 |
[74, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'i', 0], |
| 281 |
[19, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'i', -40], |
| 282 |
[6, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'ipsum', 0], |
| 283 |
[57, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'amet', 0], |
| 284 |
[57, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'amet', -10], |
| 285 |
[22, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'amet', -30], |
| 286 |
]; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* @dataProvider provideIndexOfLastIgnoreCase |
| 291 |
*/ |
| 292 |
public function testIndexOfLastIgnoreCase(?int $result, string $string, string $needle, int $offset) |
| 293 |
{ |
| 294 |
$instance = static::createFromString($string); |
| 295 |
|
| 296 |
$this->assertSame($result, $instance->ignoreCase()->indexOfLast($needle, $offset)); |
| 297 |
} |
| 298 |
|
| 299 |
public static function provideIndexOfLastIgnoreCase(): array |
| 300 |
{ |
| 301 |
return [ |
| 302 |
[null, 'abc', '', 0], |
| 303 |
[null, 'abc', '', -2], |
| 304 |
[null, 'elegant', 'z', -1], |
| 305 |
[1, 'abc', 'b', 0], |
| 306 |
[1, 'abc', 'b', -1], |
| 307 |
[2, 'abcdefgh', 'c', -1], |
| 308 |
[2, 'abcdefgh', 'C', -1], |
| 309 |
[5, 'dejaaaa', 'A', -2], |
| 310 |
[5, 'DEJAAAA', 'a', -2], |
| 311 |
[74, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'I', 0], |
| 312 |
[19, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'I', -40], |
| 313 |
[6, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'IPSUM', 0], |
| 314 |
[57, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'AmeT', 0], |
| 315 |
[57, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'aMEt', -10], |
| 316 |
[22, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, amet sagittis felis.', 'AMET', -30], |
| 317 |
]; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* @dataProvider provideSplit |
| 322 |
*/ |
| 323 |
public function testSplit(string $string, string $delimiter, array $chunks, ?int $limit, int $flags = null) |
| 324 |
{ |
| 325 |
$this->assertEquals($chunks, static::createFromString($string)->split($delimiter, $limit, $flags)); |
| 326 |
} |
| 327 |
|
| 328 |
public static function provideSplit(): array |
| 329 |
{ |
| 330 |
return [ |
| 331 |
[ |
| 332 |
'hello world', |
| 333 |
' ', |
| 334 |
[ |
| 335 |
static::createFromString('hello'), |
| 336 |
static::createFromString('world'), |
| 337 |
], |
| 338 |
null, |
| 339 |
], |
| 340 |
[ |
| 341 |
'radar', |
| 342 |
'd', |
| 343 |
[ |
| 344 |
static::createFromString('ra'), |
| 345 |
static::createFromString('ar'), |
| 346 |
], |
| 347 |
2, |
| 348 |
], |
| 349 |
[ |
| 350 |
'foo,bar,baz,qux,kix', |
| 351 |
',', |
| 352 |
[ |
| 353 |
static::createFromString('foo'), |
| 354 |
static::createFromString('bar'), |
| 355 |
static::createFromString('baz'), |
| 356 |
static::createFromString('qux'), |
| 357 |
static::createFromString('kix'), |
| 358 |
], |
| 359 |
null, |
| 360 |
], |
| 361 |
[ |
| 362 |
'foo,bar,baz,qux,kix', |
| 363 |
',', |
| 364 |
[ |
| 365 |
static::createFromString('foo,bar,baz,qux,kix'), |
| 366 |
], |
| 367 |
1, |
| 368 |
], |
| 369 |
[ |
| 370 |
'foo,bar,baz,qux,kix', |
| 371 |
',', |
| 372 |
[ |
| 373 |
static::createFromString('foo'), |
| 374 |
static::createFromString('bar'), |
| 375 |
static::createFromString('baz,qux,kix'), |
| 376 |
], |
| 377 |
3, |
| 378 |
], |
| 379 |
[ |
| 380 |
'Quisque viverra tincidunt elit. Vestibulum convallis dui nec lacis suscipit cursus.', |
| 381 |
'is', |
| 382 |
[ |
| 383 |
static::createFromString('Qu'), |
| 384 |
static::createFromString('que viverra tincidunt elit. Vestibulum convall'), |
| 385 |
static::createFromString(' dui nec lac'), |
| 386 |
static::createFromString(' suscipit cursus.'), |
| 387 |
], |
| 388 |
null, |
| 389 |
], |
| 390 |
[ |
| 391 |
'foo,,bar, baz , qux,kix', |
| 392 |
'/\s*,\s*/', |
| 393 |
[ |
| 394 |
static::createFromString('foo'), |
| 395 |
static::createFromString(''), |
| 396 |
static::createFromString('bar'), |
| 397 |
static::createFromString('baz'), |
| 398 |
static::createFromString('qux,kix'), |
| 399 |
], |
| 400 |
5, |
| 401 |
AbstractString::PREG_SPLIT, |
| 402 |
], |
| 403 |
[ |
| 404 |
'foo ,,bar, baz', |
| 405 |
'/\s*(,)\s*/', |
| 406 |
[ |
| 407 |
static::createFromString('foo'), |
| 408 |
static::createFromString(','), |
| 409 |
static::createFromString(','), |
| 410 |
static::createFromString('bar'), |
| 411 |
static::createFromString(','), |
| 412 |
static::createFromString('baz'), |
| 413 |
], |
| 414 |
null, |
| 415 |
AbstractString::PREG_SPLIT_NO_EMPTY | AbstractString::PREG_SPLIT_DELIM_CAPTURE, |
| 416 |
], |
| 417 |
[ |
| 418 |
'foo, bar,baz', |
| 419 |
'/\s*(,)\s*/', |
| 420 |
[ |
| 421 |
[static::createFromString('foo'), 0], |
| 422 |
[static::createFromString('bar'), 5], |
| 423 |
[static::createFromString('baz'), 9], |
| 424 |
], |
| 425 |
null, |
| 426 |
AbstractString::PREG_SPLIT_OFFSET_CAPTURE, |
| 427 |
], |
| 428 |
]; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* @dataProvider provideInvalidChunkLength |
| 433 |
*/ |
| 434 |
public function testInvalidChunkLength(int $length) |
| 435 |
{ |
| 436 |
$this->expectException(InvalidArgumentException::class); |
| 437 |
|
| 438 |
static::createFromString('foo|bar|baz')->chunk($length); |
| 439 |
} |
| 440 |
|
| 441 |
public static function provideInvalidChunkLength(): array |
| 442 |
{ |
| 443 |
return [ |
| 444 |
[-2], |
| 445 |
[-1], |
| 446 |
[0], |
| 447 |
]; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* @dataProvider provideChunk |
| 452 |
*/ |
| 453 |
public function testChunk(string $string, array $chunks, int $length) |
| 454 |
{ |
| 455 |
$this->assertEquals($chunks, static::createFromString($string)->chunk($length)); |
| 456 |
} |
| 457 |
|
| 458 |
public static function provideChunk() |
| 459 |
{ |
| 460 |
return [ |
| 461 |
[ |
| 462 |
'', |
| 463 |
[], |
| 464 |
1, |
| 465 |
], |
| 466 |
[ |
| 467 |
'hello', |
| 468 |
[ |
| 469 |
static::createFromString('h'), |
| 470 |
static::createFromString('e'), |
| 471 |
static::createFromString('l'), |
| 472 |
static::createFromString('l'), |
| 473 |
static::createFromString('o'), |
| 474 |
], |
| 475 |
1, |
| 476 |
], |
| 477 |
[ |
| 478 |
'hello you!', |
| 479 |
[ |
| 480 |
static::createFromString('h'), |
| 481 |
static::createFromString('e'), |
| 482 |
static::createFromString('l'), |
| 483 |
static::createFromString('l'), |
| 484 |
static::createFromString('o'), |
| 485 |
static::createFromString(' '), |
| 486 |
static::createFromString('y'), |
| 487 |
static::createFromString('o'), |
| 488 |
static::createFromString('u'), |
| 489 |
static::createFromString('!'), |
| 490 |
], |
| 491 |
1, |
| 492 |
], |
| 493 |
[ |
| 494 |
'hell', |
| 495 |
[ |
| 496 |
static::createFromString('h'), |
| 497 |
static::createFromString('e'), |
| 498 |
static::createFromString('l'), |
| 499 |
static::createFromString('l'), |
| 500 |
], |
| 501 |
1, |
| 502 |
], |
| 503 |
[ |
| 504 |
'hell', |
| 505 |
[ |
| 506 |
static::createFromString('he'), |
| 507 |
static::createFromString('ll'), |
| 508 |
], |
| 509 |
2, |
| 510 |
], |
| 511 |
[ |
| 512 |
str_repeat('-', 65537), |
| 513 |
[ |
| 514 |
static::createFromString(str_repeat('-', 65536)), |
| 515 |
static::createFromString('-'), |
| 516 |
], |
| 517 |
65536, |
| 518 |
], |
| 519 |
]; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* @dataProvider provideLower |
| 524 |
*/ |
| 525 |
public function testLower(string $expected, string $origin) |
| 526 |
{ |
| 527 |
$instance = static::createFromString($origin)->lower(); |
| 528 |
|
| 529 |
$this->assertNotSame(static::createFromString($origin), $instance); |
| 530 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 531 |
$this->assertSame($expected, (string) $instance); |
| 532 |
} |
| 533 |
|
| 534 |
public static function provideLower() |
| 535 |
{ |
| 536 |
return [ |
| 537 |
['hello world', 'hello world'], |
| 538 |
['hello world', 'HELLO WORLD'], |
| 539 |
['hello world!', 'Hello World!'], |
| 540 |
['symfony', 'symfony'], |
| 541 |
['symfony', 'Symfony'], |
| 542 |
['symfony', 'sYmFOny'], |
| 543 |
]; |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* @dataProvider provideUpper |
| 548 |
*/ |
| 549 |
public function testUpper(string $expected, string $origin) |
| 550 |
{ |
| 551 |
$instance = static::createFromString($origin)->upper(); |
| 552 |
|
| 553 |
$this->assertNotSame(static::createFromString($origin), $instance); |
| 554 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 555 |
$this->assertSame($expected, (string) $instance); |
| 556 |
} |
| 557 |
|
| 558 |
public static function provideUpper() |
| 559 |
{ |
| 560 |
return [ |
| 561 |
['HELLO WORLD', 'hello world'], |
| 562 |
['HELLO WORLD', 'HELLO WORLD'], |
| 563 |
['HELLO WORLD!', 'Hello World!'], |
| 564 |
['SYMFONY', 'symfony'], |
| 565 |
['SYMFONY', 'Symfony'], |
| 566 |
['SYMFONY', 'sYmFOny'], |
| 567 |
]; |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* @dataProvider provideTitle |
| 572 |
*/ |
| 573 |
public function testTitle(string $expected, string $origin, bool $allWords) |
| 574 |
{ |
| 575 |
$this->assertEquals( |
| 576 |
static::createFromString($expected), |
| 577 |
static::createFromString($origin)->title($allWords) |
| 578 |
); |
| 579 |
} |
| 580 |
|
| 581 |
public static function provideTitle() |
| 582 |
{ |
| 583 |
return [ |
| 584 |
['Hello world', 'hello world', false], |
| 585 |
['Hello World', 'hello world', true], |
| 586 |
['HELLO WORLD', 'HELLO WORLD', false], |
| 587 |
['HELLO WORLD', 'HELLO WORLD', true], |
| 588 |
['HELLO wORLD', 'hELLO wORLD', false], |
| 589 |
['HELLO WORLD', 'hELLO wORLD', true], |
| 590 |
['Symfony', 'symfony', false], |
| 591 |
['Symfony', 'Symfony', false], |
| 592 |
['SYmFOny', 'sYmFOny', false], |
| 593 |
]; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* @dataProvider provideSlice |
| 598 |
*/ |
| 599 |
public function testSlice(string $expected, string $origin, int $start, int $length = null) |
| 600 |
{ |
| 601 |
$this->assertEquals( |
| 602 |
static::createFromString($expected), |
| 603 |
static::createFromString($origin)->slice($start, $length) |
| 604 |
); |
| 605 |
} |
| 606 |
|
| 607 |
public static function provideSlice() |
| 608 |
{ |
| 609 |
return [ |
| 610 |
['Symfony', 'Symfony is awesome', 0, 7], |
| 611 |
[' ', 'Symfony is awesome', 7, 1], |
| 612 |
['is', 'Symfony is awesome', 8, 2], |
| 613 |
['is awesome', 'Symfony is awesome', 8, null], |
| 614 |
[' ', 'Symfony is awesome', 10, 1], |
| 615 |
['awesome', 'Symfony is awesome', 11, 7], |
| 616 |
['awesome', 'Symfony is awesome', -7, null], |
| 617 |
['awe', 'Symfony is awesome', -7, -4], |
| 618 |
['S', 'Symfony is awesome', -42, 1], |
| 619 |
['', 'Symfony is awesome', 42, 1], |
| 620 |
['', 'Symfony is awesome', 0, -42], |
| 621 |
]; |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* @dataProvider provideSplice |
| 626 |
*/ |
| 627 |
public function testSplice(string $expected, int $start, int $length = null) |
| 628 |
{ |
| 629 |
$this->assertEquals( |
| 630 |
static::createFromString($expected), |
| 631 |
static::createFromString('Symfony is awesome')->splice('X', $start, $length) |
| 632 |
); |
| 633 |
} |
| 634 |
|
| 635 |
public static function provideSplice() |
| 636 |
{ |
| 637 |
return [ |
| 638 |
['X is awesome', 0, 7], |
| 639 |
['SymfonyXis awesome', 7, 1], |
| 640 |
['Symfony X awesome', 8, 2], |
| 641 |
['Symfony X', 8, null], |
| 642 |
['Symfony isXawesome', 10, 1], |
| 643 |
['Symfony is X', 11, 7], |
| 644 |
['Symfony is X', -7, null], |
| 645 |
['Symfony is Xsome', -7, -4], |
| 646 |
['Xymfony is awesome', -42, 1], |
| 647 |
['Symfony is awesomeX', 42, 1], |
| 648 |
['XSymfony is awesome', 0, -42], |
| 649 |
]; |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* @dataProvider provideAppend |
| 654 |
*/ |
| 655 |
public function testAppend(string $expected, array $suffixes) |
| 656 |
{ |
| 657 |
$instance = static::createFromString(''); |
| 658 |
foreach ($suffixes as $suffix) { |
| 659 |
$instance = $instance->append($suffix); |
| 660 |
} |
| 661 |
|
| 662 |
$this->assertEquals($expected, $instance); |
| 663 |
|
| 664 |
$instance = static::createFromString('')->append(...$suffixes); |
| 665 |
|
| 666 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 667 |
} |
| 668 |
|
| 669 |
public static function provideAppend() |
| 670 |
{ |
| 671 |
return [ |
| 672 |
[ |
| 673 |
'', |
| 674 |
[], |
| 675 |
], |
| 676 |
[ |
| 677 |
'Symfony', |
| 678 |
['Sym', 'fony'], |
| 679 |
], |
| 680 |
[ |
| 681 |
'Hello World!', |
| 682 |
['Hel', 'lo', ' ', 'World', '!'], |
| 683 |
], |
| 684 |
]; |
| 685 |
} |
| 686 |
|
| 687 |
/** |
| 688 |
* @dataProvider provideAppend |
| 689 |
*/ |
| 690 |
public function testPrepend(string $expected, array $prefixes) |
| 691 |
{ |
| 692 |
$instance = static::createFromString(''); |
| 693 |
foreach (array_reverse($prefixes) as $suffix) { |
| 694 |
$instance = $instance->prepend($suffix); |
| 695 |
} |
| 696 |
|
| 697 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 698 |
|
| 699 |
$instance = static::createFromString('')->prepend(...$prefixes); |
| 700 |
|
| 701 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
* @dataProvider provideTrim |
| 706 |
*/ |
| 707 |
public function testTrim(string $expected, string $origin, ?string $chars) |
| 708 |
{ |
| 709 |
$result = static::createFromString($origin); |
| 710 |
$result = null !== $chars ? $result->trim($chars) : $result->trim(); |
| 711 |
|
| 712 |
$this->assertEquals(static::createFromString($expected), $result); |
| 713 |
} |
| 714 |
|
| 715 |
public static function provideTrim() |
| 716 |
{ |
| 717 |
return [ |
| 718 |
[ |
| 719 |
"Symfony IS GREAT\t!!!", |
| 720 |
" Symfony IS GREAT\t!!!\n", |
| 721 |
null, |
| 722 |
], |
| 723 |
[ |
| 724 |
"Symfony IS GREAT\t!!!\n", |
| 725 |
" Symfony IS GREAT\t!!!\n", |
| 726 |
' ', |
| 727 |
], |
| 728 |
[ |
| 729 |
" Symfony IS GREAT\t!!!", |
| 730 |
" Symfony IS GREAT\t!!!\n", |
| 731 |
"\n", |
| 732 |
], |
| 733 |
[ |
| 734 |
"Symfony IS GREAT\t", |
| 735 |
" Symfony IS GREAT\t!!!\n", |
| 736 |
" \n!", |
| 737 |
], |
| 738 |
[ |
| 739 |
"Symfony IS GREAT\t!!! \n", |
| 740 |
" Symfony IS GREAT\t!!! \n", |
| 741 |
' ', |
| 742 |
], |
| 743 |
]; |
| 744 |
} |
| 745 |
|
| 746 |
public function testTrimPrefix() |
| 747 |
{ |
| 748 |
$str = static::createFromString('abc.def'); |
| 749 |
|
| 750 |
$this->assertEquals(static::createFromString('def'), $str->trimPrefix('abc.')); |
| 751 |
$this->assertEquals(static::createFromString('def'), $str->trimPrefix(['abc.', 'def'])); |
| 752 |
$this->assertEquals(static::createFromString('def'), $str->ignoreCase()->trimPrefix('ABC.')); |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* @dataProvider provideTrimStart |
| 757 |
*/ |
| 758 |
public function testTrimStart(string $expected, string $origin, ?string $chars) |
| 759 |
{ |
| 760 |
$result = static::createFromString($origin); |
| 761 |
$result = null !== $chars ? $result->trimStart($chars) : $result->trimStart(); |
| 762 |
|
| 763 |
$this->assertEquals(static::createFromString($expected), $result); |
| 764 |
} |
| 765 |
|
| 766 |
public function testTrimSuffix() |
| 767 |
{ |
| 768 |
$str = static::createFromString('abc.def'); |
| 769 |
|
| 770 |
$this->assertEquals(static::createFromString('abc'), $str->trimSuffix('.def')); |
| 771 |
$this->assertEquals(static::createFromString('abc'), $str->trimSuffix(['.def', 'abc'])); |
| 772 |
$this->assertEquals(static::createFromString('abc'), $str->ignoreCase()->trimSuffix('.DEF')); |
| 773 |
} |
| 774 |
|
| 775 |
public static function provideTrimStart() |
| 776 |
{ |
| 777 |
return [ |
| 778 |
[ |
| 779 |
"<span>Symfony is a PHP framework</span>\n", |
| 780 |
"\n\t<span>Symfony is a PHP framework</span>\n", |
| 781 |
null, |
| 782 |
], |
| 783 |
[ |
| 784 |
"\t<span>Symfony is a PHP framework</span>\n", |
| 785 |
"\n\t<span>Symfony is a PHP framework</span>\n", |
| 786 |
"\n", |
| 787 |
], |
| 788 |
]; |
| 789 |
} |
| 790 |
|
| 791 |
/** |
| 792 |
* @dataProvider provideTrimEnd |
| 793 |
*/ |
| 794 |
public function testTrimEnd(string $expected, string $origin, ?string $chars) |
| 795 |
{ |
| 796 |
$result = static::createFromString($origin); |
| 797 |
$result = null !== $chars ? $result->trimEnd($chars) : $result->trimEnd(); |
| 798 |
|
| 799 |
$this->assertEquals(static::createFromString($expected), $result); |
| 800 |
} |
| 801 |
|
| 802 |
public static function provideTrimEnd() |
| 803 |
{ |
| 804 |
return [ |
| 805 |
[ |
| 806 |
"\n\t<span>Symfony is a PHP framework</span>", |
| 807 |
"\n\t<span>Symfony is a PHP framework</span> \n", |
| 808 |
null, |
| 809 |
], |
| 810 |
[ |
| 811 |
"\n\t<span>Symfony is a PHP framework</span> ", |
| 812 |
"\n\t<span>Symfony is a PHP framework</span> \n", |
| 813 |
"\n", |
| 814 |
], |
| 815 |
[ |
| 816 |
"\n\t<span>Symfony is a PHP framework</span> \n", |
| 817 |
"\n\t<span>Symfony is a PHP framework</span> \n", |
| 818 |
' ', |
| 819 |
], |
| 820 |
]; |
| 821 |
} |
| 822 |
|
| 823 |
/** |
| 824 |
* @dataProvider provideBeforeAfter |
| 825 |
*/ |
| 826 |
public function testBeforeAfter(string $expected, string $needle, string $origin, int $offset, bool $before) |
| 827 |
{ |
| 828 |
$result = static::createFromString($origin); |
| 829 |
$result = $before ? $result->before($needle, false, $offset) : $result->after($needle, true, $offset); |
| 830 |
$this->assertEquals(static::createFromString($expected), $result); |
| 831 |
} |
| 832 |
|
| 833 |
public static function provideBeforeAfter() |
| 834 |
{ |
| 835 |
return [ |
| 836 |
['hello world', '', 'hello world', 0, true], |
| 837 |
['hello world', '', 'hello world', 0, false], |
| 838 |
['hello World', 'w', 'hello World', 0, true], |
| 839 |
['hello World', 'w', 'hello World', 0, false], |
| 840 |
['hello world', 'o', 'hello world', 10, true], |
| 841 |
['hello world', 'o', 'hello world', 10, false], |
| 842 |
['hello ', 'w', 'hello world', 0, true], |
| 843 |
['world', 'w', 'hello world', 0, false], |
| 844 |
['hello W', 'O', 'hello WORLD', 0, true], |
| 845 |
['ORLD', 'O', 'hello WORLD', 0, false], |
| 846 |
['abac', 'ab', 'abacabab', 1, true], |
| 847 |
['abab', 'ab', 'abacabab', 1, false], |
| 848 |
]; |
| 849 |
} |
| 850 |
|
| 851 |
/** |
| 852 |
* @dataProvider provideBeforeAfterIgnoreCase |
| 853 |
*/ |
| 854 |
public function testBeforeAfterIgnoreCase(string $expected, string $needle, string $origin, int $offset, bool $before) |
| 855 |
{ |
| 856 |
$result = static::createFromString($origin)->ignoreCase(); |
| 857 |
$result = $before ? $result->before($needle, false, $offset) : $result->after($needle, true, $offset); |
| 858 |
$this->assertEquals(static::createFromString($expected), $result); |
| 859 |
} |
| 860 |
|
| 861 |
public static function provideBeforeAfterIgnoreCase() |
| 862 |
{ |
| 863 |
return [ |
| 864 |
['hello world', '', 'hello world', 0, true], |
| 865 |
['hello world', '', 'hello world', 0, false], |
| 866 |
['hello world', 'foo', 'hello world', 0, true], |
| 867 |
['hello world', 'foo', 'hello world', 0, false], |
| 868 |
['hello world', 'o', 'hello world', 10, true], |
| 869 |
['hello world', 'o', 'hello world', 10, false], |
| 870 |
['hello ', 'w', 'hello world', 0, true], |
| 871 |
['world', 'w', 'hello world', 0, false], |
| 872 |
['hello ', 'W', 'hello world', 0, true], |
| 873 |
['world', 'W', 'hello world', 0, false], |
| 874 |
['Abac', 'Ab', 'AbacaBAb', 1, true], |
| 875 |
['aBAb', 'Ab', 'AbacaBAb', 1, false], |
| 876 |
]; |
| 877 |
} |
| 878 |
|
| 879 |
/** |
| 880 |
* @dataProvider provideBeforeAfterLast |
| 881 |
*/ |
| 882 |
public function testBeforeAfterLast(string $expected, string $needle, string $origin, int $offset, bool $before) |
| 883 |
{ |
| 884 |
$result = static::createFromString($origin); |
| 885 |
$result = $before ? $result->beforeLast($needle, false, $offset) : $result->afterLast($needle, true, $offset); |
| 886 |
$this->assertEquals(static::createFromString($expected), $result); |
| 887 |
} |
| 888 |
|
| 889 |
public static function provideBeforeAfterLast() |
| 890 |
{ |
| 891 |
return [ |
| 892 |
['hello world', '', 'hello world', 0, true], |
| 893 |
['hello world', '', 'hello world', 0, false], |
| 894 |
['hello world', 'L', 'hello world', 0, true], |
| 895 |
['hello world', 'L', 'hello world', 0, false], |
| 896 |
['hello world', 'o', 'hello world', 10, true], |
| 897 |
['hello world', 'o', 'hello world', 10, false], |
| 898 |
['hello wor', 'l', 'hello world', 0, true], |
| 899 |
['ld', 'l', 'hello world', 0, false], |
| 900 |
['hello w', 'o', 'hello world', 0, true], |
| 901 |
['orld', 'o', 'hello world', 0, false], |
| 902 |
['abacab', 'ab', 'abacabab', 1, true], |
| 903 |
['ab', 'ab', 'abacabab', 1, false], |
| 904 |
['hello world', 'hello', 'hello world', 0, false], |
| 905 |
]; |
| 906 |
} |
| 907 |
|
| 908 |
/** |
| 909 |
* @dataProvider provideBeforeAfterLastIgnoreCase |
| 910 |
*/ |
| 911 |
public function testBeforeAfterLastIgnoreCase(string $expected, string $needle, string $origin, int $offset, bool $before) |
| 912 |
{ |
| 913 |
$result = static::createFromString($origin)->ignoreCase(); |
| 914 |
$result = $before ? $result->beforeLast($needle, false, $offset) : $result->afterLast($needle, true, $offset); |
| 915 |
$this->assertEquals(static::createFromString($expected), $result); |
| 916 |
} |
| 917 |
|
| 918 |
public static function provideBeforeAfterLastIgnoreCase() |
| 919 |
{ |
| 920 |
return [ |
| 921 |
['hello world', '', 'hello world', 0, true], |
| 922 |
['hello world', '', 'hello world', 0, false], |
| 923 |
['hello world', 'FOO', 'hello world', 0, true], |
| 924 |
['hello world', 'FOO', 'hello world', 0, false], |
| 925 |
['hello world', 'o', 'hello world', 10, true], |
| 926 |
['hello world', 'o', 'hello world', 10, false], |
| 927 |
['hello wor', 'l', 'hello world', 0, true], |
| 928 |
['ld', 'l', 'hello world', 0, false], |
| 929 |
['hello wor', 'L', 'hello world', 0, true], |
| 930 |
['ld', 'L', 'hello world', 0, false], |
| 931 |
['hello w', 'O', 'hello world', 0, true], |
| 932 |
['orld', 'O', 'hello world', 0, false], |
| 933 |
['AbacaB', 'Ab', 'AbacaBaB', 1, true], |
| 934 |
['aB', 'Ab', 'AbacaBaB', 1, false], |
| 935 |
]; |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* @dataProvider provideFolded |
| 940 |
*/ |
| 941 |
public function testFolded(string $expected, string $origin) |
| 942 |
{ |
| 943 |
$this->assertEquals( |
| 944 |
static::createFromString($expected), |
| 945 |
static::createFromString($origin)->folded() |
| 946 |
); |
| 947 |
} |
| 948 |
|
| 949 |
public static function provideFolded() |
| 950 |
{ |
| 951 |
return [ |
| 952 |
['hello', 'HELlo'], |
| 953 |
['world', 'worLd'], |
| 954 |
]; |
| 955 |
} |
| 956 |
|
| 957 |
/** |
| 958 |
* @dataProvider provideReplace |
| 959 |
*/ |
| 960 |
public function testReplace(string $expectedString, int $expectedCount, string $origin, string $from, string $to) |
| 961 |
{ |
| 962 |
$origin = static::createFromString($origin); |
| 963 |
$result = $origin->replace($from, $to); |
| 964 |
|
| 965 |
$this->assertEquals(static::createFromString($expectedString), $result); |
| 966 |
} |
| 967 |
|
| 968 |
public static function provideReplace() |
| 969 |
{ |
| 970 |
return [ |
| 971 |
['hello world', 0, 'hello world', '', ''], |
| 972 |
['hello world', 0, 'hello world', '', '_'], |
| 973 |
['helloworld', 1, 'hello world', ' ', ''], |
| 974 |
['hello_world', 1, 'hello world', ' ', '_'], |
| 975 |
['hemmo wormd', 3, 'hello world', 'l', 'm'], |
| 976 |
['hello world', 0, 'hello world', 'L', 'm'], |
| 977 |
]; |
| 978 |
} |
| 979 |
|
| 980 |
/** |
| 981 |
* @dataProvider provideReplaceMatches |
| 982 |
*/ |
| 983 |
public function testReplaceMatches(string $expectedString, string $origin, string $fromRegexp, $to) |
| 984 |
{ |
| 985 |
$origin = static::createFromString($origin); |
| 986 |
$result = $origin->replaceMatches($fromRegexp, $to); |
| 987 |
|
| 988 |
$this->assertEquals(static::createFromString($expectedString), $result); |
| 989 |
} |
| 990 |
|
| 991 |
public static function provideReplaceMatches() |
| 992 |
{ |
| 993 |
return [ |
| 994 |
['April,15,2003', 'April 15, 2003', '/(\w+) (\d+), (\d+)/i', '${1},$2,$3'], |
| 995 |
['5/27/1999', '1999-5-27', '/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '\3/\4/\1\2'], |
| 996 |
['Copyright 2000', 'Copyright 1999', '([0-9]+)', '2000'], |
| 997 |
['hello world! this is a test', 'HELLO WORLD! THIS is a test', '/\b([A-Z]+)\b/', function ($word) { |
| 998 |
return strtolower($word[1]); |
| 999 |
}], |
| 1000 |
['COPYRIGHT 1999', 'Copyright 1999', '/[a-z]/', function ($matches) { |
| 1001 |
foreach ($matches as $match) { |
| 1002 |
return strtoupper($match); |
| 1003 |
} |
| 1004 |
}], |
| 1005 |
]; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/** |
| 1009 |
* @dataProvider provideReplaceIgnoreCase |
| 1010 |
*/ |
| 1011 |
public function testReplaceIgnoreCase(string $expectedString, int $expectedCount, string $origin, string $from, string $to) |
| 1012 |
{ |
| 1013 |
$origin = static::createFromString($origin); |
| 1014 |
$result = $origin->ignoreCase()->replace($from, $to); |
| 1015 |
|
| 1016 |
$this->assertEquals(static::createFromString($expectedString), $result); |
| 1017 |
} |
| 1018 |
|
| 1019 |
public static function provideReplaceIgnoreCase() |
| 1020 |
{ |
| 1021 |
return [ |
| 1022 |
['hello world', 0, 'hello world', '', ''], |
| 1023 |
['hello world', 0, 'hello world', '', '_'], |
| 1024 |
['helloworld', 1, 'hello world', ' ', ''], |
| 1025 |
['hello_world', 1, 'hello world', ' ', '_'], |
| 1026 |
['hemmo wormd', 3, 'hello world', 'l', 'm'], |
| 1027 |
['heMMo worMd', 3, 'hello world', 'L', 'M'], |
| 1028 |
]; |
| 1029 |
} |
| 1030 |
|
| 1031 |
/** |
| 1032 |
* @dataProvider provideCamel |
| 1033 |
*/ |
| 1034 |
public function testCamel(string $expectedString, string $origin) |
| 1035 |
{ |
| 1036 |
$instance = static::createFromString($origin)->camel(); |
| 1037 |
|
| 1038 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1039 |
} |
| 1040 |
|
| 1041 |
public static function provideCamel() |
| 1042 |
{ |
| 1043 |
return [ |
| 1044 |
['', ''], |
| 1045 |
['xY', 'x_y'], |
| 1046 |
['xuYo', 'xu_yo'], |
| 1047 |
['symfonyIsGreat', 'symfony_is_great'], |
| 1048 |
['symfony5IsGreat', 'symfony_5_is_great'], |
| 1049 |
['symfonyIsGreat', 'Symfony is great'], |
| 1050 |
['symfonyIsAGreatFramework', 'Symfony is a great framework'], |
| 1051 |
['symfonyIsGREAT', '*Symfony* is GREAT!!'], |
| 1052 |
['SYMFONY', 'SYMFONY'], |
| 1053 |
]; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* @dataProvider provideSnake |
| 1058 |
*/ |
| 1059 |
public function testSnake(string $expectedString, string $origin) |
| 1060 |
{ |
| 1061 |
$instance = static::createFromString($origin)->snake(); |
| 1062 |
|
| 1063 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1064 |
} |
| 1065 |
|
| 1066 |
public static function provideSnake() |
| 1067 |
{ |
| 1068 |
return [ |
| 1069 |
['', ''], |
| 1070 |
['x_y', 'x_y'], |
| 1071 |
['x_y', 'X_Y'], |
| 1072 |
['xu_yo', 'xu_yo'], |
| 1073 |
['symfony_is_great', 'symfonyIsGreat'], |
| 1074 |
['symfony5_is_great', 'symfony5IsGreat'], |
| 1075 |
['symfony5is_great', 'symfony5isGreat'], |
| 1076 |
['symfony_is_great', 'Symfony is great'], |
| 1077 |
['symfony_is_a_great_framework', 'symfonyIsAGreatFramework'], |
| 1078 |
['symfony_is_great', 'symfonyIsGREAT'], |
| 1079 |
['symfony_is_really_great', 'symfonyIsREALLYGreat'], |
| 1080 |
['symfony', 'SYMFONY'], |
| 1081 |
]; |
| 1082 |
} |
| 1083 |
|
| 1084 |
/** |
| 1085 |
* @dataProvider provideStartsWith |
| 1086 |
*/ |
| 1087 |
public function testStartsWith(bool $expected, string $origin, $prefix, int $form = null) |
| 1088 |
{ |
| 1089 |
$instance = static::createFromString($origin); |
| 1090 |
$instance = $form ? $instance->normalize($form) : $instance; |
| 1091 |
|
| 1092 |
$this->assertSame($expected, $instance->startsWith($prefix)); |
| 1093 |
} |
| 1094 |
|
| 1095 |
public static function provideStartsWith() |
| 1096 |
{ |
| 1097 |
return [ |
| 1098 |
[false, '', ''], |
| 1099 |
[false, '', 'foo'], |
| 1100 |
[false, 'foo', ''], |
| 1101 |
[false, 'foo', 'o'], |
| 1102 |
[false, 'foo', 'F'], |
| 1103 |
[false, "\nfoo", 'f'], |
| 1104 |
[true, 'foo', 'f'], |
| 1105 |
[true, 'foo', 'fo'], |
| 1106 |
[true, 'foo', new ByteString('f')], |
| 1107 |
[true, 'foo', new CodePointString('f')], |
| 1108 |
[true, 'foo', new UnicodeString('f')], |
| 1109 |
[true, 'foo', ['e', 'f', 'g']], |
| 1110 |
]; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/** |
| 1114 |
* @dataProvider provideStartsWithIgnoreCase |
| 1115 |
*/ |
| 1116 |
public function testStartsWithIgnoreCase(bool $expected, string $origin, $prefix) |
| 1117 |
{ |
| 1118 |
$this->assertSame($expected, static::createFromString($origin)->ignoreCase()->startsWith($prefix)); |
| 1119 |
} |
| 1120 |
|
| 1121 |
public static function provideStartsWithIgnoreCase() |
| 1122 |
{ |
| 1123 |
return [ |
| 1124 |
[false, '', ''], |
| 1125 |
[false, '', 'foo'], |
| 1126 |
[false, 'foo', ''], |
| 1127 |
[false, 'foo', 'o'], |
| 1128 |
[false, "\nfoo", 'f'], |
| 1129 |
[true, 'foo', 'F'], |
| 1130 |
[true, 'FoO', 'foo'], |
| 1131 |
[true, 'foo', new ByteString('F')], |
| 1132 |
[true, 'foo', new CodePointString('F')], |
| 1133 |
[true, 'foo', new UnicodeString('F')], |
| 1134 |
[true, 'foo', ['E', 'F', 'G']], |
| 1135 |
]; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/** |
| 1139 |
* @dataProvider provideEndsWith |
| 1140 |
*/ |
| 1141 |
public function testEndsWith(bool $expected, string $origin, $suffix, int $form = null) |
| 1142 |
{ |
| 1143 |
$instance = static::createFromString($origin); |
| 1144 |
$instance = $form ? $instance->normalize($form) : $instance; |
| 1145 |
|
| 1146 |
$this->assertSame($expected, $instance->endsWith($suffix)); |
| 1147 |
} |
| 1148 |
|
| 1149 |
public static function provideEndsWith() |
| 1150 |
{ |
| 1151 |
return [ |
| 1152 |
[false, '', ''], |
| 1153 |
[false, '', 'foo'], |
| 1154 |
[false, 'foo', ''], |
| 1155 |
[false, 'foo', 'f'], |
| 1156 |
[false, 'foo', 'O'], |
| 1157 |
[false, "foo\n", 'o'], |
| 1158 |
[true, 'foo', 'o'], |
| 1159 |
[true, 'foo', 'foo'], |
| 1160 |
[true, 'foo', new ByteString('o')], |
| 1161 |
[true, 'foo', new CodePointString('o')], |
| 1162 |
[true, 'foo', new UnicodeString('o')], |
| 1163 |
[true, 'foo', ['a', 'o', 'u']], |
| 1164 |
]; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/** |
| 1168 |
* @dataProvider provideEndsWithIgnoreCase |
| 1169 |
*/ |
| 1170 |
public function testEndsWithIgnoreCase(bool $expected, string $origin, $suffix) |
| 1171 |
{ |
| 1172 |
$this->assertSame($expected, static::createFromString($origin)->ignoreCase()->endsWith($suffix)); |
| 1173 |
} |
| 1174 |
|
| 1175 |
public static function provideEndsWithIgnoreCase() |
| 1176 |
{ |
| 1177 |
return [ |
| 1178 |
[false, '', ''], |
| 1179 |
[false, '', 'foo'], |
| 1180 |
[false, 'foo', ''], |
| 1181 |
[false, 'foo', 'f'], |
| 1182 |
[false, "foo\n", 'o'], |
| 1183 |
[true, 'foo', 'O'], |
| 1184 |
[true, 'Foo', 'foo'], |
| 1185 |
[true, 'foo', new ByteString('O')], |
| 1186 |
[true, 'foo', new CodePointString('O')], |
| 1187 |
[true, 'foo', new UnicodeString('O')], |
| 1188 |
[true, 'foo', ['A', 'O', 'U']], |
| 1189 |
]; |
| 1190 |
} |
| 1191 |
|
| 1192 |
/** |
| 1193 |
* @dataProvider provideEnsureStart |
| 1194 |
*/ |
| 1195 |
public function testEnsureStart(string $expectedString, string $origin, $prefix) |
| 1196 |
{ |
| 1197 |
$instance = static::createFromString($origin)->ensureStart($prefix); |
| 1198 |
|
| 1199 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1200 |
} |
| 1201 |
|
| 1202 |
public static function provideEnsureStart() |
| 1203 |
{ |
| 1204 |
return [ |
| 1205 |
['', '', ''], |
| 1206 |
['foo', 'foo', ''], |
| 1207 |
['foo', '', 'foo'], |
| 1208 |
['foo', 'foo', 'foo'], |
| 1209 |
['foobar', 'foobar', 'foo'], |
| 1210 |
['foobar', 'bar', 'foo'], |
| 1211 |
['foo', 'foofoofoo', 'foo'], |
| 1212 |
['foobar', 'foofoofoobar', 'foo'], |
| 1213 |
['fooFoobar', 'Foobar', 'foo'], |
| 1214 |
["foo\nfoo", "\nfoo", 'foo'], |
| 1215 |
]; |
| 1216 |
} |
| 1217 |
|
| 1218 |
/** |
| 1219 |
* @dataProvider provideEnsureStartIgnoreCase |
| 1220 |
*/ |
| 1221 |
public function testEnsureStartIgnoreCase(string $expectedString, string $origin, $prefix) |
| 1222 |
{ |
| 1223 |
$instance = static::createFromString($origin)->ignoreCase()->ensureStart($prefix); |
| 1224 |
|
| 1225 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1226 |
} |
| 1227 |
|
| 1228 |
public static function provideEnsureStartIgnoreCase() |
| 1229 |
{ |
| 1230 |
return [ |
| 1231 |
['', '', ''], |
| 1232 |
['foo', 'foo', ''], |
| 1233 |
['foo', '', 'foo'], |
| 1234 |
['Foo', 'Foo', 'foo'], |
| 1235 |
['Foobar', 'Foobar', 'foo'], |
| 1236 |
['foobar', 'bar', 'foo'], |
| 1237 |
['Foo', 'fOofoOFoo', 'foo'], |
| 1238 |
['Foobar', 'fOofoOFoobar', 'foo'], |
| 1239 |
["foo\nfoo", "\nfoo", 'foo'], |
| 1240 |
]; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/** |
| 1244 |
* @dataProvider provideEnsureEnd |
| 1245 |
*/ |
| 1246 |
public function testEnsureEnd(string $expectedString, string $origin, $suffix) |
| 1247 |
{ |
| 1248 |
$instance = static::createFromString($origin)->ensureEnd($suffix); |
| 1249 |
|
| 1250 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1251 |
} |
| 1252 |
|
| 1253 |
public static function provideEnsureEnd() |
| 1254 |
{ |
| 1255 |
return [ |
| 1256 |
['', '', ''], |
| 1257 |
['foo', 'foo', ''], |
| 1258 |
['foo', '', 'foo'], |
| 1259 |
['foo', 'foo', 'foo'], |
| 1260 |
['foobar', 'foobar', 'bar'], |
| 1261 |
['foobar', 'foo', 'bar'], |
| 1262 |
['foo', 'foofoofoo', 'foo'], |
| 1263 |
['foobar', 'foobarbarbar', 'bar'], |
| 1264 |
['fooBarbar', 'fooBar', 'bar'], |
| 1265 |
["foo\nfoo", "foo\n", 'foo'], |
| 1266 |
]; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/** |
| 1270 |
* @dataProvider provideEnsureEndIgnoreCase |
| 1271 |
*/ |
| 1272 |
public function testEnsureEndIgnoreCase(string $expectedString, string $origin, $suffix) |
| 1273 |
{ |
| 1274 |
$instance = static::createFromString($origin)->ignoreCase()->ensureEnd($suffix); |
| 1275 |
|
| 1276 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1277 |
} |
| 1278 |
|
| 1279 |
public static function provideEnsureEndIgnoreCase() |
| 1280 |
{ |
| 1281 |
return [ |
| 1282 |
['', '', ''], |
| 1283 |
['foo', 'foo', ''], |
| 1284 |
['foo', '', 'foo'], |
| 1285 |
['foo', 'foo', 'foo'], |
| 1286 |
['fooBar', 'fooBar', 'bar'], |
| 1287 |
['foobar', 'foo', 'bar'], |
| 1288 |
['fOo', 'fOofoOFoo', 'foo'], |
| 1289 |
['fooBar', 'fooBarbArbaR', 'bar'], |
| 1290 |
["foo\nfoo", "foo\n", 'foo'], |
| 1291 |
]; |
| 1292 |
} |
| 1293 |
|
| 1294 |
/** |
| 1295 |
* @dataProvider provideCollapseWhitespace |
| 1296 |
*/ |
| 1297 |
public function testCollapseWhitespace(string $expectedString, string $origin) |
| 1298 |
{ |
| 1299 |
$instance = static::createFromString($origin)->collapseWhitespace(); |
| 1300 |
|
| 1301 |
$this->assertEquals(static::createFromString($expectedString), $instance); |
| 1302 |
} |
| 1303 |
|
| 1304 |
public static function provideCollapseWhitespace() |
| 1305 |
{ |
| 1306 |
return [ |
| 1307 |
['', ''], |
| 1308 |
['', " \t\r\n"], |
| 1309 |
['foo bar', 'foo bar'], |
| 1310 |
['foo bar baz', ' foo bar baz'], |
| 1311 |
['foo bar baz', " foo\nbar \t baz\n"], |
| 1312 |
]; |
| 1313 |
} |
| 1314 |
|
| 1315 |
/** |
| 1316 |
* @dataProvider provideEqualsTo |
| 1317 |
*/ |
| 1318 |
public function testEqualsTo(bool $expected, string $origin, $other) |
| 1319 |
{ |
| 1320 |
$this->assertSame($expected, static::createFromString($origin)->equalsTo($other)); |
| 1321 |
} |
| 1322 |
|
| 1323 |
public static function provideEqualsTo() |
| 1324 |
{ |
| 1325 |
return [ |
| 1326 |
[true, '', ''], |
| 1327 |
[false, '', 'foo'], |
| 1328 |
[false, 'foo', ''], |
| 1329 |
[false, 'foo', 'Foo'], |
| 1330 |
[false, "foo\n", 'foo'], |
| 1331 |
[true, 'Foo bar', 'Foo bar'], |
| 1332 |
[true, 'Foo bar', new ByteString('Foo bar')], |
| 1333 |
[true, 'Foo bar', new CodePointString('Foo bar')], |
| 1334 |
[true, 'Foo bar', new UnicodeString('Foo bar')], |
| 1335 |
[false, '', []], |
| 1336 |
[false, 'foo', ['bar', 'baz']], |
| 1337 |
[true, 'foo', ['bar', 'foo', 'baz']], |
| 1338 |
]; |
| 1339 |
} |
| 1340 |
|
| 1341 |
/** |
| 1342 |
* @dataProvider provideEqualsToIgnoreCase |
| 1343 |
*/ |
| 1344 |
public function testEqualsToIgnoreCase(bool $expected, string $origin, $other) |
| 1345 |
{ |
| 1346 |
$this->assertSame($expected, static::createFromString($origin)->ignoreCase()->equalsTo($other)); |
| 1347 |
} |
| 1348 |
|
| 1349 |
public static function provideEqualsToIgnoreCase() |
| 1350 |
{ |
| 1351 |
return [ |
| 1352 |
[true, '', ''], |
| 1353 |
[false, '', 'foo'], |
| 1354 |
[false, 'foo', ''], |
| 1355 |
[false, "foo\n", 'foo'], |
| 1356 |
[true, 'foo Bar', 'FOO bar'], |
| 1357 |
[true, 'foo Bar', new ByteString('FOO bar')], |
| 1358 |
[true, 'foo Bar', new CodePointString('FOO bar')], |
| 1359 |
[true, 'foo Bar', new UnicodeString('FOO bar')], |
| 1360 |
[false, '', []], |
| 1361 |
[false, 'Foo', ['bar', 'baz']], |
| 1362 |
[true, 'Foo', ['bar', 'foo', 'baz']], |
| 1363 |
]; |
| 1364 |
} |
| 1365 |
|
| 1366 |
/** |
| 1367 |
* @dataProvider provideIsEmpty |
| 1368 |
*/ |
| 1369 |
public function testIsEmpty(bool $expected, string $origin) |
| 1370 |
{ |
| 1371 |
$this->assertSame($expected, static::createFromString($origin)->isEmpty()); |
| 1372 |
} |
| 1373 |
|
| 1374 |
public static function provideIsEmpty() |
| 1375 |
{ |
| 1376 |
return [ |
| 1377 |
[true, ''], |
| 1378 |
[false, ' '], |
| 1379 |
[false, "\n"], |
| 1380 |
[false, 'Foo bar'], |
| 1381 |
]; |
| 1382 |
} |
| 1383 |
|
| 1384 |
/** |
| 1385 |
* @dataProvider provideJoin |
| 1386 |
*/ |
| 1387 |
public function testJoin(string $expected, string $origin, array $join) |
| 1388 |
{ |
| 1389 |
$instance = static::createFromString($origin)->join($join); |
| 1390 |
|
| 1391 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1392 |
} |
| 1393 |
|
| 1394 |
public function testJoinWithLastGlue() |
| 1395 |
{ |
| 1396 |
$this->assertSame('foo, bar and baz', (string) static::createFromString(', ')->join(['foo', 'bar', 'baz'], ' and ')); |
| 1397 |
} |
| 1398 |
|
| 1399 |
public static function provideJoin() |
| 1400 |
{ |
| 1401 |
return [ |
| 1402 |
['', '', []], |
| 1403 |
['', ',', []], |
| 1404 |
['foo', ',', ['foo']], |
| 1405 |
['foobar', '', ['foo', 'bar']], |
| 1406 |
['foo, bar', ', ', ['foo', 'bar']], |
| 1407 |
]; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/** |
| 1411 |
* @dataProvider provideRepeat |
| 1412 |
*/ |
| 1413 |
public function testRepeat(string $expected, string $origin, int $multiplier) |
| 1414 |
{ |
| 1415 |
$instance = static::createFromString($origin)->repeat($multiplier); |
| 1416 |
|
| 1417 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1418 |
} |
| 1419 |
|
| 1420 |
public static function provideRepeat() |
| 1421 |
{ |
| 1422 |
return [ |
| 1423 |
['', '', 0], |
| 1424 |
['', '', 5], |
| 1425 |
['', 'foo', 0], |
| 1426 |
['foo', 'foo', 1], |
| 1427 |
['foofoofoo', 'foo', 3], |
| 1428 |
]; |
| 1429 |
} |
| 1430 |
|
| 1431 |
/** |
| 1432 |
* @dataProvider providePadBoth |
| 1433 |
*/ |
| 1434 |
public function testPadBoth(string $expected, string $origin, int $length, string $padStr) |
| 1435 |
{ |
| 1436 |
$instance = static::createFromString($origin)->padBoth($length, $padStr); |
| 1437 |
|
| 1438 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1439 |
} |
| 1440 |
|
| 1441 |
public static function providePadBoth() |
| 1442 |
{ |
| 1443 |
return [ |
| 1444 |
['', '', 0, '_'], |
| 1445 |
['###', '', 3, '#'], |
| 1446 |
['foo', 'foo', 2, '#'], |
| 1447 |
['foo', 'foo', 3, '#'], |
| 1448 |
['foo#', 'foo', 4, '#'], |
| 1449 |
['#foo#', 'foo', 5, '#'], |
| 1450 |
['##foo###', 'foo', 8, '#'], |
| 1451 |
['#+#foo#+#', 'foo', 9, '#+'], |
| 1452 |
]; |
| 1453 |
} |
| 1454 |
|
| 1455 |
/** |
| 1456 |
* @dataProvider providePadEnd |
| 1457 |
*/ |
| 1458 |
public function testPadEnd(string $expected, string $origin, int $length, string $padStr) |
| 1459 |
{ |
| 1460 |
$instance = static::createFromString($origin)->padEnd($length, $padStr); |
| 1461 |
|
| 1462 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1463 |
} |
| 1464 |
|
| 1465 |
public static function providePadEnd() |
| 1466 |
{ |
| 1467 |
return [ |
| 1468 |
['', '', 0, '_'], |
| 1469 |
['###', '', 3, '#'], |
| 1470 |
['foo', 'foo', 2, '#'], |
| 1471 |
['foo', 'foo', 3, '#'], |
| 1472 |
['foo#', 'foo', 4, '#'], |
| 1473 |
['foo###', 'foo', 6, '#'], |
| 1474 |
['foo#+#', 'foo', 6, '#+'], |
| 1475 |
]; |
| 1476 |
} |
| 1477 |
|
| 1478 |
/** |
| 1479 |
* @dataProvider providePadStart |
| 1480 |
*/ |
| 1481 |
public function testPadStart(string $expected, string $origin, int $length, string $padStr) |
| 1482 |
{ |
| 1483 |
$instance = static::createFromString($origin)->padStart($length, $padStr); |
| 1484 |
|
| 1485 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1486 |
} |
| 1487 |
|
| 1488 |
public static function providePadStart() |
| 1489 |
{ |
| 1490 |
return [ |
| 1491 |
['', '', 0, '_'], |
| 1492 |
['###', '', 3, '#'], |
| 1493 |
['foo', 'foo', 2, '#'], |
| 1494 |
['foo', 'foo', 3, '#'], |
| 1495 |
['#foo', 'foo', 4, '#'], |
| 1496 |
['###foo', 'foo', 6, '#'], |
| 1497 |
['#+#foo', 'foo', 6, '#+'], |
| 1498 |
]; |
| 1499 |
} |
| 1500 |
|
| 1501 |
/** |
| 1502 |
* @dataProvider provideTruncate |
| 1503 |
*/ |
| 1504 |
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true) |
| 1505 |
{ |
| 1506 |
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut); |
| 1507 |
|
| 1508 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1509 |
} |
| 1510 |
|
| 1511 |
public static function provideTruncate() |
| 1512 |
{ |
| 1513 |
return [ |
| 1514 |
['', '', 3, ''], |
| 1515 |
['', 'foo', 0, '...'], |
| 1516 |
['foo', 'foo', 0, '...', false], |
| 1517 |
['fo', 'foobar', 2, ''], |
| 1518 |
['foobar', 'foobar', 10, ''], |
| 1519 |
['foobar', 'foobar', 10, '...', false], |
| 1520 |
['foo', 'foo', 3, '...'], |
| 1521 |
['fo', 'foobar', 2, '...'], |
| 1522 |
['...', 'foobar', 3, '...'], |
| 1523 |
['fo...', 'foobar', 5, '...'], |
| 1524 |
['foobar...', 'foobar foo', 6, '...', false], |
| 1525 |
['foobar...', 'foobar foo', 7, '...', false], |
| 1526 |
['foobar foo...', 'foobar foo a', 10, '...', false], |
| 1527 |
['foobar foo aar', 'foobar foo aar', 12, '...', false], |
| 1528 |
]; |
| 1529 |
} |
| 1530 |
|
| 1531 |
public function testToString() |
| 1532 |
{ |
| 1533 |
$instance = static::createFromString('foobar'); |
| 1534 |
|
| 1535 |
self::assertSame('foobar', $instance->toString()); |
| 1536 |
} |
| 1537 |
|
| 1538 |
/** |
| 1539 |
* @dataProvider provideReverse |
| 1540 |
*/ |
| 1541 |
public function testReverse(string $expected, string $origin) |
| 1542 |
{ |
| 1543 |
$instance = static::createFromString($origin)->reverse(); |
| 1544 |
|
| 1545 |
$this->assertEquals(static::createFromString($expected), $instance); |
| 1546 |
} |
| 1547 |
|
| 1548 |
public static function provideReverse() |
| 1549 |
{ |
| 1550 |
return [ |
| 1551 |
['', ''], |
| 1552 |
['oof', 'foo'], |
| 1553 |
["\n!!!\tTAERG SI ynofmyS ", " Symfony IS GREAT\t!!!\n"], |
| 1554 |
]; |
| 1555 |
} |
| 1556 |
|
| 1557 |
/** |
| 1558 |
* @dataProvider provideWidth |
| 1559 |
*/ |
| 1560 |
public function testWidth(int $expected, string $origin, bool $ignoreAnsiDecoration = true) |
| 1561 |
{ |
| 1562 |
$this->assertSame($expected, static::createFromString($origin)->width($ignoreAnsiDecoration)); |
| 1563 |
} |
| 1564 |
|
| 1565 |
public static function provideWidth(): array |
| 1566 |
{ |
| 1567 |
return [ |
| 1568 |
[0, ''], |
| 1569 |
[1, 'c'], |
| 1570 |
[3, 'foo'], |
| 1571 |
[2, '⭐'], |
| 1572 |
[8, 'f⭐o⭐⭐'], |
| 1573 |
[19, 'コンニチハ, セカイ!'], |
| 1574 |
[6, "foo\u{0000}bar"], |
| 1575 |
[6, "foo\u{001b}[0mbar"], |
| 1576 |
[6, "foo\u{0001}bar"], |
| 1577 |
[6, "foo\u{0001}bar", false], |
| 1578 |
[4, '--ֿ--'], |
| 1579 |
[4, 'café'], |
| 1580 |
[1, 'А҈'], |
| 1581 |
[4, 'ᬓᬨᬮ᭄'], |
| 1582 |
[1, "\u{00AD}"], |
| 1583 |
[14, "\u{007f}\u{007f}f\u{001b}[0moo\u{0001}bar\u{007f}cccïf\u{008e}cy\u{0005}1"], // foobarcccïfcy1 |
| 1584 |
[17, "\u{007f}\u{007f}f\u{001b}[0moo\u{0001}bar\u{007f}cccïf\u{008e}cy\u{0005}1", false], // f[0moobarcccïfcy1 |
| 1585 |
]; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|