UtilsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests;
  11. use Symfony\CS\Tokenizer\Token;
  12. use Symfony\CS\Utils;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. * @author Graham Campbell <graham@mineuk.com>
  16. * @author Odín del Río <odin.drp@gmail.com>
  17. */
  18. class UtilsTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @dataProvider provideCamelCaseToUnderscoreCases
  22. *
  23. * @param string $expected Camel case string.
  24. * @param string $input Input string.
  25. */
  26. public function testCamelCaseToUnderscore($expected, $input = null)
  27. {
  28. if (null !== $input) {
  29. $this->assertSame($expected, Utils::camelCaseToUnderscore($input));
  30. }
  31. $this->assertSame($expected, Utils::camelCaseToUnderscore($expected));
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function provideCamelCaseToUnderscoreCases()
  37. {
  38. return array(
  39. array(
  40. 'dollar_close_curly_braces',
  41. 'DollarCloseCurlyBraces',
  42. ),
  43. array(
  44. 'utf8_encoder_fixer',
  45. 'utf8EncoderFixer',
  46. ),
  47. array(
  48. 'terminated_with_number10',
  49. 'TerminatedWithNumber10',
  50. ),
  51. array(
  52. 'utf8_encoder_fixer',
  53. ),
  54. );
  55. }
  56. /**
  57. * @dataProvider provideCmpIntCases
  58. */
  59. public function testCmpInt($expected, $left, $right)
  60. {
  61. $this->assertSame($expected, Utils::cmpInt($left, $right));
  62. }
  63. public function provideCmpIntCases()
  64. {
  65. return array(
  66. array(0, 1, 1),
  67. array(0, -1, -1),
  68. array(-1, 10, 20),
  69. array(-1, -20, -10),
  70. array(1, 20, 10),
  71. array(1, -10, -20),
  72. );
  73. }
  74. /**
  75. * @dataProvider provideSplitLinesCases
  76. */
  77. public function testSplitLines(array $expected, $input)
  78. {
  79. $this->assertSame($expected, Utils::splitLines($input));
  80. }
  81. public function provideSplitLinesCases()
  82. {
  83. return array(
  84. array(
  85. array("\t aaa\n", " bbb\n", "\t"),
  86. "\t aaa\n bbb\n\t",
  87. ),
  88. array(
  89. array("aaa\r\n", " bbb\r\n"),
  90. "aaa\r\n bbb\r\n",
  91. ),
  92. array(
  93. array("aaa\r\n", " bbb\n"),
  94. "aaa\r\n bbb\n",
  95. ),
  96. array(
  97. array("aaa\r\n\n\n\r\n", " bbb\n"),
  98. "aaa\r\n\n\n\r\n bbb\n",
  99. ),
  100. );
  101. }
  102. /**
  103. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  104. */
  105. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  106. {
  107. $token = new Token(array(T_WHITESPACE, $input));
  108. $this->assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  109. }
  110. public function provideCalculateTrailingWhitespaceIndentCases()
  111. {
  112. return array(
  113. array(' ', "\n\n "),
  114. array(' ', "\r\n\r\r\r "),
  115. array("\t", "\r\n\t"),
  116. array('', "\t\n\r"),
  117. array('', "\n"),
  118. array('', ''),
  119. );
  120. }
  121. /**
  122. * @expectedException \InvalidArgumentException
  123. * @expectedExceptionMessage The given token must be whitespace, got "T_STRING".
  124. */
  125. public function testCalculateTrailingWhitespaceIndentFail()
  126. {
  127. $token = new Token(array(T_STRING, 'foo'));
  128. Utils::calculateTrailingWhitespaceIndent($token);
  129. }
  130. }