UtilsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * @internal
  19. */
  20. final class UtilsTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @dataProvider provideCamelCaseToUnderscoreCases
  24. *
  25. * @param string $expected Camel case string.
  26. * @param string $input Input string.
  27. */
  28. public function testCamelCaseToUnderscore($expected, $input = null)
  29. {
  30. if (null !== $input) {
  31. $this->assertSame($expected, Utils::camelCaseToUnderscore($input));
  32. }
  33. $this->assertSame($expected, Utils::camelCaseToUnderscore($expected));
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function provideCamelCaseToUnderscoreCases()
  39. {
  40. return array(
  41. array(
  42. 'dollar_close_curly_braces',
  43. 'DollarCloseCurlyBraces',
  44. ),
  45. array(
  46. 'utf8_encoder_fixer',
  47. 'utf8EncoderFixer',
  48. ),
  49. array(
  50. 'terminated_with_number10',
  51. 'TerminatedWithNumber10',
  52. ),
  53. array(
  54. 'utf8_encoder_fixer',
  55. ),
  56. );
  57. }
  58. /**
  59. * @dataProvider provideCmpIntCases
  60. */
  61. public function testCmpInt($expected, $left, $right)
  62. {
  63. $this->assertSame($expected, Utils::cmpInt($left, $right));
  64. }
  65. public function provideCmpIntCases()
  66. {
  67. return array(
  68. array(0, 1, 1),
  69. array(0, -1, -1),
  70. array(-1, 10, 20),
  71. array(-1, -20, -10),
  72. array(1, 20, 10),
  73. array(1, -10, -20),
  74. );
  75. }
  76. /**
  77. * @dataProvider provideSplitLinesCases
  78. */
  79. public function testSplitLines(array $expected, $input)
  80. {
  81. $this->assertSame($expected, Utils::splitLines($input));
  82. }
  83. public function provideSplitLinesCases()
  84. {
  85. return array(
  86. array(
  87. array("\t aaa\n", " bbb\n", "\t"),
  88. "\t aaa\n bbb\n\t",
  89. ),
  90. array(
  91. array("aaa\r\n", " bbb\r\n"),
  92. "aaa\r\n bbb\r\n",
  93. ),
  94. array(
  95. array("aaa\r\n", " bbb\n"),
  96. "aaa\r\n bbb\n",
  97. ),
  98. array(
  99. array("aaa\r\n\n\n\r\n", " bbb\n"),
  100. "aaa\r\n\n\n\r\n bbb\n",
  101. ),
  102. );
  103. }
  104. /**
  105. * @dataProvider provideCalculateTrailingWhitespaceIndentCases
  106. */
  107. public function testCalculateTrailingWhitespaceIndent($spaces, $input)
  108. {
  109. $token = new Token(array(T_WHITESPACE, $input));
  110. $this->assertSame($spaces, Utils::calculateTrailingWhitespaceIndent($token));
  111. }
  112. public function provideCalculateTrailingWhitespaceIndentCases()
  113. {
  114. return array(
  115. array(' ', "\n\n "),
  116. array(' ', "\r\n\r\r\r "),
  117. array("\t", "\r\n\t"),
  118. array('', "\t\n\r"),
  119. array('', "\n"),
  120. array('', ''),
  121. );
  122. }
  123. /**
  124. * @expectedException \InvalidArgumentException
  125. * @expectedExceptionMessage The given token must be whitespace, got "T_STRING".
  126. */
  127. public function testCalculateTrailingWhitespaceIndentFail()
  128. {
  129. $token = new Token(array(T_STRING, 'foo'));
  130. Utils::calculateTrailingWhitespaceIndent($token);
  131. }
  132. }