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