RunnerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Runner;
  12. use PhpCsFixer\Cache\Directory;
  13. use PhpCsFixer\Cache\NullCacheManager;
  14. use PhpCsFixer\Differ\NullDiffer;
  15. use PhpCsFixer\Error\Error;
  16. use PhpCsFixer\Error\ErrorsManager;
  17. use PhpCsFixer\Fixer;
  18. use PhpCsFixer\Linter\Linter;
  19. use PhpCsFixer\Runner\Runner;
  20. use Prophecy\Argument;
  21. use Symfony\Component\Finder\Finder;
  22. /**
  23. * @internal
  24. */
  25. final class RunnerTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * @covers \PhpCsFixer\Runner\Runner::fix
  29. * @covers \PhpCsFixer\Runner\Runner::fixFile
  30. */
  31. public function testThatFixSuccessfully()
  32. {
  33. $linterProphecy = $this->prophesize('PhpCsFixer\Linter\LinterInterface');
  34. $linterProphecy
  35. ->isAsync()
  36. ->willReturn(false);
  37. $linterProphecy
  38. ->lintFile(Argument::type('string'))
  39. ->willReturn($this->prophesize('PhpCsFixer\Linter\LintingResultInterface')->reveal());
  40. $linterProphecy
  41. ->lintSource(Argument::type('string'))
  42. ->willReturn($this->prophesize('PhpCsFixer\Linter\LintingResultInterface')->reveal());
  43. $fixers = array(
  44. new Fixer\ClassNotation\VisibilityRequiredFixer(),
  45. new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
  46. );
  47. $expectedChangedInfo = array(
  48. 'appliedFixers' => array('visibility_required'),
  49. 'diff' => '',
  50. );
  51. $path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FixerTest'.DIRECTORY_SEPARATOR.'fix';
  52. $runner = new Runner(
  53. Finder::create()->in($path),
  54. $fixers,
  55. new NullDiffer(),
  56. null,
  57. new ErrorsManager(),
  58. $linterProphecy->reveal(),
  59. true,
  60. new NullCacheManager(),
  61. new Directory($path),
  62. false
  63. );
  64. $changed = $runner->fix();
  65. $this->assertCount(2, $changed);
  66. $this->assertArraySubset($expectedChangedInfo, array_pop($changed));
  67. $this->assertArraySubset($expectedChangedInfo, array_pop($changed));
  68. $path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FixerTest'.DIRECTORY_SEPARATOR.'fix';
  69. $runner = new Runner(
  70. Finder::create()->in($path),
  71. $fixers,
  72. new NullDiffer(),
  73. null,
  74. new ErrorsManager(),
  75. $linterProphecy->reveal(),
  76. true,
  77. new NullCacheManager(),
  78. new Directory($path),
  79. true
  80. );
  81. $changed = $runner->fix();
  82. $this->assertCount(1, $changed);
  83. $this->assertArraySubset($expectedChangedInfo, array_pop($changed));
  84. }
  85. /**
  86. * @covers \PhpCsFixer\Runner\Runner::fix
  87. * @covers \PhpCsFixer\Runner\Runner::fixFile
  88. */
  89. public function testThatFixInvalidFileReportsToErrorManager()
  90. {
  91. $errorsManager = new ErrorsManager();
  92. $path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'FixerTest'.DIRECTORY_SEPARATOR.'invalid';
  93. $runner = new Runner(
  94. Finder::create()->in($path),
  95. array(
  96. new Fixer\ClassNotation\VisibilityRequiredFixer(),
  97. new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
  98. ),
  99. new NullDiffer(),
  100. null,
  101. $errorsManager,
  102. new Linter(),
  103. true,
  104. new NullCacheManager()
  105. );
  106. $changed = $runner->fix();
  107. $pathToInvalidFile = $path.DIRECTORY_SEPARATOR.'somefile.php';
  108. $this->assertCount(0, $changed);
  109. $errors = $errorsManager->getInvalidErrors();
  110. $this->assertCount(1, $errors);
  111. $error = $errors[0];
  112. $this->assertInstanceOf('PhpCsFixer\Error\Error', $error);
  113. $this->assertSame(Error::TYPE_INVALID, $error->getType());
  114. $this->assertSame($pathToInvalidFile, $error->getFilePath());
  115. }
  116. }