1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace hanneskod\readmetester\Command; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use hanneskod\readmetester\ReadmeTester; |
13
|
|
|
use hanneskod\readmetester\SourceFileIterator; |
14
|
|
|
use hanneskod\readmetester\Expectation\Regexp; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* CLI command to run test |
18
|
|
|
*/ |
19
|
|
|
class TestCommand extends Command |
20
|
|
|
{ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this->setName('test') |
24
|
|
|
->setDescription('Test examples in readme file') |
25
|
|
|
->addArgument( |
26
|
|
|
'filename', |
27
|
|
|
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
28
|
|
|
'One or more files or directories to test', |
29
|
|
|
['README.md'] |
30
|
|
|
) |
31
|
|
|
->addOption( |
32
|
|
|
'filter', |
33
|
|
|
null, |
34
|
|
|
InputOption::VALUE_REQUIRED, |
35
|
|
|
'Filter which examples to test using a regular expression' |
36
|
|
|
) |
37
|
|
|
->addOption( |
38
|
|
|
'bootstrap', |
39
|
|
|
null, |
40
|
|
|
InputOption::VALUE_REQUIRED, |
41
|
|
|
'A "bootstrap" PHP file that is run before testing' |
42
|
|
|
) |
43
|
|
|
->addOption( |
44
|
|
|
'no-auto-bootstrap', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
"Don't try to load a local composer autoloader when boostrap is not definied" |
48
|
|
|
) |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
53
|
|
|
{ |
54
|
|
|
$this->bootstrap($input, $output); |
55
|
|
|
|
56
|
|
|
$tester = new ReadmeTester; |
57
|
|
|
$exitStatus = 0; |
58
|
|
|
|
59
|
|
|
$filter = $input->getOption('filter') ? new Regexp($input->getOption('filter')) : null; |
60
|
|
|
|
61
|
|
|
foreach ($input->getArgument('filename') as $source) { |
62
|
|
|
foreach (new SourceFileIterator($source) as $filename => $contents) { |
63
|
|
|
$output->writeln("Testing examples in <comment>$filename</comment>"); |
64
|
|
|
|
65
|
|
|
foreach ($tester->test($contents) as $exampleName => $returnObj) { |
66
|
|
|
if ($filter && !$filter->isMatch($exampleName)) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($returnObj->isSuccess()) { |
71
|
|
|
$output->writeln(" <info>Example $exampleName: {$returnObj->getMessage()}</info>"); |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$output->writeln(" <error>Example $exampleName: {$returnObj->getMessage()}</error>"); |
76
|
|
|
$exitStatus = 1; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $exitStatus; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function bootstrap(InputInterface $input, OutputInterface $output) |
85
|
|
|
{ |
86
|
|
|
if ($filename = $input->getOption('bootstrap')) { |
87
|
|
|
if (!file_exists($filename) || !is_readable($filename)) { |
88
|
|
|
throw new \RuntimeException("Unable to bootstrap $filename"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($output->isVerbose()) { |
|
|
|
|
92
|
|
|
$output->writeln("Loading bootstrap <comment>$filename</comment>"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return require_once $filename; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$input->getOption('no-auto-bootstrap') && is_readable('vendor/autoload.php')) { |
99
|
|
|
if ($output->isVerbose()) { |
|
|
|
|
100
|
|
|
$output->writeln("Loading bootstrap <comment>vendor/autoload.php</comment>"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return require_once 'vendor/autoload.php'; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: