-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiCommandTest.php
More file actions
207 lines (182 loc) · 7.39 KB
/
WikiCommandTest.php
File metadata and controls
207 lines (182 loc) · 7.39 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?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\Console\Command;
use FastForward\DevTools\Composer\Json\ComposerJsonInterface;
use FastForward\DevTools\Console\Command\Traits\LogsCommandResults;
use FastForward\DevTools\Console\Command\WikiCommand;
use FastForward\DevTools\Filesystem\FilesystemInterface;
use FastForward\DevTools\Git\GitClientInterface;
use FastForward\DevTools\Path\DevToolsPathResolver;
use FastForward\DevTools\Process\ProcessBuilderInterface;
use FastForward\DevTools\Process\ProcessQueueInterface;
use FastForward\DevTools\Path\ManagedWorkspace;
use FastForward\DevTools\Path\WorkingProjectPathResolver;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\Attributes\UsesTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use ReflectionMethod;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
#[CoversClass(WikiCommand::class)]
#[UsesClass(DevToolsPathResolver::class)]
#[UsesClass(ManagedWorkspace::class)]
#[UsesClass(WorkingProjectPathResolver::class)]
#[UsesTrait(LogsCommandResults::class)]
final class WikiCommandTest extends TestCase
{
use ProphecyTrait;
private ObjectProphecy $processBuilder;
private ObjectProphecy $processQueue;
private ObjectProphecy $composer;
private ObjectProphecy $filesystem;
private ObjectProphecy $gitClient;
private ObjectProphecy $logger;
private ObjectProphecy $input;
private ObjectProphecy $output;
private ObjectProphecy $process;
private WikiCommand $command;
/**
* @return void
*/
protected function setUp(): void
{
$this->processBuilder = $this->prophesize(ProcessBuilderInterface::class);
$this->processQueue = $this->prophesize(ProcessQueueInterface::class);
$this->composer = $this->prophesize(ComposerJsonInterface::class);
$this->filesystem = $this->prophesize(FilesystemInterface::class);
$this->gitClient = $this->prophesize(GitClientInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->input = $this->prophesize(InputInterface::class);
$this->output = $this->prophesize(OutputInterface::class);
$this->process = $this->prophesize(Process::class);
$this->composer->getDescription()
->willReturn('Fast Forward Dev Tools plugin');
$this->composer->getAutoload('psr-4')
->willReturn([
'FastForward\\DevTools\\' => 'src/',
]);
$this->input->getOption('target')
->willReturn('.github/wiki');
$this->input->getOption('cache-dir')
->willReturn(ManagedWorkspace::getCacheDirectory(ManagedWorkspace::PHPDOC));
$this->input->getOption('cache')
->willReturn(false);
$this->input->getOption('no-cache')
->willReturn(false);
$this->input->getOption('init')
->willReturn(false);
$this->input->getOption('json')
->willReturn(false);
$this->input->getOption('pretty-json')
->willReturn(false);
$this->processBuilder->withArgument(Argument::any())->willReturn($this->processBuilder->reveal());
$this->processBuilder->withArgument(Argument::any(), Argument::any())->willReturn(
$this->processBuilder->reveal()
);
$this->processBuilder->build([DevToolsPathResolver::getPreferredToolBinaryPath('phpdoc')])
->willReturn($this->process->reveal());
$this->command = new WikiCommand(
$this->processBuilder->reveal(),
$this->processQueue->reveal(),
$this->composer->reveal(),
$this->filesystem->reveal(),
$this->gitClient->reveal(),
$this->logger->reveal(),
);
}
/**
* @return void
*/
#[Test]
public function executeWillReturnSuccessWhenProcessQueueSucceeds(): void
{
$this->processBuilder->withArgument(
'--cache-folder',
ManagedWorkspace::getCacheDirectory(ManagedWorkspace::PHPDOC)
)
->willReturn($this->processBuilder->reveal())
->shouldBeCalled();
$this->processQueue->add($this->process->reveal(), Argument::cetera())
->shouldBeCalled();
$this->processQueue->run($this->output->reveal())
->willReturn(WikiCommand::SUCCESS)
->shouldBeCalled();
$this->logger->info('Generating wiki documentation...', Argument::that(
static fn(array $context): bool => $context['input'] instanceof InputInterface
))
->shouldBeCalled();
$this->logger->log(
'info',
'Wiki documentation generated successfully.',
Argument::that(static fn(array $context): bool => $context['input'] instanceof InputInterface
&& $context['output'] instanceof OutputInterface),
)->shouldBeCalled();
self::assertSame(WikiCommand::SUCCESS, $this->executeCommand());
}
/**
* @return void
*/
#[Test]
public function executeWithNoCacheWillSkipPhpDocumentorCacheFolder(): void
{
$this->input->getOption('no-cache')
->willReturn(true);
$this->processBuilder->withArgument('--cache-folder', Argument::cetera())
->shouldNotBeCalled();
$this->processQueue->add($this->process->reveal(), Argument::cetera())
->shouldBeCalled();
$this->processQueue->run($this->output->reveal())
->willReturn(WikiCommand::SUCCESS)
->shouldBeCalled();
self::assertSame(WikiCommand::SUCCESS, $this->executeCommand());
}
/**
* @return void
*/
#[Test]
public function executeWithInitWillSkipExistingWikiSubmodule(): void
{
$this->input->getOption('init')
->willReturn(true);
$this->filesystem->getAbsolutePath('.github/wiki')
->willReturn('/repo/.github/wiki');
$this->filesystem->exists('/repo/.github/wiki')
->willReturn(true);
$this->logger->log(
'info',
'Wiki submodule already exists at {wiki_submodule_path}.',
Argument::that(fn(array $context): bool => '/repo/.github/wiki' === $context['wiki_submodule_path']
&& $this->input->reveal() === $context['input']),
)->shouldBeCalled();
self::assertSame(WikiCommand::SUCCESS, $this->executeCommand());
}
/**
* @return int
*/
private function executeCommand(): int
{
return (new ReflectionMethod($this->command, 'execute'))
->invoke($this->command, $this->input->reveal(), $this->output->reveal());
}
}