PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPCompatibility/Tests/ParameterValues/NewIconvMbstringCharsetDefaultUnitTest.php

http://github.com/wimg/PHPCompatibility
PHP | 197 lines | 94 code | 20 blank | 83 comment | 5 complexity | 1a07926ec82cb22ace3d821df31bdea2 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * PHPCompatibility, an external standard for PHP_CodeSniffer.
  4. *
  5. * @package PHPCompatibility
  6. * @copyright 2012-2020 PHPCompatibility Contributors
  7. * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
  8. * @link https://github.com/PHPCompatibility/PHPCompatibility
  9. */
  10. namespace PHPCompatibility\Tests\ParameterValues;
  11. use PHPCompatibility\Tests\BaseSniffTest;
  12. /**
  13. * Test the NewIconvMbstringCharsetDefault sniff.
  14. *
  15. * @group newIconvMbstringCharsetDefault
  16. * @group parameterValues
  17. *
  18. * @covers \PHPCompatibility\Sniffs\ParameterValues\NewIconvMbstringCharsetDefaultSniff
  19. *
  20. * @since 9.3.0
  21. */
  22. class NewIconvMbstringCharsetDefaultUnitTest extends BaseSniffTest
  23. {
  24. /**
  25. * testNewIconvMbstringCharsetDefault
  26. *
  27. * @dataProvider dataNewIconvMbstringCharsetDefault
  28. *
  29. * @param int $line Line number where the error should occur.
  30. * @param string $function The name of the function called.
  31. * @param string $paramName The name of parameter which is missing.
  32. * Defaults to `$encoding`.
  33. *
  34. * @return void
  35. */
  36. public function testNewIconvMbstringCharsetDefault($line, $function, $paramName = '$encoding')
  37. {
  38. $file = $this->sniffFile(__FILE__, '5.4-7.0');
  39. $error = "The default value of the {$paramName} parameter for {$function}() was changed from ISO-8859-1 to UTF-8 in PHP 5.6";
  40. $this->assertError($file, $line, $error);
  41. }
  42. /**
  43. * Data provider.
  44. *
  45. * @see testNewIconvMbstringCharsetDefault()
  46. *
  47. * @return array
  48. */
  49. public function dataNewIconvMbstringCharsetDefault()
  50. {
  51. return [
  52. [44, 'iconv_mime_decode_headers', '$charset'],
  53. [45, 'iconv_mime_decode', '$charset'],
  54. [46, 'Iconv_Strlen', '$charset'],
  55. [47, 'iconv_strpos', '$charset'],
  56. [48, 'iconv_strrpos', '$charset'],
  57. [49, 'iconv_substr', '$charset'],
  58. [51, 'mb_check_encoding'],
  59. [52, 'MB_chr'],
  60. [53, 'mb_convert_case'],
  61. [54, 'mb_convert_encoding', '$from_encoding'],
  62. [55, 'mb_convert_kana'],
  63. [56, 'mb_decode_numericentity'],
  64. [57, 'mb_encode_numericentity'],
  65. [58, 'mb_ord'],
  66. [59, 'mb_scrub'],
  67. [60, 'mb_strcut'],
  68. [61, 'mb_stripos'],
  69. [62, 'mb_stristr'],
  70. [63, 'mb_strlen'],
  71. [64, 'mb_strpos'],
  72. [65, 'mb_strrchr'],
  73. [66, 'mb_strrichr'],
  74. [67, 'mb_strripos'],
  75. [68, 'mb_strrpos'],
  76. [69, 'mb_strstr'],
  77. [70, 'mb_strtolower'],
  78. [71, 'mb_strtoupper'],
  79. [72, 'mb_strwidth'],
  80. [73, 'mb_substr_count'],
  81. [74, 'mb_substr'],
  82. ];
  83. }
  84. /**
  85. * Test that there are no false positives.
  86. *
  87. * @return void
  88. */
  89. public function testNoFalsePositives()
  90. {
  91. $file = $this->sniffFile(__FILE__, '5.4-7.0');
  92. // No errors expected on the first 40 lines.
  93. for ($line = 1; $line <= 40; $line++) {
  94. $this->assertNoViolation($file, $line);
  95. }
  96. }
  97. /**
  98. * Test the special handling of calls to iconv_mime_encode().
  99. *
  100. * @dataProvider dataIconvMimeEncode
  101. *
  102. * @param int $line Line number where the error should occur.
  103. * @param string $missing The preferences which are missing.
  104. * @param string $type Whether an error or a warning is expected. Defaults to 'error'.
  105. *
  106. * @return void
  107. */
  108. public function testIconvMimeEncode($line, $missing, $type = 'error')
  109. {
  110. $file = $this->sniffFile(__FILE__, '5.4-7.0');
  111. $error = 'The default value of the %s parameter index for iconv_mime_encode() was changed from ISO-8859-1 to UTF-8 in PHP 5.6';
  112. $error = \sprintf($error, $missing);
  113. if ($type === 'error') {
  114. $this->assertError($file, $line, $error);
  115. } else {
  116. $this->assertWarning($file, $line, $error);
  117. }
  118. }
  119. /**
  120. * Data provider.
  121. *
  122. * @see testIconvMimeEncode()
  123. *
  124. * @return array
  125. */
  126. public function dataIconvMimeEncode()
  127. {
  128. return [
  129. [91, '$preferences[\'input/output-charset\']'],
  130. [92, '$preferences[\'input/output-charset\']', 'warning'],
  131. [96, '$preferences[\'output-charset\']'],
  132. [106, '$preferences[\'input-charset\']'],
  133. [115, '$preferences[\'input-charset\']'],
  134. ];
  135. }
  136. /**
  137. * Test that there are no false positives.
  138. *
  139. * @return void
  140. */
  141. public function testNoFalsePositivesIconvMimeEncode()
  142. {
  143. $file = $this->sniffFile(__FILE__, '5.4-7.0');
  144. // No errors expected on line 79 - 89.
  145. for ($line = 79; $line <= 89; $line++) {
  146. $this->assertNoViolation($file, $line);
  147. }
  148. }
  149. /**
  150. * Verify no notices are thrown at all.
  151. *
  152. * @dataProvider dataNoViolationsInFileOnValidVersion
  153. *
  154. * @param string $testVersion The testVersion to use.
  155. *
  156. * @return void
  157. */
  158. public function testNoViolationsInFileOnValidVersion($testVersion)
  159. {
  160. $file = $this->sniffFile(__FILE__, $testVersion);
  161. $this->assertNoViolation($file);
  162. }
  163. /**
  164. * Data provider.
  165. *
  166. * @see testNoViolationsInFileOnValidVersion()
  167. *
  168. * @return array
  169. */
  170. public function dataNoViolationsInFileOnValidVersion()
  171. {
  172. return [
  173. ['-5.5'],
  174. ['5.6-'],
  175. ];
  176. }
  177. }