|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Fast Forward Development Tools for PHP projects. |
| 7 | + * |
| 8 | + * This file is part of fast-forward/dev-tools project. |
| 9 | + * |
| 10 | + * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | + * @license https://opensource.org/licenses/MIT MIT License |
| 12 | + * |
| 13 | + * @see https://github.com/php-fast-forward/ |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | + * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | + */ |
| 19 | + |
| 20 | +namespace FastForward\DevTools\Tests\SelfUpdate; |
| 21 | + |
| 22 | +use FastForward\DevTools\Process\ProcessBuilderInterface; |
| 23 | +use FastForward\DevTools\SelfUpdate\ComposerVersionChecker; |
| 24 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 25 | +use PHPUnit\Framework\Attributes\Test; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 28 | +use Prophecy\Prophecy\ObjectProphecy; |
| 29 | +use ReflectionMethod; |
| 30 | +use Symfony\Component\Process\Process; |
| 31 | + |
| 32 | +#[CoversClass(ComposerVersionChecker::class)] |
| 33 | +final class ComposerVersionCheckerTest extends TestCase |
| 34 | +{ |
| 35 | + use ProphecyTrait; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ObjectProphecy<ProcessBuilderInterface> |
| 39 | + */ |
| 40 | + private ObjectProphecy $processBuilder; |
| 41 | + |
| 42 | + private ComposerVersionChecker $checker; |
| 43 | + |
| 44 | + /** |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + $this->processBuilder = $this->prophesize(ProcessBuilderInterface::class); |
| 50 | + $this->checker = new ComposerVersionChecker($this->processBuilder->reveal()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + #[Test] |
| 57 | + public function resolveLatestStableVersionWillReturnFirstStableComposerVersion(): void |
| 58 | + { |
| 59 | + $process = new Process([ |
| 60 | + 'php', |
| 61 | + '-r', |
| 62 | + 'echo json_encode(["versions" => ["1.x-dev", "v1.24.3", "v1.24.2"]]);', |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->expectComposerShowLookup($process); |
| 66 | + |
| 67 | + self::assertSame('v1.24.3', $this->invokeResolveLatestStableVersion($this->checker)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + #[Test] |
| 74 | + public function resolveLatestStableVersionWillReturnNullWhenComposerShowFails(): void |
| 75 | + { |
| 76 | + $process = new Process(['php', '-r', 'fwrite(STDERR, "lookup failed"); exit(1);']); |
| 77 | + |
| 78 | + $this->expectComposerShowLookup($process); |
| 79 | + |
| 80 | + self::assertNull($this->invokeResolveLatestStableVersion($this->checker)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param Process $process |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + private function expectComposerShowLookup(Process $process): void |
| 89 | + { |
| 90 | + $builder = $this->processBuilder->reveal(); |
| 91 | + |
| 92 | + $this->processBuilder->withArgument('fast-forward/dev-tools') |
| 93 | + ->willReturn($builder) |
| 94 | + ->shouldBeCalledOnce(); |
| 95 | + $this->processBuilder->withArgument('--available') |
| 96 | + ->willReturn($builder) |
| 97 | + ->shouldBeCalledOnce(); |
| 98 | + $this->processBuilder->withArgument('--format=json') |
| 99 | + ->willReturn($builder) |
| 100 | + ->shouldBeCalledOnce(); |
| 101 | + $this->processBuilder->withArgument('--no-interaction') |
| 102 | + ->willReturn($builder) |
| 103 | + ->shouldBeCalledOnce(); |
| 104 | + $this->processBuilder->build('composer show') |
| 105 | + ->willReturn($process) |
| 106 | + ->shouldBeCalledOnce(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param ComposerVersionChecker $checker |
| 111 | + * |
| 112 | + * @return ?string |
| 113 | + */ |
| 114 | + private function invokeResolveLatestStableVersion(ComposerVersionChecker $checker): ?string |
| 115 | + { |
| 116 | + return (new ReflectionMethod($checker, 'resolveLatestStableVersion'))->invoke($checker); |
| 117 | + } |
| 118 | +} |
0 commit comments