SelfUpdateCommandTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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\Command;
  13. use org\bovigo\vfs\vfsStream;
  14. use org\bovigo\vfs\vfsStreamDirectory;
  15. use org\bovigo\vfs\vfsStreamException;
  16. use org\bovigo\vfs\vfsStreamWrapper;
  17. use PhpCsFixer\Console\Application;
  18. use PhpCsFixer\Console\Command\SelfUpdateCommand;
  19. use PhpCsFixer\Console\SelfUpdate\GithubClientInterface;
  20. use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
  21. use PhpCsFixer\Console\SelfUpdate\NewVersionCheckerInterface;
  22. use PhpCsFixer\PharCheckerInterface;
  23. use PhpCsFixer\Tests\TestCase;
  24. use PhpCsFixer\ToolInfoInterface;
  25. use Symfony\Component\Console\Command\Command;
  26. use Symfony\Component\Console\Tester\CommandTester;
  27. /**
  28. * @internal
  29. *
  30. * @covers \PhpCsFixer\Console\Command\SelfUpdateCommand
  31. */
  32. final class SelfUpdateCommandTest extends TestCase
  33. {
  34. private ?vfsStreamDirectory $root = null;
  35. protected function setUp(): void
  36. {
  37. parent::setUp();
  38. $this->root = vfsStream::setup();
  39. file_put_contents($this->getToolPath(), 'Current PHP CS Fixer.');
  40. file_put_contents($this->root->url().'/'.self::getNewMinorReleaseVersion().'.phar', 'New minor version of PHP CS Fixer.');
  41. file_put_contents($this->root->url().'/'.self::getNewMajorReleaseVersion().'.phar', 'New major version of PHP CS Fixer.');
  42. }
  43. protected function tearDown(): void
  44. {
  45. parent::tearDown();
  46. $this->root = null;
  47. try {
  48. vfsStreamWrapper::unregister();
  49. } catch (vfsStreamException $exception) {
  50. // ignored
  51. }
  52. }
  53. /**
  54. * @dataProvider provideCommandNameCases
  55. */
  56. public function testCommandName(string $name): void
  57. {
  58. $command = new SelfUpdateCommand(
  59. $this->createNewVersionCheckerDouble(),
  60. $this->createToolInfoDouble(),
  61. $this->createPharCheckerDouble(),
  62. );
  63. $application = new Application();
  64. $application->add($command);
  65. self::assertSame($command, $application->find($name));
  66. }
  67. /**
  68. * @return iterable<array{string}>
  69. */
  70. public static function provideCommandNameCases(): iterable
  71. {
  72. yield ['self-update'];
  73. yield ['selfupdate'];
  74. }
  75. /**
  76. * @param array<string, bool|string> $input
  77. *
  78. * @dataProvider provideExecuteCases
  79. */
  80. public function testExecute(
  81. string $latestVersion,
  82. ?string $latestMinorVersion,
  83. array $input,
  84. bool $decorated,
  85. string $expectedFileContents,
  86. string $expectedDisplay
  87. ): void {
  88. $versionChecker = $this->createNewVersionCheckerDouble($latestVersion, $latestMinorVersion);
  89. $command = new SelfUpdateCommand(
  90. $versionChecker,
  91. $this->createToolInfoDouble(),
  92. $this->createPharCheckerDouble(),
  93. );
  94. $commandTester = $this->execute($command, $input, $decorated);
  95. self::assertSame($expectedFileContents, file_get_contents($this->getToolPath()));
  96. self::assertDisplay($expectedDisplay, $commandTester);
  97. self::assertSame(0, $commandTester->getStatusCode());
  98. }
  99. public static function provideExecuteCases(): iterable
  100. {
  101. $currentVersion = Application::VERSION;
  102. $minorRelease = self::getNewMinorReleaseVersion();
  103. $majorRelease = self::getNewMajorReleaseVersion();
  104. $major = self::getNewMajorVersion();
  105. $currentContents = 'Current PHP CS Fixer.';
  106. $minorContents = 'New minor version of PHP CS Fixer.';
  107. $majorContents = 'New major version of PHP CS Fixer.';
  108. $upToDateDisplay = "\033[32mPHP CS Fixer is already up-to-date.\033[39m\n";
  109. $newMinorDisplay = "\033[32mPHP CS Fixer updated\033[39m (\033[33m{$currentVersion}\033[39m -> \033[33m{$minorRelease}\033[39m)\n";
  110. $newMajorDisplay = "\033[32mPHP CS Fixer updated\033[39m (\033[33m{$currentVersion}\033[39m -> \033[33m{$majorRelease}\033[39m)\n";
  111. $majorInfoNoMinorDisplay = <<<OUTPUT
  112. \033[32mA new major version of PHP CS Fixer is available\033[39m (\033[33m{$majorRelease}\033[39m)
  113. \033[32mBefore upgrading please read\033[39m https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/{$majorRelease}/UPGRADE-v{$major}.md
  114. \033[32mIf you are ready to upgrade run this command with\033[39m \033[33m-f\033[39m
  115. \033[32mChecking for new minor/patch version...\033[39m
  116. \033[32mNo minor update for PHP CS Fixer.\033[39m
  117. OUTPUT;
  118. $majorInfoNewMinorDisplay = <<<OUTPUT
  119. \033[32mA new major version of PHP CS Fixer is available\033[39m (\033[33m{$majorRelease}\033[39m)
  120. \033[32mBefore upgrading please read\033[39m https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/{$majorRelease}/UPGRADE-v{$major}.md
  121. \033[32mIf you are ready to upgrade run this command with\033[39m \033[33m-f\033[39m
  122. \033[32mChecking for new minor/patch version...\033[39m
  123. \033[32mPHP CS Fixer updated\033[39m (\033[33m{$currentVersion}\033[39m -> \033[33m{$minorRelease}\033[39m)
  124. OUTPUT;
  125. // no new version available
  126. yield [Application::VERSION, Application::VERSION, [], true, $currentContents, $upToDateDisplay];
  127. yield [Application::VERSION, Application::VERSION, [], false, $currentContents, $upToDateDisplay];
  128. yield [Application::VERSION, Application::VERSION, ['--force' => true], true, $currentContents, $upToDateDisplay];
  129. yield [Application::VERSION, Application::VERSION, ['-f' => true], false, $currentContents, $upToDateDisplay];
  130. // new minor version available
  131. yield [$minorRelease, $minorRelease, [], true, $minorContents, $newMinorDisplay];
  132. yield [$minorRelease, $minorRelease, ['--force' => true], true, $minorContents, $newMinorDisplay];
  133. yield [$minorRelease, $minorRelease, ['-f' => true], true, $minorContents, $newMinorDisplay];
  134. yield [$minorRelease, $minorRelease, [], false, $minorContents, $newMinorDisplay];
  135. yield [$minorRelease, $minorRelease, ['--force' => true], false, $minorContents, $newMinorDisplay];
  136. yield [$minorRelease, $minorRelease, ['-f' => true], false, $minorContents, $newMinorDisplay];
  137. // new major version available
  138. yield [$majorRelease, Application::VERSION, [], true, $currentContents, $majorInfoNoMinorDisplay];
  139. yield [$majorRelease, Application::VERSION, [], false, $currentContents, $majorInfoNoMinorDisplay];
  140. yield [$majorRelease, Application::VERSION, ['--force' => true], true, $majorContents, $newMajorDisplay];
  141. yield [$majorRelease, Application::VERSION, ['-f' => true], false, $majorContents, $newMajorDisplay];
  142. // new minor version and new major version available
  143. yield [$majorRelease, $minorRelease, [], true, $minorContents, $majorInfoNewMinorDisplay];
  144. yield [$majorRelease, $minorRelease, [], false, $minorContents, $majorInfoNewMinorDisplay];
  145. yield [$majorRelease, $minorRelease, ['--force' => true], true, $majorContents, $newMajorDisplay];
  146. yield [$majorRelease, $minorRelease, ['-f' => true], false, $majorContents, $newMajorDisplay];
  147. // weird/unexpected versions
  148. yield ['v0.1.0', 'v0.1.0', [], true, $currentContents, $upToDateDisplay];
  149. yield ['v0.1.0', 'v0.1.0', [], false, $currentContents, $upToDateDisplay];
  150. yield ['v0.1.0', 'v0.1.0', ['--force' => true], true, $currentContents, $upToDateDisplay];
  151. yield ['v0.1.0', 'v0.1.0', ['-f' => true], false, $currentContents, $upToDateDisplay];
  152. yield ['v0.1.0', null, [], true, $currentContents, $upToDateDisplay];
  153. yield ['v0.1.0', null, [], false, $currentContents, $upToDateDisplay];
  154. yield ['v0.1.0', null, ['--force' => true], true, $currentContents, $upToDateDisplay];
  155. yield ['v0.1.0', null, ['-f' => true], false, $currentContents, $upToDateDisplay];
  156. yield ['v0.1.0', Application::VERSION, [], true, $currentContents, $upToDateDisplay];
  157. yield ['v0.1.0', Application::VERSION, [], false, $currentContents, $upToDateDisplay];
  158. yield ['v0.1.0', Application::VERSION, ['--force' => true], true, $currentContents, $upToDateDisplay];
  159. yield ['v0.1.0', Application::VERSION, ['-f' => true], false, $currentContents, $upToDateDisplay];
  160. yield [Application::VERSION, 'v0.1.0', [], true, $currentContents, $upToDateDisplay];
  161. yield [Application::VERSION, 'v0.1.0', [], false, $currentContents, $upToDateDisplay];
  162. yield [Application::VERSION, 'v0.1.0', ['--force' => true], true, $currentContents, $upToDateDisplay];
  163. yield [Application::VERSION, 'v0.1.0', ['-f' => true], false, $currentContents, $upToDateDisplay];
  164. }
  165. /**
  166. * @param array<string, bool|string> $input
  167. *
  168. * @dataProvider provideExecuteWhenNotAbleToGetLatestVersionsCases
  169. */
  170. public function testExecuteWhenNotAbleToGetLatestVersions(
  171. bool $latestMajorVersionSuccess,
  172. bool $latestMinorVersionSuccess,
  173. array $input,
  174. bool $decorated
  175. ): void {
  176. $versionChecker = $this->createNewVersionCheckerDouble(
  177. self::getNewMajorReleaseVersion(),
  178. self::getNewMinorReleaseVersion(),
  179. $latestMajorVersionSuccess,
  180. $latestMinorVersionSuccess,
  181. );
  182. $command = new SelfUpdateCommand(
  183. $versionChecker,
  184. $this->createToolInfoDouble(),
  185. $this->createPharCheckerDouble(),
  186. );
  187. $commandTester = $this->execute($command, $input, $decorated);
  188. self::assertDisplay(
  189. "\033[37;41mUnable to determine newest version: Foo.\033[39;49m\n",
  190. $commandTester
  191. );
  192. self::assertSame(1, $commandTester->getStatusCode());
  193. }
  194. public static function provideExecuteWhenNotAbleToGetLatestVersionsCases(): iterable
  195. {
  196. yield [false, false, [], true];
  197. yield [false, false, ['--force' => true], true];
  198. yield [false, false, ['-f' => true], true];
  199. yield [false, false, [], false];
  200. yield [false, false, ['--force' => true], false];
  201. yield [false, false, ['-f' => true], false];
  202. yield [true, false, [], true];
  203. yield [true, false, ['--force' => true], true];
  204. yield [true, false, ['-f' => true], true];
  205. yield [true, false, [], false];
  206. yield [true, false, ['--force' => true], false];
  207. yield [true, false, ['-f' => true], false];
  208. yield [false, true, [], true];
  209. yield [false, true, ['--force' => true], true];
  210. yield [false, true, ['-f' => true], true];
  211. yield [false, true, [], false];
  212. yield [false, true, ['--force' => true], false];
  213. yield [false, true, ['-f' => true], false];
  214. }
  215. /**
  216. * @param array<string, bool|string> $input
  217. *
  218. * @dataProvider provideExecuteWhenNotInstalledAsPharCases
  219. */
  220. public function testExecuteWhenNotInstalledAsPhar(array $input, bool $decorated): void
  221. {
  222. $command = new SelfUpdateCommand(
  223. $this->createNewVersionCheckerDouble(),
  224. $this->createToolInfoDouble(false),
  225. $this->createPharCheckerDouble(),
  226. );
  227. $commandTester = $this->execute($command, $input, $decorated);
  228. self::assertDisplay(
  229. "\033[37;41mSelf-update is available only for PHAR version.\033[39;49m\n",
  230. $commandTester
  231. );
  232. self::assertSame(1, $commandTester->getStatusCode());
  233. }
  234. public static function provideExecuteWhenNotInstalledAsPharCases(): iterable
  235. {
  236. yield [[], true];
  237. yield [['--force' => true], true];
  238. yield [['-f' => true], true];
  239. yield [[], false];
  240. yield [['--force' => true], false];
  241. yield [['-f' => true], false];
  242. }
  243. /**
  244. * @param array<string, bool|string> $input
  245. */
  246. private function execute(Command $command, array $input, bool $decorated): CommandTester
  247. {
  248. $application = new Application();
  249. $application->add($command);
  250. $input = ['command' => $command->getName()] + $input;
  251. $commandTester = new CommandTester($command);
  252. $realPath = $_SERVER['argv'][0];
  253. $_SERVER['argv'][0] = $this->getToolPath();
  254. $commandTester->execute($input, ['decorated' => $decorated]);
  255. $_SERVER['argv'][0] = $realPath;
  256. return $commandTester;
  257. }
  258. private static function assertDisplay(string $expectedDisplay, CommandTester $commandTester): void
  259. {
  260. if (!$commandTester->getOutput()->isDecorated()) {
  261. $expectedDisplay = preg_replace("/\033\\[(\\d+;)*\\d+m/", '', $expectedDisplay);
  262. }
  263. self::assertSame(
  264. $expectedDisplay,
  265. $commandTester->getDisplay(true)
  266. );
  267. }
  268. private function createToolInfoDouble(bool $isInstalledAsPhar = true): ToolInfoInterface
  269. {
  270. return new class($this->root, $isInstalledAsPhar) implements ToolInfoInterface {
  271. private vfsStreamDirectory $directory;
  272. private bool $isInstalledAsPhar;
  273. public function __construct(vfsStreamDirectory $directory, bool $isInstalledAsPhar)
  274. {
  275. $this->directory = $directory;
  276. $this->isInstalledAsPhar = $isInstalledAsPhar;
  277. }
  278. public function getComposerInstallationDetails(): array
  279. {
  280. throw new \LogicException('Not implemented.');
  281. }
  282. public function getComposerVersion(): string
  283. {
  284. throw new \LogicException('Not implemented.');
  285. }
  286. public function getVersion(): string
  287. {
  288. throw new \LogicException('Not implemented.');
  289. }
  290. public function isInstalledAsPhar(): bool
  291. {
  292. return $this->isInstalledAsPhar;
  293. }
  294. public function isInstalledByComposer(): bool
  295. {
  296. throw new \LogicException('Not implemented.');
  297. }
  298. public function isRunInsideDocker(): bool
  299. {
  300. return false;
  301. }
  302. public function getPharDownloadUri(string $version): string
  303. {
  304. return \sprintf('%s/%s.phar', $this->directory->url(), $version);
  305. }
  306. };
  307. }
  308. private function getToolPath(): string
  309. {
  310. return "{$this->root->url()}/php-cs-fixer";
  311. }
  312. private static function getCurrentMajorVersion(): int
  313. {
  314. return (int) preg_replace('/^v?(\d+).*$/', '$1', Application::VERSION);
  315. }
  316. private static function getNewMinorReleaseVersion(): string
  317. {
  318. return self::getCurrentMajorVersion().'.999.0';
  319. }
  320. private static function getNewMajorVersion(): int
  321. {
  322. return self::getCurrentMajorVersion() + 1;
  323. }
  324. private static function getNewMajorReleaseVersion(): string
  325. {
  326. return self::getNewMajorVersion().'.0.0';
  327. }
  328. private function createNewVersionCheckerDouble(
  329. string $latestVersion = Application::VERSION,
  330. ?string $latestMinorVersion = Application::VERSION,
  331. bool $latestMajorVersionSuccess = true,
  332. bool $latestMinorVersionSuccess = true
  333. ): NewVersionCheckerInterface {
  334. return new class($latestVersion, $latestMinorVersion, $latestMajorVersionSuccess, $latestMinorVersionSuccess) implements NewVersionCheckerInterface {
  335. private string $latestVersion;
  336. private ?string $latestMinorVersion;
  337. private bool $latestMajorVersionSuccess;
  338. private bool $latestMinorVersionSuccess;
  339. public function __construct(
  340. string $latestVersion,
  341. ?string $latestMinorVersion,
  342. bool $latestMajorVersionSuccess = true,
  343. bool $latestMinorVersionSuccess = true
  344. ) {
  345. $this->latestVersion = $latestVersion;
  346. $this->latestMinorVersion = $latestMinorVersion;
  347. $this->latestMajorVersionSuccess = $latestMajorVersionSuccess;
  348. $this->latestMinorVersionSuccess = $latestMinorVersionSuccess;
  349. }
  350. public function getLatestVersion(): string
  351. {
  352. if ($this->latestMajorVersionSuccess) {
  353. return $this->latestVersion;
  354. }
  355. throw new \RuntimeException('Foo.');
  356. }
  357. public function getLatestVersionOfMajor(int $majorVersion): ?string
  358. {
  359. TestCase::assertSame((int) preg_replace('/^v?(\d+).*$/', '$1', Application::VERSION), $majorVersion);
  360. if ($this->latestMinorVersionSuccess) {
  361. return $this->latestMinorVersion;
  362. }
  363. throw new \RuntimeException('Foo.');
  364. }
  365. public function compareVersions(string $versionA, string $versionB): int
  366. {
  367. return (new NewVersionChecker(
  368. new class implements GithubClientInterface {
  369. public function getTags(): array
  370. {
  371. throw new \LogicException('Not implemented.');
  372. }
  373. }
  374. ))->compareVersions($versionA, $versionB);
  375. }
  376. };
  377. }
  378. private function createPharCheckerDouble(): PharCheckerInterface
  379. {
  380. return new class implements PharCheckerInterface {
  381. public function checkFileValidity(string $filename): ?string
  382. {
  383. return null;
  384. }
  385. };
  386. }
  387. }