RandomApiMigrationFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Alias;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Vladimir Reznichenko <kalessil@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\AbstractFunctionReferenceFixer
  19. * @covers \PhpCsFixer\Fixer\Alias\RandomApiMigrationFixer
  20. */
  21. final class RandomApiMigrationFixerTest extends AbstractFixerTestCase
  22. {
  23. public function testConfigureCheckSearchFunction()
  24. {
  25. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  26. $this->expectExceptionMessageRegExp('#^\[random_api_migration\] Invalid configuration: Function "is_null" is not handled by the fixer\.$#');
  27. $this->fixer->configure(['replacements' => ['is_null' => 'random_int']]);
  28. }
  29. public function testConfigureCheckReplacementType()
  30. {
  31. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  32. $this->expectExceptionMessageRegExp('#^\[random_api_migration\] Invalid configuration: Replacement for function "rand" must be a string, "NULL" given\.$#');
  33. $this->fixer->configure(['replacements' => ['rand' => null]]);
  34. }
  35. public function testConfigure()
  36. {
  37. $this->fixer->configure(['replacements' => ['rand' => 'random_int']]);
  38. $this->assertSame(
  39. ['replacements' => [
  40. 'rand' => ['alternativeName' => 'random_int', 'argumentCount' => [0, 2]], ],
  41. ],
  42. static::getObjectAttribute($this->fixer, 'configuration')
  43. );
  44. }
  45. /**
  46. * @param string $expected
  47. * @param null|string $input
  48. * @param array $config
  49. *
  50. * @dataProvider provideFixCases
  51. */
  52. public function testFix($expected, $input = null, array $config = [])
  53. {
  54. $this->fixer->configure($config);
  55. $this->doTest($expected, $input);
  56. }
  57. /**
  58. * @return array[]
  59. */
  60. public function provideFixCases()
  61. {
  62. return [
  63. [
  64. '<?php random_int(0, getrandmax());',
  65. '<?php rand();',
  66. ['replacements' => ['rand' => 'random_int']],
  67. ],
  68. [
  69. '<?php random_int#1
  70. #2
  71. (0, getrandmax()#3
  72. #4
  73. )#5
  74. ;',
  75. '<?php rand#1
  76. #2
  77. (#3
  78. #4
  79. )#5
  80. ;',
  81. ['replacements' => ['rand' => 'random_int']],
  82. ],
  83. ['<?php $smth->srand($a);'],
  84. ['<?php srandSmth($a);'],
  85. ['<?php smth_srand($a);'],
  86. ['<?php new srand($a);'],
  87. ['<?php new Smth\\srand($a);'],
  88. ['<?php Smth\\srand($a);'],
  89. ['<?php namespace\\srand($a);'],
  90. ['<?php Smth::srand($a);'],
  91. ['<?php new srand\\smth($a);'],
  92. ['<?php srand::smth($a);'],
  93. ['<?php srand\\smth($a);'],
  94. ['<?php "SELECT ... srand(\$a) ...";'],
  95. ['<?php "SELECT ... SRAND($a) ...";'],
  96. ["<?php 'test'.'srand' . 'in concatenation';"],
  97. ['<?php "test" . "srand"."in concatenation";'],
  98. [
  99. '<?php
  100. class SrandClass
  101. {
  102. const srand = 1;
  103. public function srand($srand)
  104. {
  105. if (!defined("srand") || $srand instanceof srand) {
  106. echo srand;
  107. }
  108. }
  109. }
  110. class srand extends SrandClass{
  111. const srand = "srand";
  112. }
  113. ',
  114. ],
  115. ['<?php mt_srand($a);', '<?php srand($a);'],
  116. ['<?php \\mt_srand($a);', '<?php \\srand($a);'],
  117. ['<?php $a = &mt_srand($a);', '<?php $a = &srand($a);'],
  118. ['<?php $a = &\\mt_srand($a);', '<?php $a = &\\srand($a);'],
  119. ['<?php /* foo */ mt_srand /** bar */ ($a);', '<?php /* foo */ srand /** bar */ ($a);'],
  120. ['<?php a(mt_getrandmax ());', '<?php a(getrandmax ());'],
  121. ['<?php a(mt_rand());', '<?php a(rand());'],
  122. ['<?php a(mt_srand());', '<?php a(srand());'],
  123. ['<?php a(\\mt_srand());', '<?php a(\\srand());'],
  124. [
  125. '<?php rand(rand($a));',
  126. null,
  127. ['replacements' => ['rand' => 'random_int']],
  128. ],
  129. [
  130. '<?php random_int($d, random_int($a,$b));',
  131. '<?php rand($d, rand($a,$b));',
  132. ['replacements' => ['rand' => 'random_int']],
  133. ],
  134. [
  135. '<?php random_int($a, \Other\Scope\mt_rand($a));',
  136. '<?php rand($a, \Other\Scope\mt_rand($a));',
  137. ['replacements' => ['rand' => 'random_int']],
  138. ],
  139. [
  140. '<?php $a = random_int(1,2) + random_int(3,4);',
  141. '<?php $a = rand(1,2) + mt_rand(3,4);',
  142. ['replacements' => ['rand' => 'random_int', 'mt_rand' => 'random_int']],
  143. ],
  144. [
  145. '<?php
  146. interface Test
  147. {
  148. public function getrandmax();
  149. public function &rand();
  150. }',
  151. null,
  152. ['replacements' => ['rand' => 'random_int']],
  153. ],
  154. ];
  155. }
  156. }