StrTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Tests;
  3. use think\helper\Str;
  4. class StrTest extends TestCase
  5. {
  6. public function testCamel()
  7. {
  8. $this->assertSame('fooBar', Str::camel('FooBar'));
  9. $this->assertSame('fooBar', Str::camel('FooBar'));
  10. $this->assertSame('fooBar', Str::camel('foo_bar'));
  11. $this->assertSame('fooBar', Str::camel('_foo_bar'));
  12. $this->assertSame('fooBar', Str::camel('_foo_bar_'));
  13. }
  14. public function testStudly()
  15. {
  16. $this->assertSame('FooBar', Str::studly('fooBar'));
  17. $this->assertSame('FooBar', Str::studly('_foo_bar'));
  18. $this->assertSame('FooBar', Str::studly('_foo_bar_'));
  19. $this->assertSame('FooBar', Str::studly('_foo_bar_'));
  20. }
  21. public function testSnake()
  22. {
  23. $this->assertSame('think_p_h_p_framework', Str::snake('ThinkPHPFramework'));
  24. $this->assertSame('think_php_framework', Str::snake('ThinkPhpFramework'));
  25. $this->assertSame('think php framework', Str::snake('ThinkPhpFramework', ' '));
  26. $this->assertSame('think_php_framework', Str::snake('Think Php Framework'));
  27. $this->assertSame('think_php_framework', Str::snake('Think Php Framework '));
  28. // ensure cache keys don't overlap
  29. $this->assertSame('think__php__framework', Str::snake('ThinkPhpFramework', '__'));
  30. $this->assertSame('think_php_framework_', Str::snake('ThinkPhpFramework_', '_'));
  31. $this->assertSame('think_php_framework', Str::snake('think php Framework'));
  32. $this->assertSame('think_php_frame_work', Str::snake('think php FrameWork'));
  33. // prevent breaking changes
  34. $this->assertSame('foo-bar', Str::snake('foo-bar'));
  35. $this->assertSame('foo-_bar', Str::snake('Foo-Bar'));
  36. $this->assertSame('foo__bar', Str::snake('Foo_Bar'));
  37. $this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka'));
  38. }
  39. public function testTitle()
  40. {
  41. $this->assertSame('Welcome Back', Str::title('welcome back'));
  42. }
  43. public function testRandom()
  44. {
  45. $this->assertIsString(Str::random(10));
  46. }
  47. public function testUpper()
  48. {
  49. $this->assertSame('USERNAME', Str::upper('username'));
  50. $this->assertSame('USERNAME', Str::upper('userNaMe'));
  51. }
  52. }