-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplication.php
More file actions
executable file
·81 lines (71 loc) · 2.69 KB
/
Application.php
File metadata and controls
executable file
·81 lines (71 loc) · 2.69 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
<?php declare(strict_types=1);
namespace TH\DocTest;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;
use Symfony\Component\Console\SingleCommandApplication;
use TH\Maybe\Option;
#[AsCommand("doctest")]
final class Application extends SingleCommandApplication
{
// phpcs:disable SlevomatCodingStandard.Functions.FunctionLength.FunctionLength
protected function configure(): void
{
$this
->setVersion("1.0.0")
->setHelp("Test code examples found in comments")
->addArgument(
"paths",
Input\InputArgument::IS_ARRAY,
"Folders and files to look for PHP code examples in",
default: ["src"],
)
->addOption("bail", "b", Input\InputOption::VALUE_NEGATABLE, "Stop after the first failure", default: false)
->addOption(
"filter",
"f",
Input\InputOption::VALUE_REQUIRED,
"Ony run code blocks whose names match the filter",
default: "",
)
->addOption(
"languages",
"l",
Input\InputOption::VALUE_REQUIRED | Input\InputOption::VALUE_IS_ARRAY,
"Ony run code blocks whose language match." . PHP_EOL
. "(\"*\" to match any language, \"\" to match unspecified language)",
default: ["php"],
);
}
protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int
{
$testSuite = TestSuite::fromPaths(
$input->getArgument("paths"),
$input->getOption("filter"),
$this->getLanguages($input),
);
$testSuite->addSubscriber(new Subscriber\ProgressBar($input, $output));
$testSuite->addSubscriber(new Subscriber\Progress($input, $output));
$testSuite->addSubscriber(new Subscriber\TestSetup());
$testSuite->addSubscriber(new Subscriber\Summary($input, $output));
$testSuite->addSubscriber(new Subscriber\TestExecutor());
return $testSuite->run($input->getOption("bail") ?? false)
? Command::SUCCESS
: Command::FAILURE;
}
/**
* @return Option<array<string>>
*/
private function getLanguages(Input\InputInterface $input): Option
{
$languages = [];
foreach ($input->getOption("languages") as $lang) {
if ($lang === '*') {
return Option\none();
}
$languages[] = $lang;
}
return Option\some($languages);
}
}