-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocsCommandTest.php
More file actions
233 lines (208 loc) · 9.01 KB
/
DocsCommandTest.php
File metadata and controls
233 lines (208 loc) · 9.01 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?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\DocsCommand;
use FastForward\DevTools\Filesystem\FilesystemInterface;
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\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Twig\Environment;
#[CoversClass(DocsCommand::class)]
#[UsesClass(DevToolsPathResolver::class)]
#[UsesClass(ManagedWorkspace::class)]
#[UsesClass(WorkingProjectPathResolver::class)]
#[UsesTrait(LogsCommandResults::class)]
final class DocsCommandTest extends TestCase
{
use ProphecyTrait;
private ObjectProphecy $processBuilder;
private ObjectProphecy $processQueue;
private ObjectProphecy $renderer;
private ObjectProphecy $filesystem;
private ObjectProphecy $composer;
private ObjectProphecy $logger;
private ObjectProphecy $input;
private ObjectProphecy $output;
private ObjectProphecy $process;
private DocsCommand $command;
/**
* @return void
*/
protected function setUp(): void
{
$this->processBuilder = $this->prophesize(ProcessBuilderInterface::class);
$this->processQueue = $this->prophesize(ProcessQueueInterface::class);
$this->renderer = $this->prophesize(Environment::class);
$this->filesystem = $this->prophesize(FilesystemInterface::class);
$this->composer = $this->prophesize(ComposerJsonInterface::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->input->getOption('source')
->willReturn('docs');
$this->input->getOption('target')
->willReturn(ManagedWorkspace::getOutputDirectory());
$this->input->getOption('cache-dir')
->willReturn(ManagedWorkspace::getCacheDirectory(ManagedWorkspace::PHPDOC));
$this->input->getOption('template')
->willReturn('vendor/fast-forward/phpdoc-bootstrap-template');
$this->input->getOption('cache')
->willReturn(false);
$this->input->getOption('no-cache')
->willReturn(false);
$this->input->getOption('progress')
->willReturn(false);
$this->input->getOption('json')
->willReturn(false);
$this->input->getOption('pretty-json')
->willReturn(false);
$this->output->getVerbosity()
->willReturn(OutputInterface::VERBOSITY_NORMAL);
$this->output->isDecorated()
->willReturn(false);
$this->output->getFormatter()
->willReturn(new OutputFormatter());
$this->filesystem->getAbsolutePath('docs')
->willReturn('/repo/docs');
$this->filesystem->getAbsolutePath(ManagedWorkspace::getOutputDirectory())
->willReturn('/repo/.dev-tools');
$this->filesystem->getAbsolutePath(ManagedWorkspace::getCacheDirectory(ManagedWorkspace::PHPDOC))
->willReturn('/repo/.dev-tools/cache/phpdoc');
$this->filesystem->getAbsolutePath('phpdocumentor.xml', '/repo/.dev-tools/cache/phpdoc')
->willReturn('/repo/.dev-tools/cache/phpdoc/phpdocumentor.xml');
$this->filesystem->getAbsolutePath('phpdocumentor.xml', sys_get_temp_dir())
->willReturn(sys_get_temp_dir() . '/phpdocumentor.xml');
$this->filesystem->makePathRelative('/repo/docs')
->willReturn('docs');
$this->filesystem->exists('/repo/docs')
->willReturn(true);
$this->composer->getAutoload('psr-4')
->willReturn([
'FastForward\\DevTools\\' => 'src/',
]);
$this->composer->getName()
->willReturn('fast-forward/dev-tools');
$this->renderer->render('phpdocumentor.xml', Argument::type('array'))->willReturn('<phpdocumentor />');
$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 DocsCommand(
$this->processBuilder->reveal(),
$this->processQueue->reveal(),
$this->renderer->reveal(),
$this->filesystem->reveal(),
$this->composer->reveal(),
$this->logger->reveal(),
);
}
/**
* @return void
*/
#[Test]
public function executeWillFailWhenSourceDirectoryIsMissing(): void
{
$this->filesystem->exists('/repo/docs')
->willReturn(false);
$this->logger->info('Generating API documentation...', Argument::that(
static fn(array $context): bool => $context['input'] instanceof InputInterface
))
->shouldBeCalled();
$this->logger->error(
'Source directory not found: {source}',
Argument::that(static fn(array $context): bool => $context['input'] instanceof InputInterface
&& '/repo/docs' === $context['source']),
)->shouldBeCalled();
self::assertSame(DocsCommand::FAILURE, $this->executeCommand());
}
/**
* @return void
*/
#[Test]
public function executeWillReturnSuccessWhenProcessQueueSucceeds(): void
{
$this->filesystem->dumpFile('phpdocumentor.xml', '<phpdocumentor />', '/repo/.dev-tools/cache/phpdoc')
->shouldBeCalled();
$this->processBuilder->withArgument('--cache-folder', '/repo/.dev-tools/cache/phpdoc')
->willReturn($this->processBuilder->reveal())
->shouldBeCalled();
$this->processQueue->add($this->process->reveal(), Argument::cetera())
->shouldBeCalled();
$this->processQueue->run($this->output->reveal())
->willReturn(DocsCommand::SUCCESS)
->shouldBeCalled();
$this->logger->info('Generating API documentation...', Argument::that(
static fn(array $context): bool => $context['input'] instanceof InputInterface
))
->shouldBeCalled();
$this->logger->log(
'info',
'API documentation generated successfully.',
Argument::that(static fn(array $context): bool => $context['input'] instanceof InputInterface
&& $context['output'] instanceof OutputInterface),
)->shouldBeCalled();
self::assertSame(DocsCommand::SUCCESS, $this->executeCommand());
}
/**
* @return void
*/
#[Test]
public function executeWithNoCacheWillSkipPhpDocumentorCacheFolder(): void
{
$this->input->getOption('no-cache')
->willReturn(true);
$this->filesystem->dumpFile('phpdocumentor.xml', '<phpdocumentor />', sys_get_temp_dir())
->shouldBeCalled();
$this->processBuilder->withArgument('--cache-folder', Argument::cetera())
->shouldNotBeCalled();
$this->processQueue->add($this->process->reveal(), Argument::cetera())
->shouldBeCalled();
$this->processQueue->run($this->output->reveal())
->willReturn(DocsCommand::SUCCESS)
->shouldBeCalled();
self::assertSame(DocsCommand::SUCCESS, $this->executeCommand());
}
/**
* @return int
*/
private function executeCommand(): int
{
return (new ReflectionMethod($this->command, 'execute'))
->invoke($this->command, $this->input->reveal(), $this->output->reveal());
}
}