PhpUnitConstructFixerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\PhpUnit;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer
  19. */
  20. final class PhpUnitConstructFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideTestFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->fixer->configure(['assertions' => [
  31. 'assertEquals',
  32. 'assertSame',
  33. 'assertNotEquals',
  34. 'assertNotSame',
  35. ]]);
  36. $this->doTest($expected, $input);
  37. foreach (['assertSame', 'assertEquals', 'assertNotEquals', 'assertNotSame'] as $method) {
  38. $this->fixer->configure(['assertions' => [$method]]);
  39. $this->doTest(
  40. $expected,
  41. $input && false !== strpos($input, $method) ? $input : null
  42. );
  43. }
  44. }
  45. public function provideTestFixCases()
  46. {
  47. $cases = [
  48. ['<?php $sth->assertSame(true, $foo);'],
  49. ['<?php $this->assertSame($b, null);'],
  50. [
  51. '<?php $this->assertNull(/*bar*/ $a);',
  52. '<?php $this->assertSame(null /*foo*/, /*bar*/ $a);',
  53. ],
  54. [
  55. '<?php $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException());',
  56. ],
  57. [
  58. '<?php $this->assertSame(null /*comment*/ === $eventException ? $exception : $eventException, $event->getException());',
  59. ],
  60. [
  61. '<?php
  62. $this->assertTrue(
  63. $a,
  64. "foo" . $bar
  65. );',
  66. '<?php
  67. $this->assertSame(
  68. true,
  69. $a,
  70. "foo" . $bar
  71. );',
  72. ],
  73. [
  74. '<?php
  75. $this->assertTrue(#
  76. #
  77. $a,#
  78. "foo" . $bar#
  79. );',
  80. '<?php
  81. $this->assertSame(#
  82. true,#
  83. $a,#
  84. "foo" . $bar#
  85. );',
  86. ],
  87. [
  88. '<?php $this->assertSame("a", $a); $this->assertTrue($b);',
  89. '<?php $this->assertSame("a", $a); $this->assertSame(true, $b);',
  90. ],
  91. [
  92. '<?php $this->assertSame(true || $a, $b); $this->assertTrue($c);',
  93. '<?php $this->assertSame(true || $a, $b); $this->assertSame(true, $c);',
  94. ],
  95. ];
  96. return array_merge(
  97. $cases,
  98. $this->generateCases('<?php $this->assert%s%s($a); //%s %s', '<?php $this->assert%s(%s, $a); //%s %s'),
  99. $this->generateCases('<?php $this->assert%s%s($a, "%s", "%s");', '<?php $this->assert%s(%s, $a, "%s", "%s");')
  100. );
  101. }
  102. public function testInvalidConfig()
  103. {
  104. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  105. $this->expectExceptionMessageRegExp('/^\[php_unit_construct\] Invalid configuration: The option "assertions" .*\.$/');
  106. $this->fixer->configure(['assertions' => ['__TEST__']]);
  107. }
  108. private function generateCases($expectedTemplate, $inputTemplate)
  109. {
  110. $cases = [];
  111. $functionTypes = ['Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false];
  112. foreach (['true', 'false', 'null'] as $type) {
  113. foreach ($functionTypes as $method => $positive) {
  114. $cases[] = [
  115. sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type),
  116. sprintf($inputTemplate, $method, $type, $method, $type),
  117. ];
  118. }
  119. }
  120. return $cases;
  121. }
  122. }