RunnerTest.php 4.0 KB

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