WarningsDetectorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console;
  13. use PhpCsFixer\Console\WarningsDetector;
  14. use PhpCsFixer\Tests\TestCase;
  15. use PhpCsFixer\ToolInfoInterface;
  16. /**
  17. * @author ntzm
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Console\WarningsDetector
  22. */
  23. final class WarningsDetectorTest extends TestCase
  24. {
  25. public function testDetectOldVendorNotInstalledByComposer(): void
  26. {
  27. $toolInfo = $this->createToolInfoDouble(false, 'not-installed-by-composer');
  28. $warningsDetector = new WarningsDetector($toolInfo);
  29. $warningsDetector->detectOldVendor();
  30. self::assertSame([], $warningsDetector->getWarnings());
  31. }
  32. public function testDetectOldVendorNotLegacyPackage(): void
  33. {
  34. $toolInfo = $this->createToolInfoDouble(false, 'friendsofphp/php-cs-fixer');
  35. $warningsDetector = new WarningsDetector($toolInfo);
  36. $warningsDetector->detectOldVendor();
  37. self::assertSame([], $warningsDetector->getWarnings());
  38. }
  39. public function testDetectOldVendorLegacyPackage(): void
  40. {
  41. $toolInfo = $this->createToolInfoDouble(true, 'fabpot/php-cs-fixer');
  42. $warningsDetector = new WarningsDetector($toolInfo);
  43. $warningsDetector->detectOldVendor();
  44. self::assertSame([
  45. 'You are running PHP CS Fixer installed with old vendor `fabpot/php-cs-fixer`. Please update to `friendsofphp/php-cs-fixer`.',
  46. 'If you need help while solving warnings, ask at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/discussions/, we will help you!',
  47. ], $warningsDetector->getWarnings());
  48. }
  49. private function createToolInfoDouble(bool $isInstalledByComposer, string $packageName): ToolInfoInterface
  50. {
  51. $composerInstallationDetails = [
  52. 'name' => $packageName,
  53. 'version' => '1.0.0',
  54. 'dist' => [],
  55. ];
  56. return new class($isInstalledByComposer, $composerInstallationDetails) implements ToolInfoInterface {
  57. private bool $isInstalledByComposer;
  58. /** @var array{name: string, version: string, dist: array{reference?: string}} */
  59. private array $composerInstallationDetails;
  60. /**
  61. * @param array{name: string, version: string, dist: array{reference?: string}} $composerInstallationDetails
  62. */
  63. public function __construct(bool $isInstalledByComposer, array $composerInstallationDetails)
  64. {
  65. $this->isInstalledByComposer = $isInstalledByComposer;
  66. $this->composerInstallationDetails = $composerInstallationDetails;
  67. }
  68. public function getComposerInstallationDetails(): array
  69. {
  70. return $this->composerInstallationDetails;
  71. }
  72. public function getComposerVersion(): string
  73. {
  74. throw new \LogicException('Not implemented.');
  75. }
  76. public function getVersion(): string
  77. {
  78. throw new \LogicException('Not implemented.');
  79. }
  80. public function isInstalledAsPhar(): bool
  81. {
  82. throw new \LogicException('Not implemented.');
  83. }
  84. public function isInstalledByComposer(): bool
  85. {
  86. return $this->isInstalledByComposer;
  87. }
  88. public function isRunInsideDocker(): bool
  89. {
  90. return false;
  91. }
  92. public function getPharDownloadUri(string $version): string
  93. {
  94. throw new \LogicException('Not implemented.');
  95. }
  96. };
  97. }
  98. }